Salut, j'ai enfin trouvé une solution qui répond à mes besoins en matière de sérialisation mais je reste bloqué sur un dernier point.

Je voudrais choisir une fonction à appelé en fonction que l'on veille sérialiser les attributs de l'objet dérivé ou non. (dans le cas de pointeurs sur objets dérivés)
J'ai pensé à std::is_polymorphic mais cette solution me pose problème surtout si je dois définir un destructeur dans la classe dérivée, car, celui-ci devient automatiquement virtuelle du coup il considère que la classe dérivée possèdes des objets dérivé hors que ce n'est pas le cas, du coup la mauvais fonction est appelée et erreur de compilation :

Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
 void operator()(O& object, D...) {
        std::cout<<"write polymorphic"<<std::endl;
        object.reg();
        object.save(*this);
    }
    template <class O,
              class = typename std::enable_if<std::is_member_function_pointer<void(O::*)(OTextArchive&)>::value && !std::is_polymorphic<O>::value && !std::is_same<O, std::string>::value>::type>
    void operator() (O& object) {
        std::cout<<"write non polymorphic"<<std::endl;
        object.serialize(*this);
    }

J'ai pensé à inclure une fonction bidon dans la classe de base et à tester si cette fonction est présente dans la classe de base à l'aide de std::is_member_function_pointer mais cela ne marche pas.

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
 
 template <class O,
              class... D,
              class = typename std::enable_if<std::is_member_function_pointer<void(O::*)(OTextArchive&)>::value && std::is_member_function_pointer<decltype(&O::foo)>::value && !std::is_same<O, std::string>::value>::type>
    void operator()(O& object, D...) {
        std::cout<<"write polymorphic"<<std::endl;
        object.reg();
        object.save(*this);
    }
    template <class O,
              class = typename std::enable_if<std::is_member_function_pointer<void(O::*)(OTextArchive&)>::value && !std::is_member_function_pointer<decltype(&O::foo)>::value && !std::is_same<O, std::string>::value>::type>
    void operator() (O& object) {
        std::cout<<"write non polymorphic"<<std::endl;
        object.serialize(*this);
    }
    template <class O,
              class = typename std::enable_if<std::is_member_function_pointer<void(O::*)(OTextArchive&)>::value && !std::is_member_function_pointer<decltype(&O::foo)>::value && !std::is_same<O, std::string>::value>::type>
    void operator() (O* object) {
        std::cout<<"write non polymorphic"<<std::endl;
        std::map<unsigned long long int, unsigned long long int>::iterator it = adresses.find(reinterpret_cast<unsigned long long int>(object));
        if (it != adresses.end()) {
            buffer<<it->second;
        } else {
            std::pair<unsigned long long int, unsigned long long int> newAddress (reinterpret_cast<unsigned long long int>(object), nbSerialized);
            adresses.insert(newAddress);
            buffer<<newAddress.second;
            object->serialize(*this);
            nbSerialized++;
        }
    }
    template <class O,
              class... D,
              class = typename std::enable_if<std::is_member_function_pointer<void(O::*)(OTextArchive&)>::value && std::is_member_function_pointer<decltype(&O::foo)>::value && !std::is_same<O, std::string>::value>::type>
    void operator()(O* object, D... args) {
        std::cout<<"write polymorphic"<<std::endl;
        object->reg();
        object->save(*this);
    }

Il échoue lors de la déduction de paramètres template, sans toutes parce que toutes les classes ne contiennent pas de méthodes foo;

Code : 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
 
