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
|
template <typename R>
class SmartPointer {
public :
SmartPointer() :
rm (ResourceManager<R>::Instance()),
localisation("0") {
counter = new unsigned int(0);
(*counter)++;
}
SmartPointer(R* resource) :
rm (ResourceManager<R>::Instance()),
localisation(rm.make_resource(resource, ResourceManagerBase::INTERNAL)) {
counter = new unsigned int(0);
(*counter)++;
}
SmartPointer(const SmartPointer& sp) :
rm (ResourceManager<R>::Instance()),
localisation(rm.make_resource(sp.get(), ResourceManagerBase::INTERNAL)){
counter = sp.counter;
(*counter)++;
}
template <typename D>
SmartPointer(const SmartPointer<D>& sp) :
rm (ResourceManager<R>::Instance()),
localisation(rm.make_resource(sp.get(), ResourceManagerBase::INTERNAL)) {
counter = sp.counter;
(*counter)++;
}
SmartPointer& operator=(const SmartPointer& sp) {
localisation = rm.make_resource(sp.get(), ResourceManagerBase::INTERNAL);
(*sp.counter)++;
(*counter)--;
counter = sp.counter;
return *this;
}
template <typename D>
SmartPointer& operator=(const SmartPointer<D>& sp) {
localisation = rm.make_resource(sp.get(), ResourceManagerBase::INTERNAL);
(*sp.counter)++;
(*counter)--;
counter = sp.counter;
return *this;
}
std::string getLocalisation() const {
return localisation;
}
ResourceManager<R>& getResourceManager() const {
return rm;
}
unsigned int getCounter() {
return *counter;
}
R* operator-> () {
std::lock_guard<std::recursive_mutex> locker(rec_mutex);
return const_cast<R*>(rm.getResourceByPath(localisation));
}
R* get() const {
std::lock_guard<std::recursive_mutex> locker(rec_mutex);
return const_cast<R*>(rm.getResourceByPath(localisation));
}
bool operator< (const SmartPointer& sp) const {
return get() < sp.get();
}
~SmartPointer() {
std::lock_guard<std::recursive_mutex> locker(rec_mutex);
(*counter)--;
if (*counter == 0 && localisation != "0") {
rm.deleteResourceByPath(localisation, ResourceManagerBase::TYPE::INTERNAL);
}
}
unsigned int* counter;
private :
ResourceManager<R>& rm;
std::string localisation;
};
template <typename T, typename... A>
SmartPointer<T> make_smart(A&&... args) {
std::lock_guard<std::recursive_mutex> locker(rec_mutex);
SmartPointer<T> sp(new T(std::forward<A>(args)...));
return sp;
}
template <typename... A>
class SmartContainerPointer {
};
template <template <typename...> class C, class... A>
class SmartContainerPointer<C<A...>> : public C<A...> {
};
template <template <typename, typename> class C, class H, class T>
class SmartContainerPointer<C<SmartPointer<H>, T>> : public C<SmartPointer<H>, T> {
};
template <template <typename, typename> class C, class H, class T>
class SmartContainerPointer<C<H, T>> : public C<H, T> {
};
template <template <class> class C, class H>
class SmartContainerPointer<C<SmartPointer<H>>> : public C<SmartPointer<H>> {
};
template <template <class> class C, class H>
class SmartContainerPointer<C<H>> : public C<H> {
};
template <class H>
using SmartVectorPointer = SmartContainerPointer<std::vector<SmartPointer<H>>>;
template <class H, class T>
using SmartMapPointerK = SmartContainerPointer<std::map<SmartPointer<H>, T>>;
template <class H, class T>
using SmartMapPointerV = SmartContainerPointer<std::map<H, SmartPointer<T>>>;
template <class H, class T>
using SmartMapPointer = SmartContainerPointer<std::map<SmartPointer<H>, SmartPointer<T>>>;
} |
Partager