00001
00002 #ifndef AKAXISO2_FRAMEWORK_ENTITY_COMPLEMENTS_H__
00003 #define AKAXISO2_FRAMEWORK_ENTITY_COMPLEMENTS_H__
00004
00010 #include <akaxiso2/framework/types.h>
00011 #include <akaxiso2/util/shared_ptr.h>
00012 #include <map>
00013 #include <set>
00014
00015 namespace aka2 {
00016 class prefix_map;
00017
00018 void reset_for_new_document();
00019
00024 struct complement_operator {
00025 virtual ~complement_operator() { }
00030 virtual void reset_for_new_document(void *e) const = 0;
00031 };
00032
00036 template <class T>
00037 struct T_complement_operator : public complement_operator {
00038 virtual void reset_for_new_document(void *e) const {
00039 T* t = static_cast<T*>(e);
00040 t->clear();
00041 }
00042 };
00043
00049 struct entity_complement {
00055 entity_complement(void *complement, complement_operator *op)
00056 : complement_(complement), op_(op) { }
00057 void *complement_;
00058 shared_ptr<complement_operator> op_;
00059
00063 void reset_for_new_document() const {
00064 op_->reset_for_new_document(complement_);
00065 }
00066 };
00067
00068
00069 typedef std::set<std::string> id_constraint;
00070
00082 class entity_complements {
00083 shared_ptr<prefix_map> prefixes_;
00084 typedef std::map<std::string, entity_complement> complements;
00085 complements complements_;
00086 entity_complements(const entity_complements &);
00087 id_constraint id_constraint_;
00088 void clear_complements();
00089 public:
00094 prefix_map &get_prefixes() const { return *prefixes_; }
00095
00101 template<class T>
00102 void add_complement(const std::string &name, T &t) {
00103 add_complement(name, &t, new T_complement_operator<T>());
00104 }
00105
00112 template<class T>
00113 void add_complement(const std::string &name, T &t, complement_operator *op) {
00114 add_complement_ptr(name, &t, op);
00115 }
00116
00117 void add_complement_ptr(const std::string &name, void *p, complement_operator *prop_op);
00118 void *get_complement_ptr(const std::string &name) const;
00119 const void *const_get_complement_ptr(const std::string &name) const;
00120
00127 bool check_id(const std::string &value);
00128
00132 void clear_all();
00133
00137 void reset_for_new_document();
00138
00139 shared_ptr<entity_complements> clone(bool copy_prefixes) const;
00140 explicit entity_complements();
00141 ~entity_complements();
00142 };
00143
00144
00151 template<class T>
00152 inline T& get_complement(entity_complements &ecomps, const std::string &name) {
00153 return *static_cast<T*>(ecomps.get_complement_ptr(name));
00154 }
00155
00162 template<class T>
00163 inline const T& get_complement(const entity_complements &ecomps,
00164 const std::string &name) {
00165 return *static_cast<const T*>(ecomps.const_get_complement_ptr(name));
00166 }
00167 }
00168
00169 #endif