collection.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 3 -*-
00002  *
00003  * Copyright (c) 2013, GREYC.
00004  * All rights reserved
00005  *
00006  * You may use this file under the terms of the BSD license as follows:
00007  *
00008  * "Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are
00010  * met:
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above copyright
00014  *     notice, this list of conditions and the following disclaimer in
00015  *     the documentation and/or other materials provided with the
00016  *     distribution.
00017  *   * Neither the name of the GREYC, nor the name of its
00018  *     contributors may be used to endorse or promote products
00019  *     derived from this software without specific prior written
00020  *     permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00033  *
00034  *
00035  * For more information, refer to:
00036  * https://clouard.users.greyc.fr/Pandore
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    // Because VisualC++ cannot use template as: method<Type>(...)
00064    // we use the dummy 0 with the convenient casting.
00065    // So: change the prototype to: method(..., (type)0);
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    /* @brief A trait that returns the name of the object Collection.
00085     *
00086     * TypeName is a trait that returns the name
00087     * of the object Collection.
00088     */
00089    template<>
00090    struct TypeName< Collection > {
00091          /*
00092           * Returns the name of the type.
00093           * @return     the string with the name.
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 * /*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 * /*mask*/ ) {
00245             return this;
00246          }
00247 
00258          Pobject* UnMask( const Pobject * /*mask*/, const Pobject * /*reference*/ ) {
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 * /*val*/ ) const {
00300             BundledValue<T>* bvp = dynamic_cast< BundledValue<T>* >(Get(name));
00301             if (!bvp) { // Panic
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 * /*val*/ ) const {
00335             BundledArray<T>* bap = dynamic_cast< BundledArray<T>* >(Get(name));
00336             if (!bap) { // Panic
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 * /*val*/ ) const {
00355             BundledArray<T>* bap = dynamic_cast< BundledArray<T>* >(Get(name));
00356             if (!bap) { // Panic
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 * /*val*/ ) const {
00387             BundledPobject* bap = dynamic_cast< BundledPobject* >(Get(name));
00388             if (!bap) { // Panic
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 * /*val*/ ) const {
00423             BundledPArray* bap = dynamic_cast< BundledPArray* >(Get(name));
00424             if (!bap) { // Panic
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 * /*val*/ ) const {
00443             BundledPArray* bap = dynamic_cast< BundledPArray* >(Get(name));
00444             if (!bap) { // Panic
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 * /*val*/ ) 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 } //End of pandore:: namespace
00487 
00488 #endif // __PCOLLECTIONH__

The Pantheon project
Image Team GREYC Laboratory
UMR CNRS 6072 - ENSICAEN - University of Caen, France
This page was last modified on