src/engine/EntityTemplate.h

Go to the documentation of this file.
00001 
00012 #ifndef ENTITYTEMPLATE_H
00013 #define ENTITYTEMPLATE_H
00014 #include <vector>
00015 #include <boost/shared_ptr.hpp>
00016 #include "util/Error.h"
00017 #include "VariableTableTemplate.h"
00018 #include "TemplateTable.h"
00019 #include "parser/impcomp/UnresOpSeq.h"
00020 #include "parser/impcomp/OpSeq.h"
00021 
00022 namespace green
00023 {
00031         class EntityTemplate : public VariableTableTemplate
00032         {
00033         public:
00034                 EntityTemplate()
00035                         : VariableTableTemplate()
00036                 {
00037                         this->AddEntry("x", Variable::FLOAT_VAR, 0.0f);
00038                         this->AddEntry("y", Variable::FLOAT_VAR, 0.0f);
00039                         this->AddEntry("xs", Variable::FLOAT_VAR, 0.0f);
00040                         this->AddEntry("ys", Variable::FLOAT_VAR, 0.0f);
00041                         this->AddEntry("rot", Variable::FLOAT_VAR, 0.0f);
00042                 }
00043                 
00048                 inline
00049                 void AddBehavior(Key behavior,
00050                                                                                  ArgListPtr args)
00051                 {
00052                         CHECK_SHARED(args);
00053                         mBehaviors.push_back(_listEntry(behavior, args));
00054                 }
00055                 
00063                 inline
00064                 void AddBehavior(const Name& name,
00065                                                                                  ArgListPtr args)
00066                 {
00067                         CHECK_SHARED(args);
00068                         mBehaviors.push_back(_listEntry(name, args));
00069                 }
00070 
00077                 inline
00078                 void SetBehaviorKey(size_t behavior, Key key)
00079                 {
00080                         ERROR_ASSERT(behavior < mBehaviors.size(),
00081                                                                          "That behavior is not in this template");
00082                         mBehaviors[behavior].mBehaviorKey = key;
00083                 }
00090                 Key GetBehavior(size_t behavior) const
00091                 {
00092                         if(behavior > mBehaviors.size())
00093                                 THROW_ERROR("behavior too large");
00094                         return mBehaviors[behavior].mBehaviorKey;
00095                 }
00096                 
00101                 inline
00102                 const Name& GetBehaviorName(size_t behavior)
00103                 {
00104                         ERROR_ASSERT(behavior < mBehaviors.size(),
00105                                                                          "that behavior is not in this template");
00106                         return mBehaviors[behavior].mBehaviorName;
00107                 }
00108 
00114                 ArgListPtr GetBehaviorArgs(size_t behavior) const
00115                 {
00116                         if(behavior > mBehaviors.size())
00117                                 THROW_ERROR("behavior too large");
00118                         return mBehaviors[behavior].mArg;
00119                 }
00120 
00125                 inline
00126                 size_t GetNumBehaviors() const
00127                 {
00128                         return mBehaviors.size();
00129                 }
00130 
00131 
00135                 void addInitCode(UnresOpSeqPtr uInitCodeIn) {
00136                         CHECK_SHARED(uInitCodeIn);
00137                         unresInitCode = uInitCodeIn;
00138                 }
00139 
00144                 UnresOpSeqPtr getInitCode() {
00145                         CHECK_SHARED(unresInitCode);
00146                         return unresInitCode;
00147                 }
00148 
00149 
00157                 void addResolvedInit(VariableTableTemplatePtr argList,OpSeqPtr resdCode) {
00158                         CHECK_SHARED(argList);
00159                         CHECK_SHARED(resdCode);
00160                         
00161                         if (initCode.get() != 0) return;
00162                         initCode = resdCode;
00163 
00164                         //yes.. I'm ashamed but resolveOpSeq takes a VarTableTemplate
00165                         //and that is just hard hard to change... soooo.... yea...
00166                         InitFormatPtr initFormat(new InitFormat());
00167                         for (size_t i=0;i<argList->GetNumEntries();++i)
00168                                 initFormat->push_back(InitArgFormat(i,argList->GetEntry(i).mType));
00169                 }
00170 
00171 
00176                 virtual void Print(std::ostream& os)
00177                 {
00178                         VariableTableTemplate::Print(os);
00179                         os << std::endl;
00180                         os << "Behaviors:" << std::endl;
00181                         for(size_t i = 0; i < mBehaviors.size(); i++)
00182                         {
00183                                 os << " Key: " << mBehaviors[i].mBehaviorKey;
00184                                 os << " Name: " << mBehaviors[i].mBehaviorName;
00185                                 os << " ArgList: ";
00186                                 
00187                                 CHECK_SHARED(mBehaviors[i].mArg);
00188                                 for(size_t j = 0; j < mBehaviors[i].mArg->size(); j++)
00189                                         os << mBehaviors[i].mArg->at(j) << " ";
00190                                 os << std::endl;
00191                         }
00192 
00193                         os << "CODE:" << std::endl;
00194                         if(unresInitCode.get())
00195                                 unresInitCode->dumpScreen();
00196                         if(initCode.get())
00197                                 initCode->dumpScreen();
00198                 }
00199                 
00200         protected:
00201 
00202                 class _listEntry
00203                 {
00204                 public:
00205                         _listEntry(Key behavior, ArgListPtr arg) :
00206                                         mBehaviorKey(behavior), mArg(arg) {}
00207                         _listEntry(const Name& name, ArgListPtr arg) :
00208                                         mBehaviorKey(0), mBehaviorName(name), mArg(arg) {}
00209                         
00210                         Key mBehaviorKey;
00211                         Name mBehaviorName;
00212                         ArgListPtr mArg;
00213                 };
00214 
00215                 std::vector<_listEntry> mBehaviors;
00216 
00217                 //unresolved and resolved code stuff, TODO:delete UnresOpSeqs
00218                 UnresOpSeqPtr unresInitCode;
00219                 OpSeqPtr initCode;
00220         };
00221         typedef boost::shared_ptr<EntityTemplate> EntityTemplatePtr;
00222         #define ENTITY_TEMPLATE_CAST(x) boost::dynamic_pointer_cast<EntityTemplate>(x)
00223 }
00224 
00225 #endif

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