Go to the documentation of this file.00001
00020 #ifndef NTK_UTILS_COMMON_H_
00021 # define NTK_UTILS_COMMON_H_
00022
00023 # include <ntk/core.h>
00024 # include <exception>
00025
00026 #define ntk_ptr_typedefs(Class) \
00027 typedef ntk::Ptr<Class> Class##Ptr; \
00028 typedef ntk::Ptr<const Class> Class##ConstPtr;
00029
00030 #define ntk_stringify(Code) #Code
00031
00032 #if (defined WIN32 || defined _WIN32 || defined WINCE)
00033 #define NTK_EXPORTS __declspec(dllexport)
00034 #else
00035 #define NTK_EXPORTS
00036 #endif
00037
00038 namespace cv {
00039 CV_EXPORTS void* fastMalloc(size_t);
00040 CV_EXPORTS void fastFree(void* ptr);
00041 }
00042
00043 namespace ntk
00044 {
00045
00046 template<typename _Tp> class CV_EXPORTS Ptr
00047 {
00048 public:
00049 struct DynamicCastTag {};
00050 public:
00051 Ptr();
00052 Ptr(_Tp* _obj);
00053 ~Ptr();
00054 Ptr(const Ptr& ptr);
00055 template <class U> Ptr(const Ptr<U>& ptr);
00056 template <class U> Ptr(const Ptr<U>& ptr, DynamicCastTag);
00057 Ptr& operator = (const Ptr& ptr);
00058 void addref();
00059 void release();
00060 void delete_obj();
00061 bool empty() const;
00062
00063 _Tp* operator -> () const;
00064 operator _Tp* () const;
00065
00066 private:
00067 template<typename _Up> friend class Ptr;
00068
00069 protected:
00070 _Tp* obj;
00071 int* refcount;
00072 };
00073
00074 template<typename _Tp> inline Ptr<_Tp>::Ptr() : obj(0), refcount(0) {}
00075 template<typename _Tp> inline Ptr<_Tp>::Ptr(_Tp* _obj) : obj(_obj)
00076 {
00077 if(obj)
00078 {
00079 refcount = (int*)cv::fastMalloc(sizeof(*refcount));
00080 *refcount = 1;
00081 }
00082 else
00083 refcount = 0;
00084 }
00085
00086 template<typename _Tp> inline void Ptr<_Tp>::addref()
00087 { if( refcount ) CV_XADD(refcount, 1); }
00088
00089 template<typename _Tp> inline void Ptr<_Tp>::release()
00090 {
00091 if( refcount && CV_XADD(refcount, -1) == 1 )
00092 {
00093 delete_obj();
00094 cv::fastFree(refcount);
00095 }
00096 refcount = 0;
00097 obj = 0;
00098 }
00099
00100 template<typename _Tp> inline void Ptr<_Tp>::delete_obj()
00101 {
00102 if( obj ) delete obj;
00103 }
00104
00105 template<typename _Tp> inline Ptr<_Tp>::~Ptr() { release(); }
00106
00107 template<typename _Tp> inline Ptr<_Tp>::Ptr(const Ptr<_Tp>& ptr)
00108 {
00109 obj = ptr.obj;
00110 refcount = ptr.refcount;
00111 addref();
00112 }
00113
00114 template<typename _Tp>
00115 template<typename _Up>
00116 inline Ptr<_Tp>::Ptr(const Ptr<_Up>& ptr)
00117 {
00118 obj = ptr.obj;
00119 refcount = ptr.refcount;
00120 addref();
00121 }
00122
00123 template<typename _Tp>
00124 template <class _Up>
00125 inline Ptr<_Tp>::Ptr(const Ptr<_Up>& ptr, DynamicCastTag)
00126 {
00127 obj = dynamic_cast<_Tp*>(ptr.obj);
00128 if (obj)
00129 {
00130 refcount = ptr.refcount;
00131 addref();
00132 }
00133 else
00134 {
00135 refcount = 0;
00136 }
00137 }
00138
00139 template<typename _Tp> inline Ptr<_Tp>& Ptr<_Tp>::operator = (const Ptr<_Tp>& ptr)
00140 {
00141 int* _refcount = ptr.refcount;
00142 if( _refcount )
00143 CV_XADD(_refcount, 1);
00144 release();
00145 obj = ptr.obj;
00146 refcount = _refcount;
00147 return *this;
00148 }
00149
00150 template<typename _Tp> inline _Tp* Ptr<_Tp>::operator -> () const { return obj; }
00151
00152 template<typename _Tp> inline Ptr<_Tp>::operator _Tp* () const { return obj; }
00153
00154 template<typename _Tp> inline bool Ptr<_Tp>::empty() const { return obj == 0; }
00155
00156 template <typename _Tp, typename _Up>
00157 inline Ptr<_Tp> dynamic_Ptr_cast(const Ptr<_Up>& ptr)
00158 { return Ptr<_Tp>(ptr, typename Ptr<_Tp>::DynamicCastTag()); }
00159
00160 }
00161
00162 namespace ntk
00163 {
00164 template <class T>
00165 ntk::Ptr<T> toPtr(T* ptr) { return ntk::Ptr<T>(ptr); }
00166 }
00167
00168 #define stl_bounds(s) s.begin(), s.end()
00169
00170 #define foreach_idx(Idxname, Vector) \
00171 for (size_t Idxname = 0; Idxname < (size_t)Vector.size(); ++Idxname)
00172
00173 #define foreach_uidx(Idxname, Vector) \
00174 for (unsigned Idxname = 0; Idxname < (unsigned)Vector.size(); ++Idxname)
00175
00176 #define foreach_it(Itname, Instance, Type) \
00177 for (Type::iterator Itname = Instance.begin(); Itname != Instance.end(); ++Itname)
00178
00179 #define foreach_const_it(Itname, Instance, Type) \
00180 for (Type::const_iterator Itname = Instance.begin(); Itname != Instance.end(); ++Itname)
00181
00182 #define cat2(a, b) a,b
00183 #define cat3(a, b, c) a,b,c
00184 #define cat4(a,b,c,d) a,b,c,d
00185
00186 #define ntk_throw_exception(msg) \
00187 { \
00188 std::string s (PRETTY_FUNCTION); s = (s + ": ") + msg; \
00189 throw ntk::exception(s); \
00190 }
00191
00192 #define ntk_throw_exception_if(cond, msg) \
00193 { \
00194 if (cond) \
00195 { \
00196 std::string s (PRETTY_FUNCTION); s = (s + ": ") + msg; \
00197 throw ntk::exception(s); \
00198 } \
00199 }
00200
00201 namespace ntk
00202 {
00203
00204 class exception : public std::exception
00205 {
00206 public:
00207 exception(std::string& what) throw() : m_what(what)
00208 {}
00209
00210 virtual ~exception() throw() {}
00211
00212 virtual const char* what() const throw()
00213 { return m_what.c_str(); }
00214
00215 private:
00216 std::string m_what;
00217 };
00218
00219 }
00220
00221 #endif