00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00053 #ifndef __PCOLLECTIONH__
00054 #define __PCOLLECTIONH__
00055
00056 #include <map>
00057 #include <string>
00058 #include <list>
00059 #include "bundled.h"
00060
00061 namespace pandore {
00062
00063
00064
00065
00066 #define SETVALUE(name, type, val) SetValue((name), (val))
00067 #define GETVALUE(name, type) GetValue((name), (type*)0)
00068
00069 #define SETARRAY(name, type, val, size) SetArray((name), (val), (size))
00070 #define GETARRAY(name, type) GetArray((name), (type*)0)
00071 #define GETARRAYSIZE(name, type) GetArraySize((name), (type*)0)
00072 #define GETNARRAYS(name, type, number, minsize_out) GetNArrays((name), (number), (minsize_out), (type*)0)
00073
00074 #define SETPOBJECT(name, type, val) SetPobject((name), (val))
00075 #define GETPOBJECT(name, type) GetPobject((name), (type*)0)
00076
00077 #define SETPARRAY(name, type, val, size) SetPArray((name), (val), (size))
00078 #define GETPARRAY(name, type) GetPArray((name), (type*)0)
00079 #define GETPARRAYSIZE(name, type) GetPArraySize((name), (type*)0)
00080
00081 class Collection;
00082
00083
00084
00085
00086
00087
00088
00089 template<>
00090 struct TypeName< Collection > {
00091
00092
00093
00094
00095 static std::string Name() { return "Collection"; }
00096 };
00097
00110 class Collection : public Pobject {
00111 public :
00112
00113 ~Collection() { Delete(); }
00114
00119 Typobj Type() const { return Po_Collection; }
00120
00125 std::string Name() const { return TypeName< Collection >::Name(); }
00126
00131 Pobject* Clone() const;
00132
00138 Collection &operator=( const Collection &col ) {
00139 this->Delete();
00140 std::map< std::string, BundledObject * >::const_iterator i;
00141 for (i = col._objs.begin(); i != col._objs.end(); ++i) {
00142 this->Set(i->first, i->second->Clone());
00143 }
00144 return *this;
00145 }
00146
00151 void Erase( const std::string &name );
00152
00156 void Delete( );
00157
00164 void Rename( const std::string &oldname, const std::string &newname );
00165
00172 bool Exists( const std::string &name ) const;
00173
00183 Errc NbOf( const std::string &name, std::string &type_out, Long &number_out, Long &minsize_out ) const;
00184
00190 std::string GetType( const std::string &name ) const;
00191
00196 std::list< std::string > List() const;
00197
00204 Errc LoadAttributes( FILE * ) {
00205 return SUCCESS;
00206 }
00207
00213 Errc SaveAttributes( FILE *file ) const {
00214 Long attr = _objs.size();
00215 if (Fencode((void*)&attr, sizeof(attr), 1, file) < 1) {
00216 return FAILURE;
00217 }
00218 return SUCCESS;
00219 }
00220
00226 Errc LoadData( FILE *file );
00227
00233 Errc SaveData( FILE *file ) const;
00234
00244 Pobject* Mask( const Pobject * ) {
00245 return this;
00246 }
00247
00258 Pobject* UnMask( const Pobject * , const Pobject * ) {
00259 return this;
00260 }
00261
00267 void Set( const std::string &name, BundledObject *bo );
00268
00274 BundledObject *Get( const std::string &name ) const;
00275
00284 template< typename T >
00285 void SetValue( const std::string &name, const T &val ) {
00286 Set(name, new BundledValue< T >(val));
00287 }
00288
00298 template< typename T >
00299 T &GetValue( const std::string & name, const T * ) const {
00300 BundledValue<T>* bvp = dynamic_cast< BundledValue<T>* >(Get(name));
00301 if (!bvp) {
00302 std::cerr << "Error: cannot convert `" << name.c_str() << "' to `"
00303 << TypeName<T>::Name().c_str() << "'." << std::endl;
00304 Exit(FAILURE);
00305 }
00306 return bvp->Value();
00307 }
00308
00319 template< typename T >
00320 void SetArray( const std::string &name, T *val, Long size ) {
00321 Set(name, new BundledArray< T >(val, size));
00322 }
00323
00333 template< typename T >
00334 T* GetArray( const std::string &name, const T * ) const {
00335 BundledArray<T>* bap = dynamic_cast< BundledArray<T>* >(Get(name));
00336 if (!bap) {
00337 std::cerr << "Error: cannot convert `" << name.c_str() << "' to `Array:"
00338 << TypeName<T>::Name().c_str() << "'." << std::endl;
00339 Exit(FAILURE);
00340 }
00341 return bap->Array();
00342 }
00343
00353 template< typename T >
00354 Long GetArraySize( const std::string &name, const T * ) const {
00355 BundledArray<T>* bap = dynamic_cast< BundledArray<T>* >(Get(name));
00356 if (!bap) {
00357 std::cerr << "Error: cannot convert `" << name.c_str() << "' to `Array:"
00358 << TypeName<T>::Name().c_str() << "'." << std::endl;
00359 Exit(FAILURE);
00360 }
00361 return bap->NbrElements();
00362 }
00363
00372 template< typename T >
00373 void SetPobject( const std::string &name, T *val ) {
00374 Set(name, new BundledPobject(val));
00375 }
00376
00385 template< typename T >
00386 T* GetPobject( const std::string &name, const T * ) const {
00387 BundledPobject* bap = dynamic_cast< BundledPobject* >(Get(name));
00388 if (!bap) {
00389 std::cerr << "Error: cannot convert `" << name.c_str() << "' to `Pobject:"
00390 << TypeName<T>::Name().c_str() << "'." << std::endl;
00391 Exit(FAILURE);
00392 }
00393 return (T*)bap->Object();
00394 }
00395
00406 template< typename T >
00407 void SetPArray( const std::string &name, T *val, Long size ) {
00408 Set(name, new BundledPArray((Pobject**)val, size));
00409 }
00410
00421 template< typename T >
00422 T** GetPArray( const std::string &name, const T * ) const {
00423 BundledPArray* bap = dynamic_cast< BundledPArray* >(Get(name));
00424 if (!bap) {
00425 std::cerr << "Error: cannot convert `" << name.c_str() << "' to `PArray:"
00426 << TypeName<T>::Name().c_str() << "'." << std::endl;
00427 Exit(FAILURE);
00428 }
00429 return (T**)bap->PArray();
00430 }
00431
00441 template< typename T >
00442 Long GetPArraySize( const std::string &name, const T * ) const {
00443 BundledPArray* bap = dynamic_cast< BundledPArray* >(Get(name));
00444 if (!bap) {
00445 std::cerr << "Error: cannot convert `" << name.c_str() << "' to `PArray:"
00446 << TypeName<T>::Name().c_str() << "'." << std::endl;
00447 Exit(FAILURE);
00448 }
00449 return bap->NbrElements();
00450 }
00451
00462 template< typename T >
00463 T** GetNArrays( const std::string &name, Long n, Long &min_out, const T * ) const {
00464 char name_in[255];
00465 T **tmp = NULL;
00466 Long nin = MAXLONG;
00467 Long n_= MAXLONG;
00468
00469 tmp = new T*[n];
00470 for (int i = 0; i < n; ++i) {
00471 sprintf(name_in, "%s.%d", name.c_str(), i + 1);
00472 tmp[i] = this->GETARRAY(name_in, T);
00473 n_ = this->GETARRAYSIZE(name_in, T);
00474 if (n_ < nin) {
00475 nin = n_;
00476 }
00477 }
00478 min_out = nin;
00479 return tmp;
00480 }
00481
00482 private :
00483 std::map< std::string, BundledObject* > _objs;
00484 };
00485
00486 }
00487
00488 #endif // __PCOLLECTIONH__