From: <mk...@us...> - 2003-01-20 06:46:38
|
Update of /cvsroot/csp/APPLICATIONS/SimData/Include/SimData In directory sc8-pr-cvs1:/tmp/cvs-serv20520/Include/SimData Modified Files: Tag: simdata ObjectInterface.h Log Message: added (experimental) simulated partial template specialization for win32 (vc++) Index: ObjectInterface.h =================================================================== RCS file: /cvsroot/csp/APPLICATIONS/SimData/Include/SimData/Attic/ObjectInterface.h,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -C2 -d -r1.1.2.3 -r1.1.2.4 *** ObjectInterface.h 20 Jan 2003 05:53:12 -0000 1.1.2.3 --- ObjectInterface.h 20 Jan 2003 06:46:35 -0000 1.1.2.4 *************** *** 47,50 **** --- 47,122 ---- + /* + * Simulated Partial Template Specialization + * + * Adapted from: metactrl.h + * by Krzysztof Czarnecki & Ulrich Eisenecker + */ + + /* The following functions come from chapter 10 of the indispensable book + * Generative Programming by Krzysztof Czarnecki & Ulrich Eisenecker + * (C) Copyright Krzysztof Czarnecki & Ulrich Eisenecker 1998-2000. + * Permission to copy, use, modify, sell and distribute this software is + * granted provided this copyright notice appears in all copies. In case of + * modification, the modified files should carry a notice stating that + * you changed the files. + * This software is provided "as is" without express or implied + * warranty, and with no claim as to its suitability for any purpose. + */ + + #ifdef WIN32 + #define __PTS_SIM__ + #endif + + #ifdef __PTS_SIM__ + + namespace PTS { + + template <typename T> + char IsVector(std::vector<T> const); // no implementation is required + + int IsVector(...); // no implementation is required + + template <class T> + struct ISVECTOR { + enum { RET = (sizeof(IsVector(*(T*)0)) == 1) }; + }; + + struct SelectThen + { template<class Then, class Else> + struct Result + { typedef Then RET; + }; + }; // end SelectThen + + struct SelectElse + { template<class Then, class Else> + struct Result + { typedef Else RET; + }; + }; // end SelectElse + + template<bool Condition> + struct Selector { + }; // end Selector + + template<> + struct Selector<true> + { typedef SelectThen RET; + }; // end Selector<false> + + template<> + struct Selector<false> + { typedef SelectElse RET; + }; // end Selector<false> + } + + #define MEMBERACCESSOR(C, T) \ + (typename PTS::Selector<PTS::ISVECTOR<T>::RET>::RET\ + ::Result<VectorMemberAccessor<C, T>, MemberAccessor<C, T> >::RET) + + #endif // __PTS_SIM__ + + /** * class MemberAccessorBase - Base class for storing and accessing member *************** *** 133,136 **** --- 205,259 ---- * @author Mark Rose <mr...@st...> */ + #ifdef __PTS_SIM__ + template <class C, typename T> + class VectorMemberAccessor: public MemberAccessorBase + { + T C::* member; + public: + VectorMemberAccessor(T C::*pm, std::string name_, bool required_) { + member = pm; + name = name_; + required = required_; + } + virtual void push_back(Object *o, TypeAdapter const &v) throw(TypeMismatch) { + C * object = dynamic_cast<C *>(o); + if (object == NULL) { + throw TypeMismatch("push_back(\"" + name + "\"): Object class does not match interface."); + } + typename T::value_type value; + v.set(value); + (object->*member).push_back(value); + } + virtual void clear(Object *o) throw(TypeMismatch) { + C * object = dynamic_cast<C *>(o); + if (object == NULL) { + throw TypeMismatch("push_back(\"" + name + "\"): Object class does not match interface."); + } + (object->*member).clear(); + } + virtual void pack(Object *o, Packer &p) const { + C * object = dynamic_cast<C *>(o); + T &m = object->*member; + p.pack(m.size()); + typename T::iterator idx; + for (idx = m.begin(); idx != m.end(); idx++) { + p.pack(*idx); + } + } + virtual void unpack(Object *, UnPacker &p) { + C * object = dynamic_cast<C *>(o); + T &m = object->*member; + typename T::value_type temp; + int n; + p.unpack(n); + while (n-- > 0) { + p.unpack(temp); + m.push_back(temp); + } + } + }; + + #else + template <class C, typename T> class MemberAccessor< C, std::vector<T> >: public MemberAccessorBase *************** *** 181,184 **** --- 304,308 ---- } }; + #endif *************** *** 244,248 **** --- 368,376 ---- Self& def(const char *name, T C::*pm, bool required) throw(InterfaceError) { if (variableExists(name)) throw InterfaceError("interface variable \"" + std::string(name) + "\" multiply defined."); + #ifdef __PTS_SIM__ + table[name] = new MEMBERACCESSOR(C, T)(pm, name, required); + #else table[name] = new MemberAccessor<C, T>(pm, name, required); + #endif return *this; } |