Salut, je possède une classe qui me sert juste de type generique pour toutes les fonctions, bref, voici le code :

Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
 
#ifndef ODFAEG_FAST_DELEGATE_HPP
#define ODFAEG_FAST_DELEGATE_HPP
#include <functional>
#include <iostream>
#include "export.hpp"
#include "erreur.h"
#include <tuple>
#include <utility>
#include <memory>
/**
  *\namespace odfaeg
  * the namespace of the Opensource Development Framework Adapted for Every Games.
  */
namespace odfaeg {
/**
*  \file  fastDelegate.h
*  \class IRefVal
*  \brief Interface for the warppers to references, values and pointers.
*  This class is used store references, pointers and values to pass to the callack's functions.
*  \param T : the type of the value to wrap.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct IRefVal {
    /**\fn
    *  \brief default constructor
    */
	IRefVal()=default;
	/**\fn T& get()
        *  \brief get the reference of the wrapped type.
        *  \return the reference of the wrapped type.
        */
	virtual T& get() =0;
	/**\fn std::unique_ptr<IRefVal<T>> clone() const = 0;
        *  \brief copy the wrapper.
        *  \return the copied wrapper.*/
	virtual std::unique_ptr<IRefVal<T>> clone() const = 0;
	/**\fn destructor*/
	virtual ~IRefVal(){}
 
protected:
    /**\fn IRefVal(const IRefVal&)
    *  \brief constructor, pass the reference to wrap.
    *  \param const IRefVal& : the reference to wrap.
    */
	IRefVal(const IRefVal&){}
	/** \fn IRefVal& operator=(const IRefVal&)
        *   \brief affector.
        *   \param const IRefVal& : the wrapper to affect.
        *   \return the affected wrapper.*/
	IRefVal& operator=(const IRefVal&)
	{ return *this; }
};
/**
*  \file  fastDelegate.h
*  \class Ref
*  \brief Warp a reference. (Use std::reference_wrapper so we can pass a reference with std::ref)
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct Ref : IRefVal<T> {
    /**
    *\fn Ref(const std::reference_wrapper<T>& r)
    *\brief Constructor : pass an std::reference_wrapper to the wrapper.
    *\param std::reference_wrapper<T>& r : the reference_wrapper.
    */
	Ref(const std::reference_wrapper<T>& r)
		: ref(r)
	{}
	/**
        * \fn T& get()
        * \brief return a reference to an object.
        * \return T& the reference to the object.
        */
	T& get()
	{ return ref.get(); }
	/**
        * \fn std::unique_ptr<IRefVal<T>> clone() const
        * \brief copy the reference wrapper.
        * \return std::unique_ptr<IRefVal<T>> : the cloned wrapper.
        */
	std::unique_ptr<IRefVal<T>> clone() const
	{ return std::make_unique<Ref>(*this); }
private:
	std::reference_wrapper<T> ref; /**> the std::reference_wrapper which warp the reference.*/
};
/**
*  \file  fastDelegate.h
*  \class Val
*  \brief Warp a value.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct Val : IRefVal<T> {
    /**\fn Val(const T& t)
    *  \brief pass a value to the wrapper.
    *  \param const T& t : the value to pass.
    */
	Val(const T& t)
		: val(t)
	{}
	/** \fn
        *   \brief return the value
        *   \return T& : return the value.
        */
	T& get()
	{ return val; }
	/**
        * \fn std::unique_ptr<IRefVal<T>> clone() const
        * \brief copy the value wrapper.
        * \return std::unique_ptr<IRefVal<T>> : the cloned wrapper.
        */
	std::unique_ptr<IRefVal<T>> clone() const
	{ return std::make_unique<Val>(*this); }
private:
	T val; /**> T val : keep the value of the wrapper.*/
};
/**
*  \file  fastDelegate.h
*  \class Ref
*  \brief Warp a pointer.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct Pointer : IRefVal<T> {
    /**\fn Pointer(const T* t)
    *  \brief pass a pointer to the wrapper.
    *  \param const T* t : the pointer to pass.
    */
	Pointer(const T* t)
		: pointer(t)
	{}
	/** \fn
        *   \brief return the pointer of the wrapper.
        *   \return T& : return the pointer.
        */
	T& get()
	{ return pointer; }
	/**
        * \fn std::unique_ptr<IRefVal<T>> clone() const
        * \brief copy the pointer wrapper.
        * \return std::unique_ptr<IRefVal<T>> : the cloned wrapper.
        */
	std::unique_ptr<IRefVal<T>> clone() const
	{ return std::make_unique<Pointer<T>>(*this); }
 
