Back to index

nordugrid-arc-nox  1.1.0~rc6
Utils.h
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifndef __ARC_UTILS_H__
00004 #define __ARC_UTILS_H__
00005 
00006 #include <cstdlib>
00007 // NOTE: On Solaris errno is not working properly if cerrno is included first
00008 #include <cerrno>
00009 #include <string>
00010 
00011 namespace Arc {
00012 
00014   std::string GetEnv(const std::string& var);
00015 
00017   std::string GetEnv(const std::string& var, bool &found);
00018 
00020   bool SetEnv(const std::string& var, const std::string& value, bool overwrite = true);
00021 
00023   void UnsetEnv(const std::string& var);
00024 
00026   std::string StrError(int errnum = errno);
00027 
00029 
00033   template<typename T>
00034   class AutoPointer {
00035   private:
00036     T *object;
00037     void operator=(const AutoPointer<T>&) {}
00038     void operator=(T*) {}
00039     AutoPointer(const AutoPointer&)
00040       : object(NULL) {}
00041   public:
00043     AutoPointer(void)
00044       : object(NULL) {}
00046     AutoPointer(T *o)
00047       : object(o) {}
00049     ~AutoPointer(void) {
00050       if (object)
00051         delete object;
00052     }
00054     T& operator*(void) const {
00055       return *object;
00056     }
00058     T* operator->(void) const {
00059       return object;
00060     }
00062     operator bool(void) const {
00063       return (object != NULL);
00064     }
00066     bool operator!(void) const {
00067       return (object == NULL);
00068     }
00070     operator T*(void) const {
00071       return object;
00072     }
00073   };
00074 
00076 
00084   template<typename T>
00085   class CountedPointer {
00086   private:
00087     template<typename P>
00088     class Base {
00089     private:
00090       Base(Base<P>&) {}
00091     public:
00092       int cnt;
00093       P *ptr;
00094       Base(P *p)
00095         : cnt(0),
00096           ptr(p) {
00097         add();
00098       }
00099       ~Base(void) {
00100         if (ptr)
00101           delete ptr;
00102       }
00103       Base<P>* add(void) {
00104         ++cnt;
00105         return this;
00106       }
00107       bool rem(void) {
00108         if (--cnt == 0) {
00109           delete this;
00110           return true;
00111         }
00112         return false;
00113       }
00114     };
00115     Base<T> *object;
00116   public:
00117     CountedPointer(T *p)
00118       : object(new Base<T>(p)) {}
00119     CountedPointer(CountedPointer<T>& p)
00120       : object(p.object->add()) {}
00121     ~CountedPointer(void) {
00122       object->rem();
00123     }
00124     CountedPointer& operator=(T *p) {
00125       if (p != object->ptr) {
00126         object->rem();
00127         object = new Base<T>(p);
00128       }
00129       return *this;
00130     }
00131     CountedPointer& operator=(CountedPointer& p) {
00132       if (p.object->ptr != object->ptr) {
00133         object->rem();
00134         object = p.object->add();
00135       }
00136       return *this;
00137     }
00139     T& operator*(void) const {
00140       return *(object->ptr);
00141     }
00143     T* operator->(void) const {
00144       return (object->ptr);
00145     }
00147     operator bool(void) const {
00148       return ((object->ptr) != NULL);
00149     }
00151     bool operator!(void) const {
00152       return ((object->ptr) == NULL);
00153     }
00155     operator T*(void) const {
00156       return (object->ptr);
00157     }
00158   };
00159 
00160   bool PersistentLibraryInit(const std::string& name);
00161 
00162 } // namespace Arc
00163 
00164 # endif // __ARC_UTILS_H__