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);
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);
00081     
00083     template<class T> PData *DataOp(const string &op, const string &name, T operand);
00084     
00086     PData* GetDataRaw(const string &name);
00087     
00089     void SetDataRaw(const string &name, PData* pd);
00090     
00093     template <class S, class T> PData *FindOperate(const string &name, TypedPData<S> *a, T b);
00094     
00096     void Resize(unsigned int size);
00097     
00099     unsigned int Size();
00100 
00101 protected:
00102 
00104     virtual void PDataDirty()=0;
00105     
00107     map<string,PData*> m_PData;
00108 
00109 };
00110 
00111 template<class T> 
00112 void PDataContainer::SetData(const string &name, unsigned int index, T s)   
00113 {
00114     dynamic_cast<TypedPData<T>*>(m_PData[name])->m_Data[index]=s;
00115 }
00116 
00117 template<class T> 
00118 T PDataContainer::GetData(const string &name, unsigned int index)
00119 {
00120     return dynamic_cast<TypedPData<T>*>(m_PData[name])->m_Data[index];
00121 }
00122 
00123 template<class T>
00124 vector<T>* PDataContainer::GetDataVec(const string &name)
00125 {
00126     map<string,PData*>::iterator i=m_PData.find(name);
00127     if (i==m_PData.end())
00128     {
00129         Trace::Stream<<"Primitive::GetPDataVec: pdata: "<<name<<" doesn't exists"<<endl;
00130         return NULL;
00131     }
00132     
00133     TypedPData<T> *ptr=dynamic_cast<TypedPData<T> *>(i->second);
00134     if (!ptr) 
00135     {
00136         Trace::Stream<<"Primitive::GetPDataVec: pdata: "<<name<<" is not of type: "<<typeid(TypedPData<T>).name()<<endl;
00137         return NULL;
00138     }
00139     
00140     return &ptr->m_Data;
00141 }
00142 
00143 template<class T>
00144 PData *PDataContainer::DataOp(const string &op, const string &name, T operand)
00145 {
00146     map<string,PData*>::iterator i=m_PData.find(name);
00147     if (i==m_PData.end())
00148     {
00149         Trace::Stream<<"Primitive::DataOp: pdata: "<<name<<" doesn't exists"<<endl;
00150         return NULL;
00151     }
00152     
00153     TypedPData<dVector> *data = dynamic_cast<TypedPData<dVector>*>(i->second);  
00154     if (data) return FindOperate<dVector,T>(op, data, operand);
00155     else
00156     {
00157         TypedPData<dColour> *data = dynamic_cast<TypedPData<dColour>*>(i->second);
00158         if (data) return FindOperate<dColour, T>(op, data, operand);
00159         else 
00160         {
00161             TypedPData<float> *data = dynamic_cast<TypedPData<float>*>(i->second);
00162             if (data) return FindOperate<float, T>(op, data, operand);
00163             else 
00164             {
00165                 TypedPData<dMatrix> *data = dynamic_cast<TypedPData<dMatrix>*>(i->second);
00166                 if (data) return FindOperate<dMatrix, T>(op, data, operand);
00167             }
00168         }
00169     }
00170     
00171     return NULL;
00172 }
00173 
00174 template <class S, class T>
00175 PData *PDataContainer::FindOperate(const string &name, TypedPData<S> *a, T b)
00176 {
00177     if (name=="+") return AddOperator::Operate<S,T>(a,b);
00178     else if (name=="*") return MultOperator::Operate<S,T>(a,b);
00179     else if (name=="closest") return ClosestOperator::Operate<S,T>(a,b);
00180     else if (name=="sin") return SineOperator::Operate<S,T>(a,b);
00181     else if (name=="cos") return CosineOperator::Operate<S,T>(a,b);
00182     
00183     Trace::Stream<<"operator "<<name<<" not found"<<endl;
00184     return NULL;
00185 }
00186 
00187 };
00188 
00189 #endif

Generated on Mon Feb 11 06:54:25 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1