Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
common.h
Go to the documentation of this file.00001 00004 /* 00005 * Copyright © 2000, 2001 Sofus Mortensen 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef COMET_COMMON_H 00020 #define COMET_COMMON_H 00021 00022 #include <comet/config.h> 00023 #include <wtypes.h> 00024 00025 namespace comet { 00026 00027 class uuid_t; 00028 template<typename Itf>class com_ptr; 00029 namespace impl { 00030 template<typename T> T* bad_alloc_check(T* x) 00031 { 00032 if (!x) throw std::bad_alloc(); 00033 return x; 00034 } 00035 00039 template<typename T> class auto_attach_t 00040 { 00041 public: 00042 explicit auto_attach_t(const T& v) : val_(v) {}; 00043 const T& get() const { return val_; } 00044 private: 00045 const T& val_; 00046 auto_attach_t& operator=(const auto_attach_t&); 00047 }; 00048 00049 // Forward declare all these - only used if we actually WANT a 00050 // safearray. 00054 template<typename T> struct sa_traits; 00058 template<typename S,typename T> class sa_iterator; 00062 template<typename T> struct const_traits; 00066 template<typename T> struct nonconst_traits; 00067 00068 enum sa_traits_check_type { stct_features_ok, stct_vt_ok, stct_iid_ok }; 00069 enum sa_traits_extras_type { stet_null, stet_record, stet_iid }; 00070 00071 // Interface traits are needed by all interfaces so that we can create 00072 // safearrays of the types. 00073 template<typename INTERFACE, VARTYPE VT, long FEATURE_FLAG> 00074 struct interface_sa_traits 00075 { 00076 enum { vt = VT }; 00077 enum { check_type = stct_iid_ok }; 00078 enum { extras_type = stet_iid }; 00079 00080 typedef INTERFACE* raw; 00081 typedef com_ptr<INTERFACE> value_type; 00082 typedef com_ptr<INTERFACE> & reference; 00083 typedef const com_ptr<INTERFACE> & const_reference; 00084 00085 static reference create_reference(raw& x) { return *reinterpret_cast<com_ptr< INTERFACE>*>(&x); } 00086 static const_reference create_const_reference(raw& x) { return *reinterpret_cast<const com_ptr< INTERFACE >*>(&x); } 00087 typedef nonconst_traits<com_ptr<INTERFACE> > nct; 00088 typedef sa_iterator<com_ptr<INTERFACE>, nct > iterator; 00089 typedef sa_iterator<com_ptr<INTERFACE>, const_traits<com_ptr<INTERFACE> > > const_iterator; 00090 00091 static bool are_features_ok(unsigned short f) { return (f & FEATURE_FLAG) != 0 && (f & FADF_HAVEIID) != 0; } 00092 static const uuid_t& iid() { return uuidof<INTERFACE>(); } 00093 }; 00094 00098 template<typename T, VARTYPE VT> struct basic_sa_traits 00099 { 00100 enum { vt = VT }; 00101 enum { check_type = stct_vt_ok }; 00102 enum { extras_type = stet_null }; 00103 00104 typedef T raw; 00105 typedef T value_type; 00106 typedef T& reference; 00107 typedef const T& const_reference; 00108 00109 static reference create_reference(T& x) { return x; } 00110 static const_reference create_const_reference(T& x) { return x; } 00111 00112 typedef T* iterator; 00113 typedef const T* const_iterator; 00114 00115 static bool are_features_ok(unsigned short) { return true; } 00116 }; 00117 } 00118 00122 00123 template<typename T> impl::auto_attach_t<T> auto_attach(const T& t) { return impl::auto_attach_t<T>(t); } 00124 00125 00126 // template<typename T, typename U> inline T up_cast(const U& u, T* = 0) { return u; } 00127 00128 00132 inline VARIANT_BOOL bool_in(bool x) { return x ? COMET_VARIANT_TRUE : COMET_VARIANT_FALSE; } 00133 00138 class bool_out { 00139 public: 00140 operator VARIANT_BOOL*() { return &vb_; } 00141 bool_out(bool& b) : b_(b) {} 00142 ~bool_out() { b_ = vb_ ? true : false; } 00143 private: 00144 bool_out &operator=( const bool_out &); 00145 VARIANT_BOOL vb_; 00146 bool& b_; 00147 }; 00148 00149 namespace impl { 00150 class bool_adapter_t { 00151 public: 00152 bool_adapter_t(VARIANT_BOOL* p) : pb_(p) { b_ = *pb_ ? true : false; } 00153 ~bool_adapter_t() { *pb_ = b_ ? COMET_VARIANT_TRUE : COMET_VARIANT_FALSE; } 00154 00155 bool& ref() { return b_; } 00156 private: 00157 bool_adapter_t(const bool_adapter_t&); 00158 bool_adapter_t& operator=(const bool_adapter_t&); 00159 00160 VARIANT_BOOL* pb_; 00161 bool b_; 00162 }; 00163 } 00164 00169 class bool_inout { 00170 public: 00171 operator VARIANT_BOOL*() { return &vb_; } 00172 bool_inout(bool& b) : b_(b), vb_(b ? COMET_VARIANT_TRUE : COMET_VARIANT_FALSE) {} 00173 ~bool_inout() { b_ = vb_ ? true : false; } 00174 private: 00175 bool_inout &operator=(const bool_inout &); 00176 VARIANT_BOOL vb_; 00177 bool& b_; 00178 }; 00179 00185 class variant_bool_t 00186 { 00187 VARIANT_BOOL vb_; 00188 public: 00190 00191 variant_bool_t(): vb_(COMET_VARIANT_FALSE) {} 00192 variant_bool_t(const impl::auto_attach_t<VARIANT_BOOL> &b) : vb_(b.get()) {} 00193 variant_bool_t(bool b) : vb_(b?COMET_VARIANT_TRUE:COMET_VARIANT_FALSE) {} 00195 00197 00198 variant_bool_t &operator=( bool b) { vb_ = b?COMET_VARIANT_TRUE:COMET_VARIANT_FALSE; return *this;} 00199 variant_bool_t &operator=( const impl::auto_attach_t<VARIANT_BOOL> &b) { vb_ = b.get(); return *this; } 00201 00203 00204 operator bool() const{ return vb_!= COMET_VARIANT_FALSE; } 00205 bool operator !() const { return vb_== COMET_VARIANT_FALSE; } 00206 bool operator==( variant_bool_t vb) const { return vb.vb_ == vb_; } 00207 bool operator!=( variant_bool_t vb) const { return vb.vb_ != vb_; } 00209 00211 00212 variant_bool_t operator~() const { variant_bool_t temp(*this); temp.vb_ = (VARIANT_BOOL)~(temp.vb_); return temp; } 00213 variant_bool_t &operator&=( const variant_bool_t &b) { vb_ &= b.vb_; return *this; } 00214 variant_bool_t &operator|=( const variant_bool_t &b) { vb_ |= b.vb_; return *this; } 00215 variant_bool_t &operator^=( const variant_bool_t &b) { vb_ ^= b.vb_; return *this; } 00216 variant_bool_t operator&( const variant_bool_t &b)const { variant_bool_t temp(*this); temp.vb_ &= b.vb_; return temp; } 00217 variant_bool_t operator|( const variant_bool_t &b)const { variant_bool_t temp(*this); temp.vb_ |= b.vb_; return temp; } 00218 variant_bool_t operator^( const variant_bool_t &b)const { variant_bool_t temp(*this); temp.vb_ ^= b.vb_; return temp; } 00220 00221 00222 bool operator&( bool b)const { return b & operator bool(); } 00223 bool operator|( bool b)const { return b | operator bool(); } 00224 bool operator^( bool b)const { return b ^ operator bool(); } 00226 00227 static const variant_bool_t &create_const_reference(const VARIANT_BOOL &vb) { return reinterpret_cast<const variant_bool_t &>(vb); } 00228 static variant_bool_t &create_reference(VARIANT_BOOL &vb) { return reinterpret_cast<variant_bool_t &>(vb); } 00229 00231 00232 VARIANT_BOOL in() { return vb_; } 00233 VARIANT_BOOL *out() { return &vb_; } 00234 VARIANT_BOOL *inout() { return &vb_; } 00236 00237 00241 class bool_pointer_t 00242 { 00243 friend class variant_bool_t; 00244 protected: 00245 bool_pointer_t( VARIANT_BOOL &vb) : vb_(vb), b_( vb != COMET_VARIANT_FALSE) {} 00246 public: 00247 ~bool_pointer_t() { vb_ = b_ ? COMET_VARIANT_TRUE: COMET_VARIANT_FALSE; } 00248 operator bool*(){ return &b_; } 00249 operator const bool*()const{ return &b_; } 00250 private: 00251 bool_pointer_t &operator=(const bool_pointer_t &); 00252 bool b_; 00253 VARIANT_BOOL &vb_; 00254 }; 00262 bool_pointer_t bool_ptr() 00263 { 00264 return bool_pointer_t(vb_); 00265 } 00266 const bool_pointer_t bool_ptr() const 00267 { 00268 return bool_pointer_t(const_cast<VARIANT_BOOL &>(vb_)); 00269 } 00270 00271 friend class bool_reference_t; 00274 class bool_reference_chooser_t 00275 { 00276 friend class variant_bool_t; 00277 variant_bool_t &vbt_; 00278 protected: 00279 bool_reference_chooser_t(variant_bool_t &vbt):vbt_(vbt) {} 00280 private: 00281 bool_reference_chooser_t &operator=(const bool_reference_chooser_t &); 00282 public: 00283 inline operator variant_bool_t&() { return vbt_;} 00284 inline operator const variant_bool_t&()const { return vbt_;} 00285 }; 00289 class bool_reference_t : protected bool_pointer_t 00290 { 00291 public: 00292 bool_reference_t( bool_reference_chooser_t &brc ) 00293 : bool_pointer_t(*static_cast<variant_bool_t&>(brc).inout()) 00294 {} 00295 operator bool &(){ return tmp; /*return * (bool_pointer_t::operator bool*());*/ } 00296 operator const bool &()const { return *(bool_pointer_t::operator const bool*()); } 00297 bool tmp; 00298 }; 00299 00302 bool_reference_chooser_t bool_ref() 00303 { 00304 return bool_reference_chooser_t(*this); 00305 } 00306 const bool_reference_chooser_t bool_ref() const 00307 { 00308 return bool_reference_chooser_t(const_cast<variant_bool_t &>(*this)); 00309 } 00310 }; 00312 00313 } // namespace 00314 00315 #endif