Go to the documentation of this file.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
00035 #ifndef KEYVALUE_KEY_MAP_OBJECTMAP_H_
00036 #define KEYVALUE_KEY_MAP_OBJECTMAP_H_
00037
00038 #include "keyvalue/extern/String.h"
00039 #include "keyvalue/frontend/LexicalToolKit.h"
00040 #include "keyvalue/mngt/BuilderFrom.h"
00041 #include "keyvalue/mngt/DataSet.h"
00042 #include "keyvalue/mngt/ProcessorInstantiator.h"
00043 #include "keyvalue/mngt/Repository.h"
00044 #include "keyvalue/sys/exception/KV_ASSERT.h"
00045 #include "keyvalue/util/Global.h"
00046 #include "keyvalue/value/Variant.h"
00047
00048 namespace keyvalue {
00049 namespace key {
00050
00072 template <typename ObjectType>
00073 class ObjectMap {
00074
00075 public:
00076
00077 typedef shared_ptr<ObjectType> OutputType_;
00078
00086 shared_ptr<ObjectType>
00087 map(const value::Variant& variant) const;
00088
00089 private:
00090
00096 virtual string
00097 getName() const = 0;
00098
00099 };
00100
00101
00102
00103
00104
00105 template<typename ObjectType>
00106 shared_ptr<ObjectType>
00107 ObjectMap<ObjectType>::map(const value::Variant& variant) const {
00108
00109 Processor& builder(ProcessorInstantiator<ObjectType>::getInstance());
00110
00111 string name;
00112
00113
00114 if (variant.is<string>(frontend::LexicalToolKit::input)) {
00115
00116 name = variant.get<string>(frontend::LexicalToolKit::input);
00117
00118 try {
00119
00120 shared_ptr<const DataSet>
00121 data(util::Global<Repository>::get()->getDataSet(name));
00122
00123 value::Result result(data->process(builder));
00124
00125 const value::ObjectPtr object(result.getObjectPtr());
00126 KV_ASSERT(object, "value::Result must be non null ObjectPtr!");
00127 return static_pointer_cast<ObjectType>(object);
00128 }
00129
00130 catch(Repository::NotFound& error) {
00131 if (error.getName() != name)
00132 throw;
00133 }
00134 }
00135
00136
00137 BuilderFromVariant<ObjectType>*
00138 ptr(dynamic_cast<BuilderFromVariant<ObjectType>*>(&builder));
00139 if (ptr)
00140 return ptr->getObject(variant);
00141
00142 if (!name.empty())
00143 throw RuntimeError() & "Object '" & name & "' not found and builder " &
00144 builder.getName() & "cannot build from this input!";
00145
00146 throw RuntimeError() & "Builder '" & builder.getName() & "'cannot build"
00147 "from input " & variant & "'!";
00148 }
00149
00150 }
00151 }
00152
00153 #endif // KEYVALUE_KEY_MAP_OBJECTMAP_H_