||=== Build: Debug in ODFAEG-DEMO (compiler: GNU GCC Compiler) ===|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/main.cpp|9|warning: "G2DEKEY" redefined [enabled by default]|
/usr/local/include/odfaeg/Graphics/2D/entity.h|13|note: this is the location of the previous definition|
/usr/local/include/odfaeg/Physics/boundingBox.h||In instantiation of ‘void odfaeg::BoundingBox::serialize(Archive&) [with Archive = odfaeg::OTextArchive]’:|
/usr/local/include/odfaeg/Core/serialization.h|21|required from ‘static void odfaeg::serializer<0, B, T, A>::checkType(B*, T, A&) [with B = odfaeg::BoundingVolume; T = std::tuple<odfaeg::BoundingBox*>; A = odfaeg::OTextArchive]’|
/usr/local/include/odfaeg/Core/serialization.h|51|required from ‘void odfaeg::Serializer<B, D>::serialize(Archive&) [with Archive = odfaeg::OTextArchive; B = odfaeg::BoundingVolume; D = {odfaeg::BoundingBox}]’|
/usr/local/include/odfaeg/Physics/boundingVolume.h|109|required from ‘void odfaeg::BoundingVolume::save(Archive&) [with Archive = odfaeg::OTextArchive]’|
/usr/local/include/odfaeg/Core/archive.h|121|required from ‘void odfaeg::OTextArchive::operator()(O*, D ...) [with O = odfaeg::BoundingBox; D = {}; <template-parameter-1-3> = void]’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/main.cpp|56|required from here|
/usr/local/include/odfaeg/Physics/boundingBox.h|150|error: no match for call to ‘(odfaeg::OTextArchive) (odfaeg::Vec3f&)’|
/usr/local/include/odfaeg/Core/archive.h|50|note: candidates are:|
/usr/local/include/odfaeg/Core/archive.h|89|note: template<class O, class ... D, class> void odfaeg::OTextArchive::operator()(O&, D ...)|
/usr/local/include/odfaeg/Core/archive.h|89|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Core/archive.h|88|error: ‘foo’ is not a member of ‘odfaeg::Vec3f’|
/usr/local/include/odfaeg/Core/archive.h|96|note: template<class O, class> void odfaeg::OTextArchive::operator()(O&)|
/usr/local/include/odfaeg/Core/archive.h|96|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Core/archive.h|88|error: ‘foo’ is not a member of ‘odfaeg::Vec3f’|
/usr/local/include/odfaeg/Core/archive.h|102|note: template<class O, class> void odfaeg::OTextArchive::operator()(O*)|
/usr/local/include/odfaeg/Core/archive.h|102|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Physics/boundingBox.h|150|note:   mismatched types ‘O*’ and ‘odfaeg::Vec3f’|
/usr/local/include/odfaeg/Core/archive.h|118|note: template<class O, class ... D, class> void odfaeg::OTextArchive::operator()(O*, D ...)|
/usr/local/include/odfaeg/Core/archive.h|118|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Physics/boundingBox.h|150|note:   mismatched types ‘O*’ and ‘odfaeg::Vec3f’|
/usr/local/include/odfaeg/Core/archive.h|127|note: template<class T, class ... D, class, class> void odfaeg::OTextArchive::operator()(T&, D ...)|
/usr/local/include/odfaeg/Core/archive.h|127|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Core/archive.h|125|error: no type named ‘type’ in ‘struct std::enable_if<false, void>’|
/usr/local/include/odfaeg/Core/archive.h|135|note: template<class T, class ... D, class, class, class> void odfaeg::OTextArchive::operator()(T&, D ...)|
/usr/local/include/odfaeg/Core/archive.h|135|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Core/archive.h|133|error: no type named ‘type’ in ‘struct std::enable_if<false, void>’|
/usr/local/include/odfaeg/Core/archive.h|142|note: template<class T, class ... D, class, class> void odfaeg::OTextArchive::operator()(T*, D ...)|
/usr/local/include/odfaeg/Core/archive.h|142|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Physics/boundingBox.h|150|note:   mismatched types ‘T*’ and ‘odfaeg::Vec3f’|
/usr/local/include/odfaeg/Core/archive.h|159|note: template<class T, class ... D, class, class, class> void odfaeg::OTextArchive::operator()(T*, D ...)|
/usr/local/include/odfaeg/Core/archive.h|159|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Physics/boundingBox.h|150|note:   mismatched types ‘T*’ and ‘odfaeg::Vec3f’|
/usr/local/include/odfaeg/Core/archive.h|173|note: template<class O> void odfaeg::OTextArchive::operator()(std::vector<_RealType>&)|
/usr/local/include/odfaeg/Core/archive.h|173|note:   template argument deduction/substitution failed:|
/usr/local/include/odfaeg/Physics/boundingBox.h|150|note:   ‘odfaeg::Vec3f’ is not derived from ‘std::vector<_RealType>’|
||=== Build failed: 5 error(s), 7 warning(s) (0 minute(s), 5 second(s)) ===|
Bref je ne vois pas vraiment de solution pour régler ce problème.

Merci d'avance pour vos réponses.