00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef incCOMMON_PERSISTABLE_OBJECT
00010 #define incCOMMON_PERSISTABLE_OBJECT
00011
00012
00013 #include <string>
00014 #include "object.hpp"
00015 #include "boost/filesystem/path.hpp"
00016
00017 namespace ph {
00018 namespace common {
00019
00020
00021 class persistable_object_context;
00022 class object_writer;
00023
00024 class persistable_object_base
00025
00026
00027
00028
00029
00030
00031
00032
00033 {
00034 public:
00035 virtual ~persistable_object_base() {}
00036
00037 virtual object_base *create(
00038 const std::string &type,
00039 const std::string &name,
00040 persistable_object_context *context) = 0;
00041
00042
00043
00044
00045 virtual object_base *get_composite_object(const std::string &name) = 0;
00046
00047
00048
00049 virtual bool has(const std::string &name) const = 0;
00050
00051
00052
00053 virtual void set(const std::string &name, const std::string &value) = 0;
00054
00055
00056 virtual std::string get(const std::string &name) const = 0;
00057
00058
00059
00060 virtual bool write(object_writer *writer) const
00061 { return true; }
00062
00063
00064
00065 virtual void set_file_path(const std::string &path) {}
00066
00067
00068 };
00069
00070 class persistable_object_context
00071
00072
00073
00074
00075 {
00076 public:
00077 virtual ~persistable_object_context() {};
00078
00079 virtual object_base *create(
00080 const std::string &type,
00081 const std::string &name) = 0;
00082
00083
00084
00085 virtual object_base *find_object(
00086 object_base *root,
00087 object_base *obj,
00088 const std::string &location) = 0;
00089
00090
00091
00092
00093
00094
00095 virtual object_base *find_composite_object(
00096 object_base *root,
00097 object_base *obj,
00098 const std::string &location) = 0;
00099
00100
00101
00102
00103
00104
00105 virtual bool delayed_import() { return false; }
00106
00107
00108
00109
00110
00111
00112
00113
00114 virtual ph::common::object_base *create_delayed(
00115 std::ostream *console,
00116 const boost::filesystem::path &url,
00117 const std::string &name,
00118 unsigned int debug) { return 0 ; }
00119
00120 };
00121
00122 class object_writer
00123
00124
00125
00126 {
00127 public:
00128 virtual ~object_writer() {};
00129
00130 virtual bool start(const std::string &tagname) = 0;
00131
00132
00133 virtual bool attr(const std::string &name, const std::string &value) = 0;
00134
00135
00136 virtual bool data(const std::string &d) = 0;
00137
00138
00139 virtual bool end(const std::string &tagname) = 0;
00140
00141
00142
00143 };
00144
00145 class object_writer_context
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 {
00159 public:
00160 object_writer_context(object_writer *writer, const std::string &tagname) :
00161 _writer(writer), _tagname(tagname)
00162 { _writer->start(_tagname); }
00163 ~object_writer_context()
00164 { _writer->end(_tagname); }
00165 void attr(const std::string &name, const std::string &value)
00166 { _writer->attr(name, value); }
00167 void data(const std::string &d)
00168 { _writer->data(d); }
00169
00170 private:
00171 object_writer *_writer;
00172 std::string _tagname;
00173 };
00174
00175 inline bool get_persistable_obj_name(const object_base *obj, std::string *name)
00176
00177
00178
00179 {
00180 if (!obj->persistable())
00181 return false;
00182
00183 *name = obj->persistable()->get("name");
00184 return true;
00185 }
00186
00187 inline bool get_persistable_obj_type(const object_base *obj, std::string *type)
00188
00189
00190
00191 {
00192 if (!obj->persistable())
00193 return false;
00194
00195 *type = obj->persistable()->get("type");
00196 return true;
00197 }
00198
00199 inline bool set_persistable_obj_name(object_base *obj, const std::string &name)
00200
00201
00202
00203 {
00204 if (!obj->persistable())
00205 return false;
00206
00207 obj->persistable()->set("name", name);
00208 return true;
00209 }
00210
00211 inline bool set_persistable_obj_type(object_base *obj, const std::string &type)
00212
00213
00214
00215 {
00216 if (!obj->persistable())
00217 return false;
00218
00219 obj->persistable()->set("type", type);
00220 return true;
00221 }
00222
00223 };
00224 };
00225
00226 #endif // incCOMMON_PERSISTABLE_OBJECT