NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
PropertyMap.h
Go to the documentation of this file.
00001 #pragma once
00002 
00013 // Standard Includes
00014 #include <map>
00015 #include <string>
00016 
00017 // Library Includes
00018 #include <boost/any.hpp>
00019 #include <boost/unordered_map.hpp>
00020 #include <threading.h>
00021 
00022 // Local Includes
00023 
00024 // Forward Declarations
00025 
00026 // Typedefs
00027 
00028 class PropertyMap {
00029 public:
00030   PropertyMap(void) { }
00031   
00032   PropertyMap(const PropertyMap& orig) {
00033     Threading::ReadLock r_lock(orig.mutex);
00034     this->propmap = orig.propmap;
00035   }
00036   
00037   ~PropertyMap(void) { }
00038 
00039   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00040   template<typename T>
00041   void SetProperty(std::string key, T value) {
00042     Threading::WriteLock w_lock(this->mutex);
00043     
00044     this->propmap[key] = value;
00045   }
00046 
00047   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00048   bool PropertyExists(std::string key) {
00049     Threading::ReadLock r_lock(this->mutex);
00050     
00051     if (this->propmap.find(key) != this->propmap.end()) {
00052       return true;
00053     }
00054     return false;
00055   }
00056 
00057   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00058   boost::any GetProperty(std::string key) {
00059     Threading::ReadLock r_lock(this->mutex);
00060     
00061     if (this->propmap.find(key) != this->propmap.end()) {
00062       return this->propmap[key];
00063     }
00064     return boost::any();
00065   }
00066 
00067   // Template specializations for different return types
00068   template <typename t>
00069   t GetPropertyReference(std::string key) {
00070     Threading::ReadLock r_lock(this->mutex);
00071     
00072     boost::any a = (GetProperty(key));
00073     if (a.type() == typeid(t)) {
00074       return boost::any_cast<t>(a);
00075     }
00076     return nullptr;
00077   }
00078 
00079   template <typename t>
00080   t GetPropertyValue(std::string key) {
00081     Threading::ReadLock r_lock(this->mutex);
00082     
00083     boost::any a = (GetProperty(key));
00084     if (a.type() == typeid(t)) {
00085       return boost::any_cast<t>(a);
00086     }
00087     return t();
00088   }
00089 
00090   void swap(PropertyMap& rhs) {
00091     Threading::ReadLock r_lock(this->mutex);
00092     
00093     this->propmap.swap(rhs.propmap);
00094   }
00095 
00096   PropertyMap& operator=(const PropertyMap& rhs) {
00097     PropertyMap temp(rhs); // Copy the rhs,
00098     swap(temp); // swap the contents, viola, assignment done! (Got to love "Effective C++" 3rd Ed. by Scott Meyers.)
00099     
00100     return *this;
00101   }
00102 
00103 private:
00104   mutable Threading::ReadWriteMutex mutex;
00105   std::map<std::string, boost::any> propmap;
00106 };