PDataContainer.h

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 
00017 #include <map>
00018 #include "PData.h"
00019 #include "PDataOperator.h"
00020 #include "PDataArithmetic.h"
00021 
00022 #ifndef PDATA_CONTAINER
00023 #define PDATA_CONTAINER
00024 
00025 using namespace std;
00026 
00027 namespace Fluxus
00028 {
00029 
00043 class PDataContainer
00044 {
00045 public:
00046     PDataContainer();
00047     PDataContainer(const PDataContainer &other);
00048     virtual ~PDataContainer();
00049     virtual PDataContainer *Clone() const=0;
00050 
00052     void Clear();
00053     
00056     void AddData(const string &name, PData* pd);
00057     
00060     void CopyData(const string &name, string newname);
00061     
00065     template<class T> vector<T>* GetDataVec(const string &name);
00066     
00068     void RemoveDataVec(const string &name);
00069     
00072     bool GetDataInfo(const string &name, char &type, unsigned int &size) const;
00073     
00076     template<class T> void SetData(const string &name, unsigned int index, T s);
00077     
00080     template<class T> T GetData(const string &name, unsigned int index) const;
00081         
00083     template<class T> PData *DataOp(const string &op, const string &name, T operand);
00084     
00086     PData* GetDataRaw(const string &name);
00087 
00089     const PData* GetDataRawConst(const string &name) const;
00090     
00092     void SetDataRaw(const string &name, PData* pd);
00093     
00096     template <class S, class T> PData *FindOperate(const string &name, TypedPData<S> *a, T b);
00097     
00099     void Resize(unsigned int size);
00100     
00102     unsigned int Size() const;
00103 
00105     void GetDataNames(vector<string> &names) const;
00106 
00107 protected:
00108 
00110     virtual void PDataDirty()=0;
00111     
00114     mutable map<string,PData*> m_PData;
00115 
00116 };
00117 
00118 template<class T> 
00119 void PDataContainer::SetData(const string &name, unsigned int index, T s)   
00120 {
00121     static_cast<TypedPData<T>*>(m_PData[name])->m_Data[index]=s;
00122 }
00123 
00125 template<class T> 
00126 T PDataContainer::GetData(const string &name, unsigned int index) const
00127 {
00128     return static_cast<TypedPData<T>*>(m_PData[name])->m_Data[index];
00129 }
00130 
00131 template<class T>
00132 vector<T>* PDataContainer::GetDataVec(const string &name)
00133 {
00134     map<string,PData*>::iterator i=m_PData.find(name);
00135     if (i==m_PData.end())
00136     {
00137         Trace::Stream<<"Primitive::GetPDataVec: pdata: "<<name<<" doesn't exists"<<endl;
00138         return NULL;
00139     }
00140     
00141     TypedPData<T> *ptr=dynamic_cast<TypedPData<T> *>(i->second);
00142     if (!ptr) 
00143     {
00144         Trace::Stream<<"Primitive::GetPDataVec: pdata: "<<name<<" is not of type: "<<typeid(TypedPData<T>).name()<<endl;
00145         return NULL;
00146     }
00147     
00148     return &ptr->m_Data;
00149 }
00150 
00151 template<class T>
00152 PData *PDataContainer::DataOp(const string &op, const string &name, T operand)
00153 {
00154     map<string,PData*>::iterator i=m_PData.find(name);
00155     if (i==m_PData.end())
00156     {
00157         Trace::Stream<<"Primitive::DataOp: pdata: "<<name<<" doesn't exists"<<endl;
00158         return NULL;
00159     }
00160     
00161     TypedPData<dVector> *data = dynamic_cast<TypedPData<dVector>*>(i->second);  
00162     if (data) return FindOperate<dVector,T>(op, data, operand);
00163     else
00164     {
00165         TypedPData<dColour> *data = dynamic_cast<TypedPData<dColour>*>(i->second);
00166         if (data) return FindOperate<dColour, T>(op, data, operand);
00167         else 
00168         {
00169             TypedPData<float> *data = dynamic_cast<TypedPData<float>*>(i->second);
00170             if (data) return FindOperate<float, T>(op, data, operand);
00171             else 
00172             {
00173                 TypedPData<dMatrix> *data = dynamic_cast<TypedPData<dMatrix>*>(i->second);
00174                 if (data) return FindOperate<dMatrix, T>(op, data, operand);
00175             }
00176         }
00177     }
00178     
00179     return NULL;
00180 }
00181 
00182 template <class S, class T>
00183 PData *PDataContainer::FindOperate(const string &name, TypedPData<S> *a, T b)
00184 {
00185     if (name=="+") return AddOperator::Operate<S,T>(a,b);
00186     else if (name=="*") return MultOperator::Operate<S,T>(a,b);
00187     else if (name=="closest") return ClosestOperator::Operate<S,T>(a,b);
00188     else if (name=="sin") return SineOperator::Operate<S,T>(a,b);
00189     else if (name=="cos") return CosineOperator::Operate<S,T>(a,b);
00190     
00191     Trace::Stream<<"operator "<<name<<" not found"<<endl;
00192     return NULL;
00193 }
00194 
00195 };
00196 
00197 #endif

Generated on Wed Sep 17 21:16:30 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1