00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019
00020
00021 #ifndef SOME_class_properties_HEADER
00022 #define SOME_class_properties_HEADER
00023
00024 #include "UserConfig.h"
00025 #include <string>
00026 #include <cassert>
00027 #include <map>
00028 #ifdef _MSC_VER //should we just go with some_string_map? probably not
00029 #include "SOMEStringMap.h"
00030 #endif
00031
00032 namespace SOME
00033 {
00042 class ClassCatalog
00043 {
00044 public:
00045 typedef void* (*ConstructFunc)();
00046
00047 ClassCatalog(const std::string& classname,
00048 const std::string& the_category,
00049 ConstructFunc def = NULL,
00050 ConstructFunc one = NULL,
00051 ConstructFunc two = NULL):
00052 ctor0_func(def),
00053 ctor1_func(one),
00054 ctor2_func(two)
00055 {
00056 assert(ctor0_func != NULL || ctor1_func != NULL || ctor2_func != NULL);
00057 class_properties["classname"] = classname;
00058 class_properties["category"] = the_category;
00059 }
00060
00062 void addCategory( std::string key, std::string value )
00063 {
00064
00065 if (class_properties[key] == "")
00066 class_properties[key] = value;
00067 }
00068
00070 std::map < std::string, std::string > getProperties()
00071 {
00072 #ifdef _MSC_VER
00073 return class_properties.getStdMap();
00074 #else
00075 return class_properties;
00076 #endif
00077 }
00078 std::string getCategory(std::string key)
00079 {
00080 return class_properties[key];
00081 }
00082
00087 bool infoEquals( std::string key, std::string value )
00088 {
00089 return class_properties[key] == value;
00090 }
00091
00092
00093
00094 const std::string& getClassName()
00095 {
00096 return class_properties["classname"];
00097 }
00099 const std::string& getCategory()
00100 {
00101 return class_properties["category"];
00102 }
00103 ConstructFunc getCtor0() const
00104 {
00105 return ctor0_func;
00106 }
00107 ConstructFunc getCtor1() const
00108 {
00109 return ctor1_func;
00110 }
00111 ConstructFunc getCtor2() const
00112 {
00113 return ctor2_func;
00114 }
00115
00116 00118 00129 template < class BaseClass >
00130 BaseClass* construct()
00131 {
00132 BaseClass * ret_ptr = 0;
00133 BaseClass* (*ctor0)() = (BaseClass * (*)())ctor0_func;
00134 if (ctor0 != NULL)
00135 {
00136 ret_ptr = (*ctor0)();
00137 }
00138 return ret_ptr;
00139 }
00140
00151 template < class BaseClass >
00152 void deduce_construct(BaseClass** dest_ptr)
00153 {
00154 BaseClass* (*ctor0)() = (BaseClass * (*)())ctor0_func;
00155 if (ctor0 != NULL)
00156 *dest_ptr = (*ctor0)();
00157 else
00158 *dest_ptr = NULL;
00159 }
00160
00161
00163
00174 template < class BaseClass, typename ParamType1 >
00175 BaseClass* construct(ParamType1& a)
00176 {
00177 BaseClass* ret_ptr = 0;
00178 BaseClass* (*ctor1)(ParamType1&) = (BaseClass * (*)(ParamType1&))ctor1_func;
00179 if (ctor1 != NULL)
00180 {
00181 ret_ptr = (*ctor1)(a);
00182 }
00183 return ret_ptr;
00184 }
00185
00187
00198 template < class BaseClass, typename ParamType1, typename ParamType2 >
00199 BaseClass* construct(ParamType1& a, ParamType2& b)
00200 {
00201 BaseClass* ret_ptr = 0;
00202 BaseClass* (*ctor2)(ParamType1&, ParamType2&) = (BaseClass * (*)(ParamType1&, ParamType2&))ctor2_func;
00203 if (ctor2 != NULL)
00204 {
00205 ret_ptr = (*ctor2)(a, b);
00206 }
00207 return ret_ptr;
00208 }
00209
00210 private:
00211 #ifdef _MSC_VER
00212 StringMap class_properties;
00213 #else
00214 std::map < std::string, std::string > class_properties;
00215 #endif
00216
00217 ConstructFunc ctor0_func;
00218 ConstructFunc ctor1_func;
00219 ConstructFunc ctor2_func;
00220
00221 };
00222
00223 }
00224
00225 #endif //SOME_class_properties_HEADER