Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members Related Pages

# attribute.h

Go to the documentation of this file.

    00001 /* -*- c++ -*- */
    00002 #ifndef AKAXISO2_FRAMEWORK_ATTRIBUTE_H__
    00003 #define AKAXISO2_FRAMEWORK_ATTRIBUTE_H__
    00004
    00010 #include <akaxiso2/framework/operators.h>
    00011 #include <akaxiso2/framework/qname.h>
    00012 #include <akaxiso2/framework/namespaces.h>
    00013 #include <akaxiso2/framework/member.h>
    00014 #include <akaxiso2/framework/membertype.h>
    00015 #include <akaxiso2/framework/fixed.h>
    00016 #include <akaxiso2/framework/any_op.h>
    00017
    00018 namespace aka2 {
    00019
    00020
    00031   template<class L, class T>
    00032   class  attributes {
    00033   public:
    00041     struct _attribute {
    00047       template<class P, class V>
    00048       _attribute(const std::string &tagname, V P::* member) {
    00049         new_attribute(tagname, member, xiso::leaf<V>());
    00050       }
    00051
    00058       template<class P, class V, class VL>
    00059       _attribute(const std::string &tagname, V P::* member, const VL& vl) {
    00060         new_attribute(tagname, member, VL());
    00061       }
    00062
    00069       void set_default(const std::string &defval) {
    00070         if (attr_ == 0)
    00071           throw tagged_error("attribute", tagname_, "could not have a default value.",
    00072                              __FILE__, __LINE__); // attribute already specified.
    00073         attr_->setup_default_value(defval);
    00074         attr_->set_required(false); // When default is specified, '@use' should be "optional".
    00075         attr_ = 0;
    00076       }
    00077
    00084       void required(bool val) {
    00085         if (attr_ == 0)
    00086           throw tagged_error("attribute", tagname_, "could not have its use as required.",
    00087                       __FILE__, __LINE__);
    00088         attr_->set_required(val);
    00089         attr_ = 0;
    00090       }
    00091
    00092     private:
    00093       template<class P, class V, class VL>
    00094       void new_attribute(const std::string &tagname, V P::*member, const VL) {
    00095         tagname_ = tagname;
    00096         VL::initialize();
    00097         member_getter *getter =
    00098           create_ptr_getter(reinterpret_cast<T*>(0), member);
    00099         const simpletype_op &sop = VL::dispatcher_;
    00100         attribute_type attr = aka2::attribute_type(getter, sop);
    00101         attr.set_name(qname(tagname));
    00102
    00103         default_op *defop = VL::create_default_op();
    00104         if (defop != 0)
    00105           attr.set_default_op(defop);
    00106
    00107         L::attribute_types_.push_back(attr); // add dup-name check? !!!!!
    00108         attr_ = &L::attribute_types_.back();
    00109       }
    00110       attribute_type *attr_;
    00111       std::string tagname_;
    00112     };
    00113
    00120     typedef _attribute attribute;
    00121
    00132     template<class P>
    00133     void any_attribute(wc_attributes P::*member, const std::string &ns_list = "##any")  {
    00134       member_getter *mgetter =
    00135         create_ptr_getter(reinterpret_cast<T*>(0), member);
    00136       any_attributes_ = aka2::attribute_type(mgetter);
    00137       any_attributes_.set_ns_list(ns_list);
    00138     }
    00139
    00146     template<class V>
    00147     struct fixed_attribute {
    00153       fixed_attribute(const std::string &tagname, const std::string &fixed_value) {
    00154         new_attribute(tagname, fixed_value, xiso::leaf<V>());
    00155       }
    00156
    00163       template<class VL>
    00164       fixed_attribute(const std::string &tagname, const std::string &fixed_value, const VL &vl) {
    00165         new_attribute(tagname, fixed_value, VL());
    00166       }
    00167
    00174       void required(bool val) { attr_->set_required(val); }
    00175     private:
    00176       template<class VL>
    00177       void new_attribute(const std::string &tagname, const std::string &fixed_value, const VL&) {
    00178         VL::initialize();
    00179         member_getter *mg = new null_getter();
    00180         attribute_type attr = aka2::attribute_type(mg, fixed<VL>::dispatcher_);
    00181         attr.set_name(qname(tagname));
    00182
    00183         default_op *defop = VL::create_default_op();
    00184         assert(defop != 0);
    00185         attr.set_default_op(defop);
    00186         attr.setup_default_value(fixed_value);
    00187
    00188         L::attribute_types_.push_back(attr); // add dup-name check?
    00189         attr_ = &L::attribute_types_.back();
    00190       }
    00191       attribute_type *attr_;
    00192     };
    00193
    00194     static const attribute_type* get_anyattr_type() {
    00195       if (any_attributes_.empty())
    00196         return 0;
    00197       return &any_attributes_;
    00198     }
    00199
    00200     static attribute_types attribute_types_;
    00201     static attribute_type any_attributes_;
    00202   };
    00203
    00204   template<class L, class T>
    00205   attribute_types attributes<L, T>::attribute_types_;
    00206
    00207   template<class L, class T>
    00208   attribute_type attributes<L, T>::any_attributes_;
    00209
    00210 }
    00211
    00212
    00213 #endif