private:
	T pointer; /**> T pointer : keep the pointer of the wrapper.*/
};
/**
*  \file  fastDelegate.h
*  \class RefVal
*  \brief Wrap a pointer, a value or a reference and keep a pointer to the generic wrapper.
*  Call the right constructor depending on the wrapper's or value's type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct RefVal {
    /**
    * \fn RefVal (const T& t)
    * \brief constructor : construct a wrapper to a value
    * \param const T& t : the value to
    */
	RefVal(const T& t)
	: rv(std::make_unique<Val<T>>(t))
	{}
	/**
    * \fn RefVal (const std::reference_wrapper<T>& r)
    * \brief constructor : construct a wrapper to an std::reference_wrapper.
    * \param const std::reference_wrapper<T>& : the std::reference_wrapper to pass.
    */
	RefVal(const std::reference_wrapper<T>& r)
	: rv(std::make_unique<Ref<T>>(r))
	{}
	/**
    * \fn RefVal (const std::reference_wrapper<T>& r)
    * \brief constructor : construct a wrapper to a pointer.
    * \param const T*& p : the pointer to pass.
    */
	RefVal(const T*& p)
	: rv(std::make_unique<Pointer<T>>(p))
	{}
	/** \fn RefVal (const RefVal& rhs)
        *   \brief copy constructor, copy the wrapper with the right wrapper type.
        *   \param const RefVal& rhs : the wrapper to copy.
        */
	RefVal(const RefVal& rhs)
    {
        rv = rhs.rv->clone();
    }
    /** \fn RefVal& operator= (const RefVal& rhs)
        *   \brief affector.
        *   \param const RefVal& rhs : the wrapper to affect.
        */
	RefVal& operator=(const RefVal& rhs)
	{ rv=rhs.rv->clone(); return *this; }
	/** \fn T& get() const
        *   \brief get the wrapper.
        *   \return a unique pointer to the generic wrapper interface.
        */
	T& get() const
	{ return rv->get(); }
 
private:
	std::unique_ptr<IRefVal<T>> rv; /**> a pointer to the generic wrapper interface.*/
};
//Classe de trait pour déterminer le type à stocker
 
//(Interne) Cas général
/**
*  \file  fastDelegate.h
*  \class ToStoreImpl
*  \param T the type of the wrapper.
*  \brief Trait class which use an alias on a wrapped type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct ToStoreImpl
{ using type = T; };
/**
*  \file  fastDelegate.h
*  \class ToStoreImpl
*  \param T the type warpped in the warpper.
*  \brief Trait class with keep an alias on the wrapped type.
*  This class is specialized for std::_reference_wrapper type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct ToStoreImpl<std::reference_wrapper<T>>
{ using type = T; };
/**
*  \file  fastDelegate.h
*  \class ToStore
*  \param T the type of the wrapper.
*  \brief Trait class with keep an alias of the wrapper type. (without the reference)
*  the inheritance use the right specialized templated class to hold the type of the wrapped object
*  depending on the wrapper type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
struct ToStore
	: ToStoreImpl<std::remove_reference_t<T>>
{};
/**
*  \file  fastDelegate.h
*  \class ToStore_t
*  \param T the type of the wrapped object.
*  \brief Trait class which hold an alias to the real type of the wrapped type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class T>
using ToStore_t = typename
	ToStore<T>::type;
/**
*  \file  fastDelegate.h
*  \class DynamicWrapper
*  \param R the return type of the wrapped functor.
*  \param C the class type of the wrapped functor.
*  \param ArgT the arguments types of the wrapped functor.
*  \brief This class warp a function pointer to a member function.
*  I don't use an std::function directly here to keep the class type of the member function pointer
*  because if the passed object is polymorphic, I need to apply a dynamic cast
*  before calling the member function pointer on the object.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class R, class C, class... ArgT>
struct DynamicWrapper {
    /**\fn DynamicWrapper(R(C::*pf)(ArgT...)) : pfunc(pf)
    *  \brief warp a pointer to a member function.
    *  \param R(C::*pf)(ArgT...) : the pointer to the member function.
    */
	DynamicWrapper(R(C::*pf)(ArgT...)) : pfunc(pf){}
	/**\fn R operator()(O* o, ArgU&&... arg) const
        *  \brief call the member function's pointer witht he given object and the given argument
        *  apply a dynamic_cast in case of the type of the object and the type of the classe containing the member function
        *  are not the same. (If the object is polymoprhic)
        *  If the cast fails it means that the object is not polymorphic so it throws an error.
        *  \param O* o : the object onwich to call the member function's pointer.
        *  \param ArgU&& arg : the arguments of the member function's pointer to call.
        */
	template<class O, class... ArgU>
	R operator()(O* o, ArgU&&... arg) const
	{
		if(dynamic_cast<C*>(o))
			return (dynamic_cast<C*>(o)->*pfunc)(std::forward<ArgU>(arg)...);
        throw Erreur(0, "Invalid cast : types are nor polymorphic!", 1);
	}
	template<class O, class... ArgU>
	R operator()(O& o, ArgU&&... arg) const
	{
		if(dynamic_cast<C&>(o))
			return (dynamic_cast<C&>(o)->*pfunc)(std::forward<ArgU>(arg)...);
        throw Erreur(0, "Invalid cast : types are nor polymorphic!", 1);
	}
