00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
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     
00095     map<string,Arg*>::iterator i=m_Args.find(name);
00096     if (i==m_Args.end())
00097     {
00098         return def;
00099     }
00100     
00101     
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     
00115     map<string,Arg*>::iterator i=m_Args.find(name);
00116     if (i==m_Args.end())
00117     {
00118         return false;
00119     }
00120     
00121     
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