Demande d'avis sur des pointeurs intelligents
Bonjour à vous tous !!.
J’espère que vous allez bien, et que pour ceux qui ont passé le bac ont réussis et qu'ils ont obtenus les vœux souhaité.
Si je viens vous voir, ce n'est pas spécialement pour vous demander de l'aide (pour une fois (a) ), mais bien pour avoir un avis sur mon code.
Je viens du langage C (langage que j'apprécie beaucoup, donc pas de troll le C c'est pourri ^^ ), mais j'ai envie d'apprendre du C++. D'après ce que j'ai vu, ce "passage" peut donner de mauvaises habitudes, etc etc, donc je veux savoir si je code dans un C++ correct ou vraiment super moche horrible :D (n'hésitez pas à le dire, c'est comme ça qu'on progresse :) ).
Bref, je ne suis pas là pour vous présentez du code super optimisé, mais juste un code sécurisé, en fait j'ai décider de "reprendre" l'idée des auto_ptr et de les "améliorer". Donc l'idée est simple, le pointeur peut se faire copier, je gère le nombre de référence etc etc. J'ai corriger toutes mes erreurs que j'ai put trouver à l'aide de valgrind, donc je pense qu'il est fonctionnel.
A part pour les itérateurs, que j'ai implémenter à la one again et bistoufly (car je n'en suis pas encore aux itérateurs en fait), je pense que j'ai fait du travail plus ou moins correct.
J'ai essayer d'imiter le plus possible le comportement d'un pointeur : Opérateur * -> [] ++ -- post et préFixé, += -=, + - etc.
Voilà mon code, si vous trouvez des erreurs, segfault, fuites de mémoires, merci de me le dire.
main.cpp
Code:
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
| #include <iostream>
#include "QGM_Ptr.h"
using namespace std;
int main(void)
{
qgm::smart_ptr<int> tableau(100);
qgm::smart_ptr<int> pointeurSurLaPremiereCase;
qgm::smart_ptr<int> ptr;
pointeurSurLaPremiereCase = tableau;
for(int i = 0; i < 102; ++i){
try{
*tableau++ = i;
}
catch(exception &e){
cout << e.what() << endl;
}
}
for(int i = 0; i < 102; ++i){
try{
cout << pointeurSurLaPremiereCase[i] << " ";
}
catch(exception &e){
cout << e.what() << endl;
}
}
try{
// Pointeur = première case du tableau
ptr = -100 + tableau;
// On affiche
cout << endl << *ptr << endl;
}
catch(exception &e){
cout << e.what() << endl;
}
// On supprime
ptr.release();
// Doit affiche qgm::bad_ptr
try{
cout << *pointeurSurLaPremiereCase << " ";
}
catch(exception &e){
cout << e.what() << endl;
}
ptr.alloc(200);
tableau = ptr;
qgm::smart_ptr<int>::iterator it = tableau.begin();
for(; it != tableau.end(); ++it)
*it = 0;
for(it = ptr.begin(); it != ptr.end(); ++it)
cout << *it << " ";
return 0;
} |
QGM_types.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #ifndef QGM_TYPES_H_INCLUDED
#define QGM_TYPES_H_INCLUDED
namespace qgm{
typedef signed char s8;
typedef unsigned char u8;
typedef signed short s16;
typedef unsigned short u16;
typedef signed long s32;
typedef unsigned long u32;
typedef signed int sint;
typedef unsigned int uint;
typedef float f32;
typedef double f64;
}
#endif // QGM_TYPES_H_INCLUDED |
qgm_ptr (le plus important haha)
Code:
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
| #ifndef QGM_PTR_H_INCLUDED
#define QGM_PTR_H_INCLUDED
#include <map>
#include <exception>
#include <string>
#include "QGM_Types.h"
namespace qgm
{
class bad_ptr : public std::exception
{
public :
bad_ptr(void) throw() : p("qgm::bad_ptr"){}
virtual char const *what(void) const throw(){
return p.c_str();
}
virtual ~bad_ptr(void) throw(){}
private :
std::string p;
};
class out_of_range_ptr : public std::exception
{
public :
out_of_range_ptr(void) throw() : p("qgm::out_of_range_ptr"){}
virtual char const *what(void) const throw(){
return p.c_str();
}
virtual ~out_of_range_ptr(void) throw(){}
private :
std::string p;
};
template<typename Type>
class smart_ptr{
/** Class gérant les pointeurs alloués sur le tas **/
public :
/** Ce constructeur permet d'avoir accès à un tableau dynamique
de taille définie **/
explicit smart_ptr(u32 size);
/** Construit un pointeur intelligent à partir d'un pointeur.
Si isArrayNelem == 0 : Le pointeur est alloué avec new
Si isArrayNelem >= 1 : Le pointeur est alloué avec new[]
et contient le nombre de "cases préallouées"
De base, on alloue avec new **/
smart_ptr(Type *ptr, u32 isArrayNelem = 0);
/** Constructeur de copie **/
smart_ptr(smart_ptr const &ptr);
/** Opérateur d'affection **/
smart_ptr &operator=(smart_ptr const &ptr);
/** Construit un pointeur NULL **/
smart_ptr(void);
/** Définie pour struct->membre **/
Type *operator->(void);
/** int i = *(int*)pInt; **/
Type &operator*(void);
/** int n = tableau[5]; **/
Type &operator[](s32 index);
/** Operateur += et -= **/
smart_ptr &operator+=(s32 i);
smart_ptr &operator-=(s32 i);
/** Opérateur d'incrémentation préFixé **/
smart_ptr &operator++(void);
smart_ptr &operator--(void);
/** Opérateur d'incrémentation postFixé **/
smart_ptr operator++(sint);
smart_ptr operator--(sint);
/** Permet d'allouer le pointeur **/
void alloc(u32 size = 1);
/** Permet de libérer le pointeur **/
void release(void);
/** Destructeur **/
~smart_ptr(void);
/** Les itérateurs **/
typedef Type* iterator;
Type *begin(void){return m_Begin;}
Type *end(void) {return m_End; }
private :
Type *m_Ptr;
Type *m_Begin; // Pointeur poinant sur le premier élément
Type *m_End; // Pointeur sur l'élément suivant le dernier
bool m_IsArray; // Est ce que le tableau est alloué avec new([])
/** Remet à 0 l'objet **/
void setZero(void);
/** Retire une référence et détruit le pointeur si besoin **/
void subPtr(void);
/** Test le pointeur **/
void testPointer(void);
};
/** Tableau associatif pour clé un pointeur
et comme valeur le nombre de référence à ce pointeur **/
static std::map<void*, u32> ptrMap;
typedef std::map<void*, u32>::iterator iteratorPtrMap;
template<typename Type>
void smart_ptr<Type>::setZero(void){
m_Ptr = m_Begin = m_End = 0;
m_IsArray = false;
}
template<typename Type>
void smart_ptr<Type>::subPtr(void){
iteratorPtrMap it = ptrMap.find(m_Begin);
/** si le pointeur existe **/
if(it != ptrMap.end()){
if(--it->second == 0){ // Si il n'est plus utilisé on supprime
ptrMap.erase(it);
if(m_IsArray == true)
delete[] m_Begin;
else
delete m_Begin;
setZero(); // On remet = 0 le pointeur
}
}
}
template<typename Type>
void smart_ptr<Type>::testPointer(void){
iteratorPtrMap it = ptrMap.find(m_Begin);
// si le pointeur n'est pas trouvé, on lève une exception
if(it == ptrMap.end())
throw bad_ptr();
}
/** Constructeurs **/
/** Constructeur NULL **/
template<typename Type>
smart_ptr<Type>::smart_ptr(void) :
m_Ptr(0),
m_Begin(0),
m_End(0),
m_IsArray(false){
}
/** Constructeur récupérant un pointeur **/
template<typename Type>
smart_ptr<Type>::smart_ptr(Type *ptr, u32 isArrayNelem) :
m_Ptr(ptr),
m_Begin(ptr),
m_End(ptr + (isArrayNelem == 0 ? 1 : isArrayNelem)),
m_IsArray(isArrayNelem == 0 ? false : true){
/** Si ce n'est pas un tableau **/
if(isArrayNelem == 0)
{
m_End = ptr + 1;
m_IsArray = false;
}
else
{
m_End = ptr + isArrayNelem;
m_IsArray = true;
}
ptrMap[m_Begin] = 1; // une utilisation
}
/** Constructeur allouant une certaines place **/
template<typename Type>
smart_ptr<Type>::smart_ptr(u32 size):
m_Ptr(new Type[size]),
m_Begin(m_Ptr),
m_End(m_Ptr + size),
m_IsArray(true){
ptrMap[m_Begin] = 1; // une utilisation
}
/** Constructeur de copie **/
template<typename Type>
smart_ptr<Type>::smart_ptr(smart_ptr const &ptr) :
m_Ptr(ptr.m_Ptr),
m_Begin(ptr.m_Begin),
m_End(ptr.m_End),
m_IsArray(ptr.m_IsArray){
iteratorPtrMap it = ptrMap.find(m_Begin);
/** Si le pointeur existe **/
if(it != ptrMap.end())
++it->second;
}
/** Opérateur = **/
template<typename Type>
smart_ptr<Type> &smart_ptr<Type>::operator=(smart_ptr const &ptr){
if(m_Ptr == ptr.m_Ptr)
return *this;
subPtr();
m_Ptr = ptr.m_Ptr;
m_Begin = ptr.m_Begin;
m_End = ptr.m_End;
m_IsArray = ptr.m_IsArray;
if(m_Ptr != 0)
++ptrMap[m_Begin]; // On ajoute une utilisation
return *this;
}
/** Fonctions d'allocations / désallocations **/
template<typename Type>
void smart_ptr<Type>::alloc(u32 size)
{
/** Si on cherche à allouer mais que c'est déjà pris **/
subPtr();
m_Ptr = new Type[size];
m_Begin = m_Ptr;
m_End = m_Ptr + size;
m_IsArray = true;
}
template<typename Type>
void smart_ptr<Type>::release(void){
iteratorPtrMap it = ptrMap.find(m_Begin);
if(it == ptrMap.end())
return;
else{
if(m_IsArray == true)
delete[] m_Begin;
else
delete m_Begin;
}
setZero();
ptrMap.erase(it);
}
/*************************************************************************/
/** Opérateurs * -> [] **/
template<typename Type>
Type &smart_ptr<Type>::operator*(void){
testPointer();
return *m_Ptr;
}
template<typename Type>
Type *smart_ptr<Type>::operator->(void){
testPointer();
if(m_Ptr < m_Begin || m_Ptr >= m_End)
throw out_of_range_ptr();
return *m_Ptr;
}
template<typename Type>
Type &smart_ptr<Type>::operator[](s32 index){
testPointer();
Type *ptr = m_Ptr + index;
if(ptr < m_Begin || ptr >= m_End)
throw out_of_range_ptr();
return *ptr;
}
/** Operateur += et -= **/
template<typename Type>
smart_ptr<Type> &smart_ptr<Type>::operator+=(s32 i){
testPointer();
Type *ptr = m_Ptr + i;
/** Le pointeur à le droit de pointé sur m_End **/
if(ptr < m_Begin || ptr > m_End)
throw out_of_range_ptr();
m_Ptr = ptr;
return *this;
}
/** Operateur += et -= **/
template<typename Type>
smart_ptr<Type> &smart_ptr<Type>::operator-=(s32 i){
testPointer();
Type *ptr = m_Ptr - i;
/** Le pointeur à le droit de pointé sur m_Begin **/
if(ptr < m_Begin || ptr > m_End)
throw out_of_range_ptr();
m_Ptr = ptr;
return *this;
}
/** Opérateur d'incrémentation préFixé **/
template<typename Type>
smart_ptr<Type> &smart_ptr<Type>::operator++(void){
return *this += 1;
}
template<typename Type>
smart_ptr<Type> &smart_ptr<Type>::operator--(void){
return *this -= 1;
}
/** Opérateur d'incrémentation postFixé **/
template<typename Type>
smart_ptr<Type> smart_ptr<Type>::operator++(sint){
smart_ptr<Type> ptr(*this);
++*this;
return ptr;
}
template<typename Type>
smart_ptr<Type> smart_ptr<Type>::operator--(sint){
smart_ptr<Type> ptr(*this);
--*this;
return ptr;
}
/** opérateur + et - de type : ptr + x **/
template<typename Type>
smart_ptr<Type> operator+(smart_ptr<Type> const &ptr1, s32 i){
smart_ptr<Type> ptr(ptr1);
ptr += i;
return ptr;
}
template<typename Type>
smart_ptr<Type> operator-(smart_ptr<Type> const &ptr1, s32 i){
smart_ptr<Type> ptr(ptr1);
ptr -= i;
return ptr;
}
/** Opérateur + et - de type : x + ptr **/
template<typename Type>
smart_ptr<Type> operator+(s32 i, smart_ptr<Type> const &ptr1){
return smart_ptr<Type>(ptr1 + i);
}
template<typename Type>
smart_ptr<Type> operator-(s32 i, smart_ptr<Type> const &ptr1){
return smart_ptr<Type>(ptr1 - i);
}
/** Destructeur **/
template<typename Type>
smart_ptr<Type>::~smart_ptr(void){
subPtr(); // On enlève une référence
}
}
#endif // QGM_PTR_H_INCLUDED |
Merci de vos conseils :)