private:
	R (C::*pfunc)(ArgT...); /**> a pointer to a member's function.*/
};
/**
*  \file  fastDelegate.h
*  \class DynamicFunction
*  \param F the type of the function.
*  \brief Generic class for every functors type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class F>
class DynamicFunction;
/**
*  \file  fastDelegate.h
*  \class DynamicFunction
*  \param R the return type of the function.
*  \param ArgT... the type of the arguments of the function.
*  \brief Specialized template class for functors. (inherit from std::function)
*  build a functor with the right constructor depending a the pointer's function type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class R, class... ArgT>
class DynamicFunction<R(ArgT...)>
	: std::function<R(ArgT...)>
{
    /**> just an alias to the type of the base class.*/
	using Base = std::function<R(ArgT...)>;
 
public:
    /**
    * \fn DynamicFunction(F&& f)
    * \brief pass a functor to the std::function.
    * \param F&& f : the functor to pass to the std::function.
    */
	template<class F>
	DynamicFunction(F&& f) : Base(std::forward<F>(f))
	{}
	/** \fn DynamicFunction (R (C::*pf)(ArgU...))
        *   \brief pass a pointer to a member's function to the DynamicWrapper, and then
        *   pass this wrapper to the std::function, so, the std::function call the operator() of the DynamicWrapper class
        *   and not the operator() of the std::function class so we can perform the dynamic_cast if necessary.
        *   \param R(C::*pf)(ArgU...) : the pointer to the member's function.
        *   \
        */
	template<class C, class... ArgU>
	DynamicFunction(R(C::*pf)(ArgU...))
		: Base(DynamicWrapper<R,C,ArgU...>(pf))
	{}
	/**> just an allias to the operator() of the base class.*/
    using Base::operator();
};
/**
*  \file  fastDelegate.h
*  \class Delegate
*  \param R the return type of the function.
*  \brief Interface with can hold a delegate to every delegates types.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class R>
struct Delegate {
    /**\fn Delegate()
    *  \brief default constructor.
    */
	Delegate() =default;
	/**\fn virtual std::unique_ptr<Delegate> clone() const = 0;
    *  \brief pure virtual function to redefine in the subclass to copy the delegate.
    *  \return std::unique_ptr<Delegate> a pointer to the copied delegate.
    */
	virtual std::unique_ptr<Delegate> clone() const = 0;
	/**\fn R operator()() = 0;
    *  \brief pure virtual function to redefines to call the std::function of the delegate.
    *  \return R : return the value returned by the std::function.
    */
	virtual R operator()() = 0;
	/** /fn virtual Delegate()
        * \brief destructor
        */
	virtual ~Delegate(){}
 
