PrimitiveFunction.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 <string>
00018 #include <map>
00019 #include "PData.h"
00020 #include "Primitive.h"
00021 #include "SceneGraph.h"
00022 
00023 #ifndef N_PRIMITIVE_FUNCTION
00024 #define N_PRIMITIVE_FUNCTION
00025 
00026 using namespace std;
00027 
00028 namespace Fluxus
00029 {
00030 
00032 class PrimitiveFunction 
00033 {
00034 public:
00035     PrimitiveFunction();
00036     virtual ~PrimitiveFunction();
00037 
00039     class Arg
00040     {
00041     public:
00042         Arg() {}
00043         virtual ~Arg() {}
00044     };
00045 
00047     template<class T>
00048     class TypedArg : public Arg
00049     {
00050     public:
00051         TypedArg(T data) : m_Data(data) {}
00052         T m_Data;
00053     };
00054 
00055     template<class T>
00056     void SetArg(const string &name, const T &arg);
00057     
00058     void ClearArgs();
00059     
00063     
00065     virtual void Run(Primitive &prim, const SceneGraph &world)=0;
00066     
00068     template<class T>
00069     TypedArg<T> GetResult() {}
00071     
00072 protected:
00073 
00077     
00079     template<class T>
00080     const T &GetArg(const string &name, const T &def);
00081 
00083     template<class T>
00084     bool ArgExists(const string &name);
00086 
00087 private:
00088     map<string, Arg *> m_Args;
00089 };
00090 
00091 template<class T>
00092 const T &PrimitiveFunction::GetArg(const string &name, const T &def)
00093 {
00094     // look for the argument
00095     map<string,Arg*>::iterator i=m_Args.find(name);
00096     if (i==m_Args.end())
00097     {
00098         return def;
00099     }
00100     
00101     // check it's the right type
00102     TypedArg<T> *ret = dynamic_cast<TypedArg<T>*>(i->second);
00103     if (ret)
00104     {
00105         return ret->m_Data;
00106     }
00107     
00108     return def;
00109 }
00110 
00111 template<class T>
00112 bool PrimitiveFunction::ArgExists(const string &name)
00113 {
00114     // look for the argument
00115     map<string,Arg*>::iterator i=m_Args.find(name);
00116     if (i==m_Args.end())
00117     {
00118         return false;
00119     }
00120     
00121     // check it's the right type
00122     TypedArg<T> *ret = dynamic_cast<TypedArg<T>*>(i->second);
00123     if (ret)
00124     {
00125         return true;
00126     }
00127     
00128     return false;
00129 }
00130 
00131 template<class T>
00132 void PrimitiveFunction::SetArg(const string &name, const T &arg)
00133 {
00134     m_Args[name]=new TypedArg<T>(arg);
00135 }
00136 
00137 
00138 }
00139 
00140 #endif

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