src/engine/TemplateTable.h

Go to the documentation of this file.
00001 
00011 #ifndef TEMPLATETABLE_H
00012 #define TEMPLATETABLE_H
00013 
00014 #include <boost/shared_ptr.hpp>
00015 #include "KeyNameTable.h"
00016 #include "Variable.h"
00017 #include "VariableTableInstance.h"
00018 #include "VariableTableTemplate.h"
00019                 
00020 namespace green
00021 {
00025         typedef std::vector<Variable::Value> ArgList;
00026         typedef boost::shared_ptr<ArgList> ArgListPtr;
00027         
00032         //template<class templateType, class instanceType>
00033         class TemplateTable : 
00034                 //public KeyNameTable<boost::shared_ptr<templateType> >
00035                 public KeyNameTable<VariableTableTemplatePtr>
00036         {
00037         public:
00038                 
00040                 virtual ~TemplateTable() {}
00041                 
00050                 VariableTableInstancePtr Instantiate(Key key, ArgListPtr initArgs) const
00051                 {
00052                         CHECK_SHARED(initArgs);
00053                         // Check the arguments and get the template entry
00054                         VariableTableTemplatePtr entry = 
00055                                         this->_checkInstArgs(key, initArgs);
00056                         
00057                         CHECK_SHARED(entry);
00058                         
00059                         // create a new instance with entry as its template
00060                         VariableTableInstancePtr instance(new VariableTableInstance(key));
00061                         this->_doInstantiate(instance, entry, initArgs);
00062                                 
00063                         // and return the instance
00064                         return instance;
00065                         
00066                 }
00067                 
00071                 void Print(std::ostream& os)
00072                 {
00073                         for(Key i = 0; i < this->GetNumEntries(); i++)
00074                         {
00075                                 _entryPtr curEntry(this->_getEntry(i));
00076                                 os << "Template Key: " << i << " Name: " << curEntry->mName << std::endl;
00077                                 os << "Template:" << std::endl;
00078                                 curEntry->mValue->Print(os);
00079                                 os << "---" << std::endl;
00080                         }
00081                 }
00082                 
00083                 protected: 
00084                         
00085                         VariableTableTemplatePtr
00086                                 _checkInstArgs(Key key, ArgListPtr initArgs) const
00087                         {
00088                                 if(!this->HasEntry(key))
00089                                         THROW_ERROR("invalid key");
00090                         
00091                                 VariableTableTemplatePtr entry(this->_getEntry(key)->mValue);
00092                 
00093                                 if(entry->GetInitFormat()->size() != initArgs->size())
00094                                         THROW_ERROR("initArgs is invalid size");
00095                                 
00096                                 return entry;
00097                         }
00098                         
00099                         void _doInstantiate(VariableTableInstancePtr instance, 
00100                                                                                                         VariableTableTemplatePtr templ, ArgListPtr args) const
00101                         {
00102                                 // Now add the default values to the instance
00103                                 for(Key i = 0; i < templ->GetNumEntries(); i++)
00104                                         instance->AddVariable(templ->GetEntry(i).mDefaultValue);
00105                         
00106                                 // Now set the initializers
00107                                 for(size_t i = 0; i < args->size(); i++)
00108                                         instance->SetVariable(templ->GetInitFormat()->at(i).mKey, (*args)[i]);
00109                         }
00110         };
00111         typedef boost::shared_ptr<TemplateTable> TemplateTablePtr;
00112         #define TEMPLATE_TABLE_CAST(x) boost::dynamic_pointer_cast<TemplateTable>(x)
00113 }
00114  #endif
00115 

Generated on Fri Apr 27 10:27:38 2007 for Green Engine by  doxygen 1.5.1