protected:
    /**
    * \fn Delegate (const Delegate&) {}
    * \brief copy constructor.
    * \param const Delegate& : the delegate to copy.
    */
	Delegate(const Delegate&){}
	/**
    * \fn Delegate& operator= (const Delegate&) {}
    * \brief affector.
    * \return Delegate& : return a reference to the current delegate.
    */
	Delegate& operator=(const Delegate&)
	{
        return *this;
	}
};
/**
*  \file  fastDelegate.h
*  \class FastDelegateImpl
*  \brief Implementation of the delegate's interfaces.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class R, class... ArgT>
struct FastDelegateImpl : Delegate<R> {
    /** \fn FastDelegateImpl(F&& f, ArgU&&... arg)
    *   \brief constructor : create the delegate with the functor and the arguments value passed.
    */
	template<class F, class... ArgU>
	FastDelegateImpl(F&& f, ArgU&&... arg)
		: func(std::forward<F>(f))
		, param(std::forward<ArgU>(arg)...)
	{}
	/** \fn std::unique_ptr<Delegate<R>> clone() const
    *   \brief make a copy of the delegate.
    *   \return the copied delegate.
    */
	std::unique_ptr<Delegate<R>> clone() const
	{ return std::make_unique<FastDelegateImpl>(*this); }
	/** \fn R operator()()
        *   \brief call the std::function of the delegate.
    *   \return the value returned by the std::function.
    */
	R operator()()
	{ return call(std::make_index_sequence<sizeof...(ArgT)>()); }
	/** \fn R operator()()
        *   \brief changed the parameter's values of the std::function.
    *   \param ArgU&&... args : the values for the parameters to the delegate's function.
    */
	template<class... ArgU>
	void setParams(ArgU&&... arg)
	{ param=std::make_tuple(std::forward<ArgU>(arg)...); }
 
private:
    /** \fn R call(std::index_sequence<I...>)
        *   \brief pass each elements of the tuple to the std::function and unwarp them.
    *   \param std::index_sequence<I...> : a sequence of indexes to get every elements of the tuple.
    *   \return the value returned by the std::function.
    */
	template<std::size_t... I>
	R call(std::index_sequence<I...>)
	{ return func(std::get<I>(param).get()...); }
	std::function<R(ArgT&...)> func; /**> a functor whith hold the pointer to a callback function.*/
	std::tuple<RefVal<ArgT>...> param; /**> the wrapped values of the parameters to pass to the callback's function.*/
};
/**
*  \file  fastDelegate.h
*  \class FastDelegate
*  \brief Class used for the type erasure,
*  which allow the user be able to store a set of different callback's functions types with the same return type.
*  \author Duroisin.L
*  \version 1.0
*  \date 1/02/2014
*/
template<class R>
struct FastDelegate {
    /**\fn default constructor
    */
    FastDelegate() = default;
    /**\fn FastDelegate (F&& f, Arg&& arf)
    *  \param F&& f : the functor to pass.
    *  \param Arg&&... arg : arguments to pass to the functor.
    */
    template<class F, class... Arg>
	FastDelegate(F&& f, Arg&&... arg) :
		delegate(std::make_unique
			<FastDelegateImpl<R,ToStore_t<Arg>...>>
			(std::forward<F>(f),std::forward<Arg>(arg)...)
		)
	{}
 
