00001
00002
00003 #ifndef __ARC_UTILS_H__
00004 #define __ARC_UTILS_H__
00005
00006 #include <cstdlib>
00007
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
00025
00026
00027
00028 void EnvLockAcquire(void);
00029 void EnvLockRelease(void);
00033 void EnvLockWrap(bool all = false);
00036 void EnvLockUnwrap(bool all = false);
00038 void EnvLockUnwrapComplete(void);
00039
00040 class EnvLockWrapper {
00041 private:
00042 bool all_;
00043 public:
00044 EnvLockWrapper(bool all = false):all_(all) { EnvLockWrap(all_); };
00045 ~EnvLockWrapper(void) { EnvLockUnwrap(all_); };
00046 };
00047
00049 std::string StrError(int errnum = errno);
00050
00052
00056 template<typename T>
00057 class AutoPointer {
00058 private:
00059 T *object;
00060 void operator=(const AutoPointer<T>&) {}
00061 void operator=(T*) {}
00062 AutoPointer(const AutoPointer&)
00063 : object(NULL) {}
00064 public:
00066 AutoPointer(void)
00067 : object(NULL) {}
00069 AutoPointer(T *o)
00070 : object(o) {}
00072 ~AutoPointer(void) {
00073 if (object)
00074 delete object;
00075 }
00077 T& operator*(void) const {
00078 return *object;
00079 }
00081 T* operator->(void) const {
00082 return object;
00083 }
00085 operator bool(void) const {
00086 return (object != NULL);
00087 }
00089 bool operator!(void) const {
00090 return (object == NULL);
00091 }
00093 T* Ptr(void) const {
00094 return object;
00095 }
00097 T* Release(void) {
00098 T* tmp = object;
00099 object = NULL;
00100 return tmp;
00101 }
00102 };
00103
00105
00113 template<typename T>
00114 class CountedPointer {
00115 private:
00116 template<typename P>
00117 class Base {
00118 private:
00119 Base(Base<P>&) {}
00120 public:
00121 int cnt;
00122 P *ptr;
00123 bool released;
00124 Base(P *p)
00125 : cnt(0),
00126 ptr(p),
00127 released(false) {
00128 add();
00129 }
00130 ~Base(void) {
00131 if (ptr && !released)
00132 delete ptr;
00133 }
00134 Base<P>* add(void) {
00135 ++cnt;
00136 return this;
00137 }
00138 bool rem(void) {
00139 if (--cnt == 0) {
00140 if(!released) delete this;
00141 return true;
00142 }
00143 return false;
00144 }
00145 };
00146 Base<T> *object;
00147 public:
00148 CountedPointer(T *p)
00149 : object(new Base<T>(p)) {}
00150 CountedPointer(const CountedPointer<T>& p)
00151 : object(p.object->add()) {}
00152 ~CountedPointer(void) {
00153 object->rem();
00154 }
00155 CountedPointer<T>& operator=(T *p) {
00156 if (p != object->ptr) {
00157 object->rem();
00158 object = new Base<T>(p);
00159 }
00160 return *this;
00161 }
00162 CountedPointer<T>& operator=(const CountedPointer<T>& p) {
00163 if (p.object->ptr != object->ptr) {
00164 object->rem();
00165 object = p.object->add();
00166 }
00167 return *this;
00168 }
00170 T& operator*(void) const {
00171 return *(object->ptr);
00172 }
00174 T* operator->(void) const {
00175 return (object->ptr);
00176 }
00178 operator bool(void) const {
00179 return ((object->ptr) != NULL);
00180 }
00182 bool operator!(void) const {
00183 return ((object->ptr) == NULL);
00184 }
00186 bool operator==(const CountedPointer& p) const {
00187 return ((object->ptr) == (p.object->ptr));
00188 }
00190 bool operator!=(const CountedPointer& p) const {
00191 return ((object->ptr) != (p.object->ptr));
00192 }
00194 bool operator<(const CountedPointer& p) const {
00195 return ((object->ptr) < (p.object->ptr));
00196 }
00198 T* Ptr(void) const {
00199 return (object->ptr);
00200 }
00202 T* Release(void) {
00203 T* tmp = object->ptr;
00204 object->released = true;
00205 return tmp;
00206 }
00207 };
00208
00209 bool PersistentLibraryInit(const std::string& name);
00210
00211 }
00212
00213 # endif // __ARC_UTILS_H__