NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
Envelope.h
Go to the documentation of this file.
00001 #pragma once
00002 
00011 // Standard Includes
00012 #include <vector>
00013 
00014 // Library Includes
00015 #include <boost/any.hpp>
00016 #include <boost/utility/value_init.hpp>
00017 #include <boost/lexical_cast.hpp>
00018 #include <d3dx9math.h>
00019 #include <boost/property_tree/ptree_fwd.hpp>
00020 
00021 // Local Includes
00022 #include "EventLogger.h"
00023 #include "Envelope_fwd.h"
00024 #include "Entity_fwd.h"
00025 
00026 // Forward Declarations
00027 
00029 void SaveToDisk(const EnvelopeSPTR&, const std::string&);
00030 
00032 bool LoadFromDisk(const EnvelopeSPTR&, const std::string&);
00033 
00034 
00035 // Typedefs
00036 
00037 class Envelope {
00038 public:
00039   Envelope();
00040   ~Envelope() {}
00041   
00042   boost::any     GetData            (const unsigned int& = 0);
00043   boost::any     GetData            (const unsigned int& = 0) const;
00044   bool           GetDataBool        (const unsigned int& = 0);
00045   int            GetDataInt         (const unsigned int& = 0);
00046   long           GetDataLong        (const unsigned int& = 0);
00047   unsigned int   GetDataUInt        (const unsigned int& = 0);
00048   float          GetDataFloat       (const unsigned int& = 0);
00049   std::string    GetDataString      (const unsigned int& = 0);
00050   D3DXVECTOR3    GetDataVector      (const unsigned int& = 0);
00051   D3DXQUATERNION GetDataQuat        (const unsigned int& = 0);
00052   D3DXCOLOR      GetDataColor       (const unsigned int& = 0);
00053   EntitySPTR     GetDataEntityP     (const unsigned int& = 0);
00054   Envelope*      GetDataEnvelopeP   (const unsigned int& = 0);
00055   EnvelopeSPTR   GetDataEnvelopeSPTR(const unsigned int& = 0);
00056   
00057   template <typename T>
00058   T GetDataReference(const unsigned int& index) {
00059     Threading::ReadLock r_lock(this->mutex);
00060     
00061     boost::any a = (this->GetData(index));
00062     if (a.type() == typeid(T)) {
00063       return boost::any_cast<T>(a);
00064     }
00065     return nullptr;
00066   }
00067   
00068   template <typename T>
00069   T GetDataValue(const unsigned int& index) {
00070     Threading::ReadLock r_lock(this->mutex);
00071     
00072     boost::any a = (this->GetData(index));
00073     if (a.type() == typeid(T)) {
00074       return boost::any_cast<T>(a);
00075     }
00076     return T();
00077   }
00078   
00079   template<typename T>
00080   void AddData(const T& data) { 
00081     Threading::WriteLock w_lock(this->mutex);
00082     
00083     this->data.push_back(EnvelopeItem(typeid(data).name(), data));
00084   }
00085   
00086   template<typename T>
00087   void AddDataValue(const T& data) { 
00088     Threading::WriteLock w_lock(this->mutex);
00089     
00090     this->data.push_back(EnvelopeItem(typeid(data).name(), data));
00091   }
00092   
00093   template<typename T>
00094   void AddDataReference(T data) { 
00095     Threading::WriteLock w_lock(this->mutex);
00096     
00097     this->data.push_back(EnvelopeItem(typeid(data).name(), data));
00098   }
00099   
00101   unsigned int GetCount();
00102   
00104   void SaveToPropertyTree(boost::property_tree::ptree&, const std::string&);
00105   
00107   void LoadFromPropertyTree(boost::property_tree::ptree&, const std::string&);
00108   
00109 public: // Public properties
00110   int msgid; // Used to identify the message type
00111   
00112 private: // Utility methods
00113   template <typename T>
00114   T TGetData(unsigned int index);
00115   
00116 private: // Private types
00117   struct EnvelopeItem {
00118     EnvelopeItem(const std::string& type, const boost::any& data) :
00119       type(type),
00120       data(data)
00121     {}
00122     
00123     std::string type;
00124     boost::any data;
00125   };
00126   
00127 private: // Member Data
00128   mutable Threading::ReadWriteMutex mutex;
00129   std::vector<EnvelopeItem> data;
00130 };