	/**\fn FastDelegate (FastDelegate& rhs)
        *  \brief copy constructor.
        *  \param FastDelegate& rhs : the delegate to copy.
        */
	FastDelegate(FastDelegate& rhs)
		: delegate(rhs.delegate->clone())
	{}
	/**\fn FastDelegate (const FastDelegate& rhs)
        *  \brief copy constructor.
        *  \param const FastDelegate& rhs : the delegate to copy.
        */
	FastDelegate(const FastDelegate& rhs)
		: delegate(rhs.delegate->clone())
	{}
	/**\fn FastDelegate (FastDelegate& rhs)
        *  \brief move constructor.
        *  \param FastDelegate&& rhs : the delegate to move.
        */
	FastDelegate(FastDelegate&& rhs) =default;
	/**\fn FastDelegate& operator= (FastDelegate& rhs)
        *  \brief affect the content of the delegate to the delegate.
        *  \param const FastDelegate& rhs : the delegate affect.
        *  \return FastDelegate& : the affected delegate.
        */
	FastDelegate& operator=(const FastDelegate& rhs)
	{ return operator=(FastDelegate(rhs)); }
	/**\fn FastDelegate& operator= (FastDelegate& rhs)
        *  \brief affect and move the content of the delegate to the delegate.
        *  \param const FastDelegate& rhs : the delegate to move and to affect.
        *  \return FastDelegate& : the moved and affected delegate.
        */
	FastDelegate& operator=(FastDelegate&&) =default;
	/**\fn R operator()() const;
        *  \brief call the std::function of the delegate.
        *  \return the value returned by the std::function.
        */
	R operator()() const
	{ return (*delegate)(); }
	/**\fn setParams(Arg&&... arg)
        *  \brief change the parameter's values of the delegate.
        *  \return Arg&&... arg : the values of the parameters of the delegate.
        */
	template<class... Arg>
	void setParams(Arg&&... arg)
	{
		using StaticType =
			FastDelegateImpl<R,ToStore_t<Arg>...>*;
		static_cast<StaticType>(delegate.get())->setParams(std::forward<Arg>(arg)...);
	}
private:
	std::unique_ptr<Delegate<R>> delegate; /**> holds the pointer to the generic delegate.*/
};
}
#endif

Le problème vient de la fonction setParams qui ne passe pas bien un pointeur sur un objet polymorphique, je m'explique ;

J'ai un classe abstraite de base (Entity), une classe abstraite dérivée (Model) qui hérite de Entity, et une troisième classe (Wall) qui hérite de Model. (Le crash ne survient que dans ce cas là)

Donc pour une classe B qui hérite directement de Entity, pas de problème, par contre pour les classes qui n'héritent pas directement de Entity, j'ai ce crash.

Le crash survient lorsque je fait appel à setParams, on dirait que les valeurs de mes variables de ma sous classe son modifiée lors du dynamic_cast (dans le dynamic wrapper) et je ne comprend pas pourquoi, bref voici un code :

Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
int main (int argv, char* argc[]) {
    EXPORT_CLASS_GUID(BoundingVolumeBoundingBox, odfaeg::BoundingVolume, odfaeg::BoundingBox)
    EXPORT_CLASS_GUID(EntityTile, odfaeg::g2d::Entity, odfaeg::g2d::Tile)
    EXPORT_CLASS_GUID(EntityWall, odfaeg::g2d::Entity, odfaeg::g2d::Wall)
    EXPORT_CLASS_GUID(EntityShadowTile, odfaeg::g2d::Entity, odfaeg::g2d::ShadowTile)
    EXPORT_CLASS_GUID(EntityShadowWall, odfaeg::g2d::Entity, odfaeg::g2d::ShadowWall)
    odfaeg::Texture tex;
    tex.loadFromFile("tilesets/murs.png");
    odfaeg::g2d::Tile* tWall = new odfaeg::g2d::Tile(&tex, odfaeg::Vec3f(0, 0, 0), odfaeg::Vec3f(100, 100, 0), sf::IntRect(100, 0, 100, 100));
    odfaeg::g2d::Entity* w = new odfaeg::g2d::Wall(3, 80, tWall,odfaeg::g2d::AmbientLight::getAmbientLight(), odfaeg::g2d::Shadow::SHADOW_TYPE::SHADOW_TILE);
    std::ofstream ofs("fichierDeSerialisation");
    odfaeg::OTextArchive oa(ofs);
    void(odfaeg::g2d::Wall::*f)(odfaeg::OTextArchive&) = &odfaeg::g2d::Wall::vtserialize;
 
    odfaeg::FastDelegate<void> fd(f, new odfaeg::g2d::Wall(), std::ref(oa));
    fd.setParams(w, std::ref(oa));
    fd();
    ofs.close();
    std::ifstream ifs("fichierDeSerialisation");
    odfaeg::ITextArchive ia(ifs);
    ia(w2);
    ifs.close();   
    return 0;
    MyAppli app(sf::VideoMode(800, 600, 32), "Test odfaeg");
    return app.exec();
}

Les valeurs des variables de ma classe Wall changent tout seules. :o

Et donc lors de l'appel sur mon delegate fd, ça crash.