00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "data/broker.hpp"
00035 #include "data/log.hpp"
00036 #include "data/exception.hpp"
00037 #include "data/string.hpp"
00038 #include "data/config.hpp"
00039
00040 #include <typeinfo>
00041
00042 #ifdef WIN32
00043
00044 #pragma warning (disable:4661)
00045 #endif
00046
00047
00048 namespace gsgl
00049 {
00050
00051 namespace data
00052 {
00053
00054 const gsgl::string & brokered_object::get_type_name() const
00055 {
00056 if (type_name.is_empty())
00057 {
00058 brokered_object *ptr = const_cast<brokered_object *>(this);
00059 const type_info & info = typeid(*ptr);
00060 type_name = string(info.name());
00061 }
00062
00063 return type_name;
00064 }
00065
00066
00067
00068
00069 broker::broker()
00070 : global_register<broker_creator, gsgl::data::dictionary<broker_creator *, gsgl::string> >()
00071 {
00072 gsgl::log(L"broker: creating global broker");
00073 }
00074
00075
00076 broker::~broker()
00077 {
00078 gsgl::log(L"broker: destroying global broker");
00079 }
00080
00081
00082 bool broker::has_object(const string & type_name)
00083 {
00084 return registered_resources.contains_index(type_name);
00085 }
00086
00087
00088 brokered_object *broker::create_object(const string & type_name, const config_record & obj_conf)
00089 {
00090 gsgl::log(string(L"broker: creating object of type ") + type_name);
00091
00092 broker_creator *bc = registered_resources[type_name];
00093
00094 if (bc)
00095 {
00096 broker_creator::creator_ft ccc = bc->get_create_func(); assert(ccc);
00097 brokered_object *result = ccc(obj_conf);
00098 if (result)
00099 result->type_name = type_name;
00100 return result;
00101 }
00102 else
00103 {
00104 gsgl::log(string(L"broker: UNABLE TO CREATE OBJECT OF TYPE ") + type_name);
00105 throw runtime_exception(L"%ls: unknown object type", type_name.w_string());
00106 }
00107 }
00108
00109
00110 broker *broker::global_instance()
00111 {
00112 broker *temp = dynamic_cast<broker *>(singleton<global_register<broker_creator, gsgl::data::dictionary<broker_creator *, gsgl::string> > >::global_instance());
00113 if (!temp)
00114 temp = new broker();
00115 return temp;
00116 }
00117
00118
00119
00120
00121 broker_creator::broker_creator(const string & type_name, creator_ft create_func)
00122 : type_name(type_name), create_func(create_func)
00123 {
00124 assert(!type_name.is_empty());
00125 assert(create_func);
00126 broker::global_instance()->register_resource(this);
00127 }
00128
00129
00130 broker_creator::~broker_creator()
00131 {
00132 broker::global_instance()->unregister_resource(this);
00133 }
00134
00135
00136
00137 global_register<broker_creator, gsgl::data::dictionary<broker_creator *, gsgl::string> > *data::broker::instance = 0;
00138
00139 }
00140
00141
00142 }