00001 #ifndef __rmBaseArray_h
00002 #define __rmBaseArray_h
00003
00004 #include <map>
00005 #include "rmClientError.h"
00006 #include <stdio.h>
00007 #include <uuid/uuid.h>
00008 #include "rmClientTypes.h"
00009
00018 class IBaseAccessor
00019 {
00020 public:
00021
00022 virtual int getInteger32(RMAttribute aName, unsigned long aID, long & aValue) = 0;
00023 virtual int setInteger32(RMAttribute aName, unsigned long aID, long aValue) = 0;
00024
00025 virtual int getUnsigned(RMAttribute aName, unsigned long aID, unsigned long & aValue) = 0;
00026
00027 virtual int setUnsigned(RMAttribute aName, unsigned long aID, unsigned long aValue) = 0;
00028
00029 virtual int getUnsigned64(RMAttribute aName, unsigned long aID, u_int64_t & aValue) = 0;
00030
00031 virtual int setUnsigned64(RMAttribute aName, unsigned long aID, u_int64_t aValue) = 0;
00032
00033 virtual int getUUIDString(RMAttribute aName, unsigned long aID, char * aUUIDStr) = 0;
00034
00035 virtual int setUUIDString(RMAttribute aName, unsigned long aID, char * aUUIDStr) = 0;
00036
00037 virtual int getUUID(RMAttribute aName, unsigned long aID, uuid_t aUUID) = 0;
00038
00039 virtual int getString(RMAttribute aName, unsigned long aID, char * & aBuf) = 0;
00040
00041 virtual int setString(RMAttribute aName, unsigned long aID, char * aBuf) = 0;
00042 virtual int getBinary(RMAttribute aName, unsigned long aID, char * & aBuf, unsigned long & aLength) = 0;
00043
00044 virtual int addDefaultElement(unsigned long aID) = 0;
00045
00046 virtual int removeElement(unsigned long aID) = 0;
00047
00048 virtual unsigned long getNewId(void) = 0;
00049
00050 virtual int findExactId(unsigned long aID) = 0;
00051
00052 virtual int getNextId(unsigned long aID, unsigned long & aNextID) = 0;
00053
00054 virtual size_t getCount() const = 0;
00055 };
00056
00068 template<class T>
00069 class rmBaseArray : public IBaseAccessor
00070 {
00071 public:
00072 rmBaseArray() : mMaxId(1) {mPos = mElementArray.begin();}
00073 virtual ~rmBaseArray(){}
00074
00075 rmBaseArray(unsigned long aStartupID) : mMaxId(aStartupID), mPos(mElementArray.begin()){}
00076
00078 virtual size_t getCount() const {return mElementArray.size();}
00079
00081 virtual T * getElement(unsigned long aID);
00082
00084 virtual const T * getConstElement(unsigned long aID)
00085 {
00086 return getElement(aID);
00087 }
00088
00090 virtual int addElement(const T & aElement, unsigned long aID);
00091
00093 virtual int newElement(const T & aElement, unsigned long & aID);
00094
00096 virtual int addDefaultElement(unsigned long aID);
00097
00099 virtual int removeElement(unsigned long aID);
00100
00102 virtual int modifyElement(unsigned long aID, const T & aElement);
00103
00105 virtual unsigned long getNewId(void)
00106 {
00107 return mMaxId;
00108 }
00109
00111 virtual unsigned long getMaxId(void) const
00112 {
00113 return mMaxId;
00114 }
00115
00117 virtual int findId(unsigned long aID, map<unsigned long, T>::iterator * aPos = 0);
00118
00120 virtual int getNextId(unsigned long aID, unsigned long & aNextID);
00121
00123 virtual int init(){return RMCLIENT_SUCCESS;}
00124
00125 virtual bool elementIsReady(const T & aElement) const
00126 {
00127 return false;
00128 }
00129
00130 virtual int getInteger32(RMAttribute aName, unsigned long aID, long & aValue)
00131 {
00132 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00133 }
00134
00135 virtual int setInteger32(RMAttribute aName, unsigned long aID, long aValue)
00136 {
00137 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00138 }
00139
00140 virtual int getUnsigned(RMAttribute aName, unsigned long aID, unsigned long & aValue)
00141 {
00142 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00143 }
00144
00145 virtual int setUnsigned(RMAttribute aName, unsigned long aID, unsigned long aValue)
00146 {
00147 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00148 }
00149
00150 virtual int getUnsigned64(RMAttribute aName, unsigned long aID, u_int64_t & aValue)
00151 {
00152 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00153 }
00154
00155 virtual int setUnsigned64(RMAttribute aName, unsigned long aID, u_int64_t aValue)
00156 {
00157 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00158 }
00159
00160 virtual int getUUIDString(RMAttribute aName, unsigned long aID, char * aUUIDStr)
00161 {
00162 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00163 }
00164
00165 virtual int setUUIDString(RMAttribute aName, unsigned long aID, char * aUUIDStr)
00166 {
00167 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00168 }
00169
00170 virtual int getUUID(RMAttribute aName, unsigned long aID, uuid_t aUUID)
00171 {
00172 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00173 }
00174
00175 virtual int getString(RMAttribute aName, unsigned long aID, char * & aBuf)
00176 {
00177 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00178 }
00179
00180 virtual int setString(RMAttribute aName, unsigned long aID, char * aBuf)
00181 {
00182 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00183 }
00184
00185 virtual int getBinary(RMAttribute aName, unsigned long aID, char * & aBuf, unsigned long & aLength)
00186 {
00187 return RMCLIENT_ERROR_NOT_IMPLEMENTED;
00188 }
00189
00190 virtual int findExactId(unsigned long aID)
00191 {
00192 return findId(aID);
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 protected:
00202
00203
00207 map<unsigned long, T> mElementArray;
00208
00210 unsigned long mMaxId;
00211
00214 map<unsigned long, T>::iterator mPos;
00215 };
00216
00227 template<class T> T *
00228 rmBaseArray<T>::getElement(unsigned long aID)
00229 {
00230 int ret;
00231 map<unsigned long, T>::iterator pos;
00232
00233 ret = findId(aID, &pos);
00234 if(ret)
00235 return NULL;
00236 else
00237 return &(pos->second);
00238 }
00239
00258 template<class T> int
00259 rmBaseArray<T>::addElement(const T & aElement, unsigned long aID)
00260 {
00261 if (aID > mMaxId)
00262 return RMCLIENT_ERROR_BASE_ID;
00263
00264 if((mElementArray.insert(make_pair(aID, aElement))).second) {
00265
00266
00267 map<unsigned long, T>::iterator pos;
00268 do {
00269 mMaxId++;
00270
00271
00272 if(mMaxId == 0)
00273 mMaxId++;
00274
00275 pos = mElementArray.find(mMaxId);
00276 if(pos == mElementArray.end())
00277 break;
00278
00279
00280 } while (1);
00281
00282
00283 return RMCLIENT_SUCCESS;
00284 }
00285 else
00286 return RMCLIENT_ERROR_BASE_INSERT;
00287 }
00288
00305 template<class T> int
00306 rmBaseArray<T>::newElement(const T & aElement, unsigned long & aID)
00307 {
00308 unsigned long id;
00309 id = getNewId();
00310
00311 aID = id;
00312
00313 return addElement(aElement, id);
00314 }
00315
00316 template<class T> int
00317 rmBaseArray<T>::addDefaultElement(unsigned long aID)
00318 {
00319 T element;
00320
00321 memset(&element, 0, sizeof(T));
00322
00323 return addElement(element, aID);
00324 }
00325
00335 template<class T> int
00336 rmBaseArray<T>::removeElement(unsigned long aID)
00337 {
00338 int ret;
00339 map<unsigned long, T>::iterator pos;
00340
00341 ret = findId(aID, &pos);
00342 if(ret)
00343 return ret;
00344
00345 mElementArray.erase(pos);
00346 mPos = mElementArray.begin();
00347
00348 return RMCLIENT_SUCCESS;
00349 }
00350
00363 template<class T> int
00364 rmBaseArray<T>::modifyElement(unsigned long aID, const T & aElement)
00365 {
00366 int ret;
00367 map<unsigned long, T>::iterator pos;
00368
00369 ret = findId(aID, &pos);
00370 if(ret)
00371 return ret;
00372
00373 pos->second = aElement;
00374
00375 return RMCLIENT_SUCCESS;
00376 }
00377
00391 template<class T> int
00392 rmBaseArray<T>::findId(unsigned long aID, map<unsigned long, T>::iterator * aPos = 0)
00393 {
00394 if(mPos != mElementArray.end() && mPos->first == aID) {
00395 if(aPos)
00396 *aPos = mPos;
00397 return RMCLIENT_SUCCESS;
00398 }
00399
00400 map<unsigned long, T>::iterator pos;
00401
00402 pos = mElementArray.find(aID);
00403
00404 if(pos != mElementArray.end()) {
00405 mPos = pos;
00406 if(aPos)
00407 *aPos = pos;
00408 return RMCLIENT_SUCCESS;
00409 }
00410
00411 return RMCLIENT_ERROR_BASE_ID;
00412 }
00413
00447 template<class T> int
00448 rmBaseArray<T>::getNextId(unsigned long aID, unsigned long & aNextID)
00449 {
00450 aNextID = 0;
00451
00452 map<unsigned long, T>::iterator pos;
00453
00454 pos = mElementArray.upper_bound(aID);
00455
00456 mPos = pos;
00457
00458 if(pos == mElementArray.end())
00459 return RMCLIENT_ERROR_BASE_ID;
00460
00461 aNextID = pos->first;
00462
00463 return RMCLIENT_SUCCESS;
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 #endif // __rmBaseArray_h