Bonjour
J'essaie de developper un FrameWork Oriente Object en C++ et pour ce faire j essaie d'utiliser les smart pointer avec un object de base gerant un compteur de reference et un type de pointer specifique permettant de liberer automatiquement l'object.
Comme je debute sur l'implementation de Smart Pointer et aussi sur les template j aimerai bien savoir si ce type d'implementation est valide et ne risque pas de me poser des probleme a terme.
J'ai regarde le code de Loki et il me semble y avoir le meme type de smart pointer cependant j ai du mal a suivre le code![]()
et je n ai pas vu dans Boost une implementation avec une classe Object de base
Fichier Pointer.h
fichier Object.h
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
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 #ifndef FRAMEWORK_POINTER_____ #define FRAMEWORK_POINTER_____ #include "Object.h" namespace foundation { template <class T> class MyPointer { private: MyObject* ObjectPointe; public: //Constructeur inline MyPointer() : ObjectPointe(NULL) { } inline MyPointer(const MyObject* inPtr) : ObjectPointe( (inPtr != NULL) ? (const_cast<MyObject*>(inPtr)->refer()) : NULL ) { } inline MyPointer( const MyPointer& inPtr ) : ObjectPointe( (inPtr.getPointer() != NULL) ? (const_cast<foundation::MyObject*>(inPtr.getPointer())->refer()) : NULL ) { } inline ~MyPointer() { if ( this->ObjectPointe != NULL ) this->ObjectPointe->unrefer(); this->ObjectPointe = NULL; } inline T* getPointer() { return static_cast<T*>( this->ObjectPointe); } inline const T* getPointer() const { return static_cast<T*> (this->ObjectPointe); } inline MyPointer& operator=(const MyObject* inPtr) { if (this->ObjectPointe == inPtr) return *this; // si le pointeur pointe deja sur l'object if ( this->ObjectPointe != NULL ) this->ObjectPointe->unrefer(); // liberation de l'object deja pointe if ( inPtr ) this->ObjectPointe = const_cast<T*>(inPtr)->refer(); else this->ObjectPointe = NULL; return *this; } inline MyPointer& operator=(const MyPointer& inPtr) { if ( this==&inPtr ) return *this; if ( this->ObjectPointe == inPtr.getPointer() ) return *this; if ( this->ObjectPointe ) this->ObjectPointe->unrefer(); if ( inPtr.getPointer() != NULL ) this->ObjectPointe = const_cast<T*>(inPtr.getPointer())->refer(); else this->ObjectPointe = NULL; return *this; } inline T& operator*() { return *(static_cast<T*> (this->ObjectPointe)); } inline const T& operator*() const { return *(static_cast<T*> (this->ObjectPointe)); } inline T* operator->() { return static_cast<T*> (this->ObjectPointe); } inline const T* operator->() const { return static_cast<T*> (this->ObjectPointe); } inline bool operator!() const { return (!this->ObjectPointe); } inline bool operator==(const MyObject* inPtr) const { return ( this->ObjectPointe == inPtr); } inline bool operator==(const MyPointer& inPtr) const { return ( this->ObjectPointe == inPtr.getPointer()); } inline bool operator!=(const MyObject* inPtr) const { return (this->ObjectPointe != inPtr); } inline bool operator!=(const MyPointer& inPtr) const { return ( this->ObjectPointe != inPtr.getPointer()); } }; } #endif
De plus pour que le FrameWork soit complet est il indispensable de definir une classe Allocator avec une interface comme ca:
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
41
42
43
44
45
46
47
48
49 #ifndef FRAMEWORK__BASEOBJECT__ #define FRAMEWORK__BASEOBJECT__ #include <iostream> namespace foundation { // forward declaration template <class T>class MyPointer; class MyObject { private: mutable unsigned int compteur; public: // Constructeur MyObject() : compteur(0) { std::cout << "Construction Base" << std::endl; } MyObject( const MyObject& inCopy ) : compteur(0) { std::cout << "Construction Base" << std::endl; } virtual ~MyObject() { std::cout << "Destruction base" << std::endl; } inline unsigned int getRefCounter() const { return this->compteur; } inline MyObject* refer() { ++this->compteur; return this; } inline void unrefer() { if ((--this->compteur) == 0) delete this; } }; } #endif
Quel en est l'utilite est ce uniquement pour permettre la gestion de conteneur?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 class Allocator : public Object { public: virtual Object* allocate() const =0; virtual Object* clone(const Object&) const =0; virtual void copy(Object&, const Object&) const =0; };
et sinon ou est ce que je peu trouver des exemple de l'utilisation des smart pointer de Loki?
Partager