00001 /*************************************************************************** 00002 * * 00003 * (c) Art Tevs, MPI Informatik Saarbruecken * 00004 * mailto: <tevs@mpi-sb.mpg.de> * 00005 * * 00006 * This program is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU General Public License as published by * 00008 * the Free Software Foundation; either version 2 of the License, or * 00009 * (at your option) any later version. * 00010 * * 00011 ***************************************************************************/ 00012 00013 00014 #ifndef _NR_PROPERTY_MANAGERH_ 00015 #define _NR_PROPERTY_MANAGERH_ 00016 00017 //---------------------------------------------------------------------------------- 00018 // Includes 00019 //---------------------------------------------------------------------------------- 00020 #include "Prerequisities.h" 00021 #include "Property.h" 00022 #include "Log.h" 00023 #include "Exception.h" 00024 00025 namespace nrEngine{ 00026 00027 //! Property manager does hold properties for global access 00028 /** 00029 * Property manager class is used to access global properties. In this 00030 * way your application can store some values of any type in the manager 00031 * by a certain name. The value can then be accessed from another place. 00032 * Propertties are grouped in groups, so group names must be unique. If no 00033 * group name is specified, so default group is used. Property must have 00034 * unique names in one group. 00035 * 00036 * \ingroup gp 00037 **/ 00038 class _NRExport PropertyManager{ 00039 public: 00040 00041 /** 00042 * Create Property manager object and initialiye default data 00043 **/ 00044 PropertyManager(); 00045 00046 /** 00047 * Release used memory and remove all properties from the memory. 00048 **/ 00049 ~PropertyManager(); 00050 00051 /** 00052 * Create new property group. 00053 * If such group already exists, so nothing happens. 00054 * 00055 * @param name Unique name of a property group 00056 **/ 00057 void createGroup(const std::string& name); 00058 00059 /** 00060 * Get number of properties in a certain group 00061 * 00062 * @param [group] Unique name of a property group 00063 **/ 00064 //int32 getPropertyCount(const std::string& group = std::string()); 00065 00066 /** 00067 * Get property list of properties of certain group. 00068 * The list is const, so no changes are allowed. 00069 * If no such group exists, so empty list will be returned 00070 * @param [group] Name of the group 00071 **/ 00072 const PropertyList& getPropertyList(const std::string& group = std::string()); 00073 00074 /** 00075 * Get property. 00076 * @param name Name of the property 00077 * @param [group] Unique name of the group 00078 **/ 00079 Property& getProperty(const std::string& name, const std::string& group = std::string()); 00080 Property& getPropertyByFullName(const std::string& fullname); 00081 00082 /** 00083 * Get the property directly. @see PropertyManager::getProperty() 00084 **/ 00085 NR_FORCEINLINE Property& operator()(const std::string& name, const std::string& group = std::string()) {return getProperty(name, group);} 00086 00087 /** 00088 * Add/add new property to a group. 00089 * If no such group exists, so group will be created. 00090 * If such property already exists, so it will be overwritten 00091 * 00092 * @param property Property which to add 00093 * @param name Name of the property 00094 * @param group Unique group name. 00095 **/ 00096 void set (const Property& property, const std::string& name, const std::string& group = std::string()); 00097 00098 /** 00099 * Directly setup new property with new value 00100 * @see PropertyManager::set() 00101 * @param value Any value to set to the property (value must be convertible) 00102 * @param name Name of the property 00103 * @param group Unique goup name 00104 **/ 00105 void set(const boost::any& value, const std::string& name, const std::string& group = std::string()); 00106 00107 /** 00108 * Set new value based on fullname 00109 * @see IPropertyManager::set() 00110 * @param value Valeu to be set for the property 00111 * @param fullname Fullname of the property (i.e. "group.name") 00112 **/ 00113 void setByFullName(const boost::any& value, const std::string& fullname); 00114 void setByFullName(const Property& property,const std::string& name); 00115 00116 /** 00117 * Get the value directly from the database. 00118 * The method is templated, so you get the value converted to given type. 00119 * If conversion is not possible, so error occurs. To prevent errors, check 00120 * the type before access. 00121 * 00122 * @param name Name of the property 00123 * @param [group] Unique group name of the property 00124 **/ 00125 template<typename T> 00126 NR_FORCEINLINE T get(const std::string& name, const std::string& group = std::string()); 00127 00128 /** 00129 * @see get() 00130 **/ 00131 template<typename T> 00132 NR_FORCEINLINE T getByFullName(const std::string& name); 00133 00134 private: 00135 00136 //! This describey the type of our property map 00137 typedef std::map<std::string, PropertyList> PropertyMap; 00138 00139 //! Here we store our properties 00140 PropertyMap mPropertyMap; 00141 }; 00142 00143 00144 //---------------------------------------------------------------------------------- 00145 template<typename T> 00146 NR_FORCEINLINE T PropertyManager::get(const std::string& name, const std::string& group) 00147 { 00148 try{ 00149 // get property value 00150 boost::any& v = getProperty(name, group).getValue(); 00151 00152 // check if property is empty 00153 if (v.empty()) 00154 { 00155 // give warning, that now a undefined value will be returned 00156 NR_Log(Log::LOG_ENGINE | Log::LOG_CONSOLE, Log::LL_WARNING, "PropertyManager::get() - You are retrieving undefined value from '%s.%s' property", group.c_str(), name.c_str()); 00157 return T(); 00158 } 00159 00160 // cast the property value and return it back 00161 return boost::any_cast<T>(v); 00162 00163 }catch(const boost::bad_any_cast &){ 00164 NR_EXCEPT(PROPERTY_WRONG_TYPE, "Cannot cast property to the given type", "PropertyManager::get()"); 00165 } 00166 } 00167 00168 //---------------------------------------------------------------------------------- 00169 template<typename T> 00170 NR_FORCEINLINE T PropertyManager::getByFullName(const std::string& fullname) 00171 { 00172 try{ 00173 // get property value 00174 boost::any& v = getPropertyByFullName(fullname).getValue(); 00175 00176 // check if property is empty 00177 if (v.empty()) 00178 { 00179 // give warning, that now a undefined value will be returned 00180 NR_Log(Log::LOG_ENGINE | Log::LOG_CONSOLE, Log::LL_WARNING, "PropertyManager::get() - You are retrieving undefined value from '%s' property", fullname.c_str()); 00181 return T(); 00182 } 00183 00184 // cast the property value and return it back 00185 return boost::any_cast<T>(v); 00186 00187 }catch(const boost::bad_any_cast &){ 00188 NR_EXCEPT(PROPERTY_WRONG_TYPE, "Cannot cast property to the given type", "PropertyManager::get()"); 00189 } 00190 } 00191 00192 }; // end namespace 00193 00194 #endif