00001
00002 #ifndef AKAXISO2_FRAMEWORK_SIMPLETYPE_H__
00003 #define AKAXISO2_FRAMEWORK_SIMPLETYPE_H__
00004
00010 #include <akaxiso2/framework/type_registry.h>
00011 #include <akaxiso2/framework/member.h>
00012 #include <akaxiso2/framework/operators.h>
00013 #include <akaxiso2/framework/xmltype.h>
00014 #include <akaxiso2/util/sstream.h>
00015
00016 namespace aka2 {
00017
00018 template<class V, class VL>
00019 struct default_value : public default_op {
00020 typedef V value_type;
00021
00022 default_value() : has_default_(false) {}
00023 virtual ~default_value(){}
00024
00025 virtual void set_default(const std::string &defval) {
00026 VL::read_text(&default_, defval, system_entity_complements());
00027 has_default_ = true;
00028 }
00029
00030 virtual bool has_default() const {
00031 return has_default_;
00032 }
00033
00034 virtual const void *value() const {
00035 assert(has_default_);
00036 return &default_;
00037 }
00038
00039 private:
00040 V default_;
00041 bool has_default_;
00042 };
00043
00044
00045 template<class L>
00046 struct simpletype_op_dispatcher : public simpletype_op {
00047 public:
00048 virtual schematype_id get_schematype() const { return simpletype_id; }
00049 virtual std::string get_typename() const { return L::get_xmltype(); }
00050 virtual const attribute_types *get_attribute_types() const { return 0; }
00051 virtual const attribute_type *get_anyattr_type() const { return 0; }
00052
00053 virtual void write_text(const void* elm,
00054 std::ostream &ostm,
00055 entity_complements &ecomp) const {
00056 L::write_text(elm, ostm, ecomp);
00057 }
00058
00059 virtual void read_text(void* elm, const std::string &entity,
00060 entity_complements &ecomp) const {
00061 L::read_text(elm, entity, ecomp);
00062 }
00063
00064 virtual void construct(void *e) const { L::construct(e); }
00065 virtual void copy_construct(void *e, const void *src) const { L::copy_construct(e, src); }
00066 virtual void destruct(void *e) const { L::destruct(e); }
00067 virtual size_t class_size() const { return L::class_size(); }
00068 virtual bool equals(const void *lhs, const void *rhs) const { return L::equals(lhs, rhs); }
00069 };
00070
00071
00072 template<class L>
00073 struct simpletype_statics {
00074 static simpletype_op_dispatcher<L> dispatcher_;
00075 };
00076
00077 template<class L>
00078 simpletype_op_dispatcher<L> simpletype_statics<L>::dispatcher_;
00079
00088 template<class V, class L=xiso::leaf<V> >
00089 class simpletype : public simpletype_statics<L>,
00090 public xmltype_statics<L> {
00091 public:
00092 virtual ~simpletype(){}
00093 typedef V value_type;
00094
00095 static void initialize() {
00096 system_type_registry().add(L());
00097 }
00098 static void uninitialize() {}
00099
00111 static void write_text(const void *elm, std::ostream &ostm,
00112 entity_complements &ecomp) {
00113 const V &value = *reinterpret_cast<const V*>(elm);
00114 ostm << value;
00115 }
00116
00128 static void read_text(void *elm, const std::string &entity,
00129 entity_complements &ecomp) {
00130 V &value = *reinterpret_cast<V*>(elm);
00131 isstream istm(entity);
00132 istm >> value;
00133 if (istm.fail())
00134 throw error(std::string("Failed to parse simpletype value, \"")
00135 + istm.rdbuf()->str() + "\".",
00136 __FILE__, __LINE__);
00137 }
00138
00139
00140 static void construct(void *e) { new (e) V(); }
00141 static void copy_construct(void *e, const void *src) {
00142 new (e) V(*static_cast<const V*>(src));
00143 }
00144 static size_t class_size() { return sizeof(V); }
00145 static void destruct(void *elm) { static_cast<V*>(elm)->~V(); }
00146
00147 static bool equals(const void *lhs, const void *rhs) {
00148 return *static_cast<const V*>(lhs) == *static_cast<const V*>(rhs);
00149 }
00150
00151 static default_op* create_default_op() { return new default_value<V, L>; }
00152 static simpletype_op_dispatcher<L> *get_attribute_dispatcher() {
00153 return &L::dispatcher_;
00154 }
00155 };
00156
00157 }
00158
00159 #endif