src/engine/LibraryMessages.h

Go to the documentation of this file.
00001 
00012 #ifndef LIBRARYMESSAGES_H
00013 #define LIBRARYMESSAGES_H
00014 
00015 #include <string>
00016 #include "MessageInstance.h"
00017 #include "MessageTemplateTable.h"
00018 #include "MessageTemplate.h"
00019 
00020 namespace green
00021 {
00022         
00027         class AlwaysMsg
00028         {
00029                 public:
00030                         virtual ~AlwaysMsg() {}
00031                         static MessageInstancePtr Instantiate(Key msgTemplate, Key genericEntity, 
00032                                                                                                                                                                                 MessageTemplateTablePtr table)
00033                         {
00034                                 using namespace std;
00035                 //              cerr << "Instantiating a always msg" << endl;
00036                                 
00037                                 CHECK_SHARED(table);
00038                                 ERROR_ASSERT(table->HasEntry(msgTemplate), "msg template table doesn't have that template");
00039                                 
00040                                 // create a new ArgList
00041                                 ArgListPtr args(new ArgList());
00042                                 
00043                                 MessageInstancePtr newMsg(table->Instantiate(msgTemplate, args));
00044                                 CHECK_SHARED(newMsg);
00045 
00046                                 return newMsg;
00047                         }
00048                         
00052                         static MessageTemplatePtr CreateTemplate()
00053                         {
00054                                 // Create the template
00055                                 MessageTemplatePtr keyEvent(new MessageTemplate());
00056                                 
00057                                 // setup an init arg format
00058                                 InitFormatPtr initFormat(new InitFormat());
00059                 
00060                                 // set the init arg format
00061                                 keyEvent->SetInitFormat(initFormat);
00062                 
00063                                 // we're done!
00064                                 return keyEvent;
00065                         }
00066                         
00067         };
00068         
00079         class KeyEventMsg
00080         {
00081                 protected:
00082                         unsigned char mKey;
00083                         int mState;
00084                         KeyEventMsg() {}
00085                         
00086                 public:
00095                         KeyEventMsg(unsigned int key, int state) :
00096                                 mKey(key), mState(state)
00097                         {}
00098                         
00105                         KeyEventMsg(MessageInstancePtr msg)
00106                         {
00107                                 CHECK_SHARED(msg);
00108                                 ERROR_ASSERT(msg->GetNumVariables() == 2, "wrong number of variables in message");
00109                                 ERROR_ASSERT(msg->GetVariable(0).intVal >= 0, "invalid key");
00110                                 int state = msg->GetVariable(1).intVal;
00111                                 ERROR_ASSERT(state == 0 || state == 1, "state must be 0 or 1");
00112                                 
00113                                 mKey = (unsigned int) state;
00114                                 mState = msg->GetVariable(1).intVal;
00115                         }
00116                         
00120                         virtual ~KeyEventMsg() {}
00121                         
00126                         unsigned int GetKey() const {
00127                                 return mKey;
00128                         }
00129                         
00134                         int GetState() const {
00135                                 return mState;
00136                         }
00137                         
00144                         virtual MessageInstancePtr Instantiate(Key msgTemplate, MessageTemplateTablePtr table)
00145                         {
00146                                 using namespace std;
00147                                 cerr << "Instantiating a KeyEventMsg" << endl;
00148                                 cerr << "msgTempalte: " << msgTemplate << endl;
00149                                 cerr << " key: " << mKey << " state: " << mState << endl;
00150                                 
00151                                 CHECK_SHARED(table);
00152                                 ERROR_ASSERT(table->HasEntry(msgTemplate), "msg template table doesn't have that template");
00153                                 
00154                                 // create a new ArgList
00155                                 ArgListPtr args(new ArgList());
00156                                 args->push_back(Variable::Value((int)mKey));
00157                                 args->push_back(Variable::Value(mState));
00158                                 
00159                                 MessageInstancePtr newMsg(table->Instantiate(msgTemplate, args));
00160                                 CHECK_SHARED(newMsg);
00161                                 ERROR_ASSERT(newMsg->GetTemplate() == msgTemplate, "newMsg's template key doesn't match");
00162 //                              VariableTableInstancePtr newMsgInstance = table->Instantiate(msgTemplate, args);
00163 //                              ERROR_ASSERT(newMsgInstance.get(), "newMsgInstance is NULL");
00164 //                              
00165 //                              MessageInstancePtr newMsg(MESSAGE_INSTANCE_CAST(newMsgInstance));
00166 //                              ERROR_ASSERT(newMsg.get(), "could not create new message");
00167                                 return newMsg;
00168                         }
00169                         
00173                         static MessageTemplatePtr CreateTemplate()
00174                         {
00175                                 // Create the template
00176                                 MessageTemplatePtr keyEvent(new MessageTemplate());
00177                 
00178                                 // add key and state variables
00179                                 Key keyKey = keyEvent->AddEntry("key", Variable::INT_VAR, Variable::Value(0));
00180                                 Key stateKey = keyEvent->AddEntry("state", Variable::INT_VAR, Variable::Value(0));
00181                                 
00182                                 // Check that the entries are assigned the right keys
00183                                 ERROR_ASSERT(keyKey == 0 && stateKey == 1, "variable keys were not assigned correctly");
00184                                 
00185                                 // setup an init arg format
00186                                 InitFormatPtr initFormat(new InitFormat());
00187                                 initFormat->push_back(InitArgFormat(keyKey, Variable::INT_VAR));
00188                                 initFormat->push_back(InitArgFormat(stateKey, Variable::INT_VAR));
00189                 
00190                                 // set the init arg format
00191                                 keyEvent->SetInitFormat(initFormat);
00192                 
00193                                 // we're done!
00194                                 return keyEvent;
00195                         }
00196                         
00197         };
00198         
00199 }
00200 
00201 #endif

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