arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

GenDataObj.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //
00003 //   Joe Kniss
00004 //     7-10-03
00005 //                   ________    ____   ___ 
00006 //                  |        \  /    | /  /
00007 //                  +---+     \/     |/  /
00008 //                  +--+|  |\    /|     < 
00009 //                  |  ||  | \  / |  |\  \ 
00010 //                  |      |  \/  |  | \  \ 
00011 //                   \_____|      |__|  \__\
00012 //                       Copyright  2003 
00013 //                      Joe Michael Kniss
00014 //                   <<< jmk@cs.utah.edu >>>
00015 //               "All Your Base are Belong to Us"
00016 //-------------------------------------------------------------------------
00017 
00018 /// GenDataObj.h
00019 ///  Simainlib
00020 /// A generic data object
00021 
00022 #ifndef __SIMIAN_GENERIC_DATA_OBJ_DOT_H
00023 #define __SIMIAN_GENERIC_DATA_OBJ_DOT_H
00024 
00025 #include <core/DataObj.h>
00026 #include <smartptr.h>
00027 #include <vector>
00028 
00029 //////////////////////////////////////////////////////////////////
00030 /// a base class for 2 kinds of generic data objects.
00031 /// this is just the interface, see the classes below too.
00032 ///  fills data vector with "nObs" Null objects, just place holders
00033 
00034 template<class T, class P>
00035 class GenDataObj_base : public DataObj
00036 {
00037 public:
00038    typedef T                     DATA_TYPE;
00039    typedef P                     PTR_TYPE;
00040    typedef std::vector<PTR_TYPE> VEC_TYPE;
00041 
00042    /// child classes are responsible for
00043    /// deleting their data
00044    virtual ~GenDataObj_base() 
00045    {
00046    } 
00047    
00048    ////////////////////////////////////////
00049    /// Public DATA
00050    ////////////////////////////////////////
00051 
00052    /// a vector of "ptrs" to the data
00053    VEC_TYPE dataVec;
00054    
00055    ////////////////////////////////////////
00056    /// Safe interface, use it if you like
00057    ////////////////////////////////////////
00058 
00059    ////////////////////////////////////////
00060    /// restate the size
00061    int size() { return int(dataVec.size()); }
00062 
00063    ////////////////////////////////////////
00064    /// safe way to get the data, zeroth by default
00065    PTR_TYPE getData(int i=0)
00066    {
00067       if(i >= size())
00068          return PTR_TYPE(0);
00069       return dataVec[i];
00070    }
00071 
00072    ////////////////////////////////////////
00073    /// safe way to set the data
00074    void setData(int i, PTR_TYPE d)
00075    {
00076       if( i > size() )
00077          for(int k = size()-1; k<i; ++k)
00078             dataVec.push_back(PTR_TYPE(0));
00079       dataVec[i] = d;
00080    }
00081 
00082    ////////////////////////////////////////
00083    /// set zeroth entry for convenience
00084    void setData(PTR_TYPE d)
00085    {
00086       if(dataVec.size() == 0)
00087          dataVec.push_back(d);
00088       else
00089          dataVec[0] = d;
00090    }
00091 
00092    ////////////////////////////////////////
00093    /// delete an entry
00094    void delData(int i=0)
00095    {
00096       if(i >= size()) return;
00097       _delData(i);
00098    }
00099 
00100    ////////////////////////////////////////
00101    /// pack vector
00102    void pack()
00103    {
00104       int laste = 0;
00105       for(int i=0; i < size(); ++i)
00106       {
00107          if(!dataVec[i]) continue;
00108          if(laste != i)
00109             dataVec[laste] = dataVec[i];
00110          ++laste;
00111       }
00112       for(int i=laste; i< size(); ++i)
00113          dataVec.pop_back();
00114    }
00115 
00116 protected:
00117    GenDataObj_base(int nObjs, const char *name=0)
00118       : DataObj()
00119    { 
00120       if(name)
00121          SimBase::setName(name); 
00122       for(int i=0; i<nObjs; ++i)
00123          dataVec.push_back((PTR_TYPE)(0));
00124    }
00125    virtual void _delData(int i) = 0;
00126 };
00127 
00128 
00129 //////////////////////////////////////////////////////////////////
00130 /// this is a generic data object for all types that are "Counted"
00131 template<class T>
00132 class GenDataObj : public GenDataObj_base< T , gutz::SmartPtr<T> > {
00133 public:
00134    typedef GenDataObj_base<T, gutz::SmartPtr<T> > BASE_TYPE;
00135    typedef typename BASE_TYPE::DATA_TYPE          DATA_TYPE;
00136    typedef typename BASE_TYPE::PTR_TYPE           PTR_TYPE;
00137    typedef typename BASE_TYPE::VEC_TYPE           VEC_TYPE;
00138 
00139    GenDataObj(int nObjs=0, const char *name=0)
00140       : BASE_TYPE(nObjs,name)
00141    { 
00142    }
00143 
00144    /// smartptrs nuke themselves
00145    virtual ~GenDataObj() {} 
00146 
00147 
00148 protected:
00149    virtual void _delData(int i)
00150    {
00151       dataVec[i] = PTR_TYPE(0);
00152    }
00153 };
00154 
00155 //////////////////////////////////////////////////////////////////
00156 /// this is a generic data object for all types that are 
00157 /// NOT "Counted", you have a choice, let the data be deleted
00158 /// with this class, or nuke it yourself
00159 template<class T>
00160 class GenDataObjNSP : public GenDataObj_base< T , T*> {
00161 public:
00162    typedef GenDataObj_base< T , T*>          BASE_TYPE;
00163    typedef typename BASE_TYPE::DATA_TYPE     DATA_TYPE;
00164    typedef typename BASE_TYPE::PTR_TYPE      PTR_TYPE;
00165    typedef typename BASE_TYPE::VEC_TYPE      VEC_TYPE;
00166 
00167    GenDataObjNSP(int nObjs=0, const char *name=0, bool del=true)
00168       : BASE_TYPE(nObjs,name), _del(del)
00169    {
00170       if(_del)
00171          for(int i=size()-1; i >=0; --i)
00172          {
00173             _delData(i);
00174             dataVec.pop_back();
00175          }
00176    }
00177 
00178    /// real pointers, yuky
00179    virtual ~GenDataObjNSP() 
00180    {
00181    }
00182 
00183 protected:
00184    virtual void _delData(int i)
00185    {
00186       if(_del && dataVec[i]) delete dataVec[i];
00187       dataVec[i] = (PTR_TYPE)(0);
00188    }
00189    bool _del;
00190 
00191 };
00192 
00193 #endif
00194 

Send questions, comments, and bug reports to:
jmk