Rapicorn - Experimental UI Toolkit - Source Code
13.07.0
|
00001 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 00002 #ifndef __RAPICORN_UTILITIES_HH__ 00003 #define __RAPICORN_UTILITIES_HH__ 00004 00005 #include <rapicorn-core.hh> 00006 #include <typeinfo> 00007 00008 #if !defined __RAPICORN_CLIENTAPI_HH_ && !defined __RAPICORN_UITHREAD_HH__ && !defined __RAPICORN_BUILD__ 00009 #error Only <rapicorn.hh> can be included directly. 00010 #endif 00011 00012 /* --- internally used macros --- */ 00013 #ifdef __RAPICORN_BUILD__ 00014 // convenience macros 00015 #define MakeProperty RAPICORN_AIDA_PROPERTY 00016 #define MakeNamedCommand RAPICORN_MakeNamedCommand 00017 #define MakeSimpleCommand RAPICORN_MakeSimpleCommand 00018 #endif 00019 00020 namespace Rapicorn { 00021 00022 /* --- standard utlities --- */ 00023 //template<typename T> inline const T& min (const T &a, const T &b) { return ::std::min<T> (a, b); } 00024 //template<typename T> inline const T& max (const T &a, const T &b) { return ::std::min<T> (a, b); } 00025 using ::std::min; 00026 using ::std::max; 00027 inline double min (double a, int64 b) { return min<double> (a, b); } 00028 inline double min (int64 a, double b) { return min<double> (a, b); } 00029 inline double max (double a, int64 b) { return max<double> (a, b); } 00030 inline double max (int64 a, double b) { return max<double> (a, b); } 00031 00032 /* --- exceptions --- */ 00033 struct Exception : std::exception { 00034 public: 00035 explicit Exception (const String &s1, const String &s2 = String(), const String &s3 = String(), const String &s4 = String(), 00036 const String &s5 = String(), const String &s6 = String(), const String &s7 = String(), const String &s8 = String()); 00037 void set (const String &s); 00038 virtual const char* what () const throw() { return reason ? reason : "out of memory"; } 00039 /*Copy*/ Exception (const Exception &e); 00040 /*Des*/ ~Exception () throw(); 00041 private: 00042 Exception& operator= (const Exception&); 00043 char *reason; 00044 }; 00045 struct NullPointer : std::exception {}; 00046 /* allow 'dothrow' as function argument analogous to 'nothrow' */ 00047 extern const std::nothrow_t dothrow; /* indicate "with exception" semantics */ 00048 using std::nothrow_t; 00049 using std::nothrow; 00050 00051 /* --- derivation assertions --- */ 00052 template<class Derived, class Base> 00053 struct EnforceDerivedFrom { 00054 EnforceDerivedFrom (Derived *derived = 0, 00055 Base *base = 0) 00056 { base = derived; } 00057 }; 00058 template<class Derived, class Base> 00059 struct EnforceDerivedFrom<Derived*,Base*> { 00060 EnforceDerivedFrom (Derived *derived = 0, 00061 Base *base = 0) 00062 { base = derived; } 00063 }; 00064 template<class Derived, class Base> void // ex: assert_derived_from<Child, Base>(); 00065 assert_derived_from (void) 00066 { 00067 EnforceDerivedFrom<Derived, Base> assertion; 00068 } 00069 00070 /* --- derivation checks --- */ 00071 template<class Child, class Base> 00072 class CheckDerivedFrom { 00073 static bool is_derived (void*) { return false; } 00074 static bool is_derived (Base*) { return true; } 00075 public: 00076 static bool is_derived () { return is_derived ((Child*) (0)); } 00077 }; 00078 template<class Child, class Base> 00079 struct CheckDerivedFrom<Child*,Base*> : CheckDerivedFrom<Child,Base> {}; 00080 template<class Derived, class Base> bool 00081 is_derived () // ex: if (is_derived<Child, Base>()) ...; */ 00082 { 00083 return CheckDerivedFrom<Derived,Base>::is_derived(); 00084 } 00085 00086 /* --- type dereferencing --- */ 00087 template<typename Type> struct Dereference; 00088 template<typename Type> struct Dereference<Type*> { 00089 typedef Type* Pointer; 00090 typedef Type Value; 00091 }; 00092 template<typename Type> struct Dereference<Type*const> { 00093 typedef Type* Pointer; 00094 typedef Type Value; 00095 }; 00096 template<typename Type> struct Dereference<const Type*> { 00097 typedef const Type* Pointer; 00098 typedef const Type Value; 00099 }; 00100 template<typename Type> struct Dereference<const Type*const> { 00101 typedef const Type* Pointer; 00102 typedef const Type Value; 00103 }; 00104 00105 /* --- PointerIterator - iterator object from pointer --- */ 00106 template<typename Value> 00107 class PointerIterator { 00108 protected: 00109 Value *current; 00110 public: 00111 typedef std::random_access_iterator_tag iterator_category; 00112 typedef Value value_type; 00113 typedef ptrdiff_t difference_type; 00114 typedef Value* pointer; 00115 typedef Value& reference; 00116 public: 00117 explicit PointerIterator () : current() {} 00118 explicit PointerIterator (value_type *v) : current (v) {} 00119 /*Copy*/ PointerIterator (const PointerIterator &x) : current (x.current) {} 00120 Value* base() const { return current; } 00121 reference operator* () const { return *current; } 00122 pointer operator-> () const { return &(operator*()); } 00123 PointerIterator& operator++ () { ++current; return *this; } 00124 PointerIterator operator++ (int) { PointerIterator tmp = *this; ++current; return tmp; } 00125 PointerIterator& operator-- () { --current; return *this; } 00126 PointerIterator operator-- (int) { PointerIterator tmp = *this; --current; return tmp; } 00127 PointerIterator operator+ (difference_type n) const { return PointerIterator (current + n); } 00128 PointerIterator& operator+= (difference_type n) { current += n; return *this; } 00129 PointerIterator operator- (difference_type n) const { return PointerIterator (current - n); } 00130 PointerIterator& operator-= (difference_type n) { current -= n; return *this; } 00131 reference operator[] (difference_type n) const { return *(*this + n); } 00132 }; 00133 template<typename Value> PointerIterator<Value> 00134 pointer_iterator (Value * const val) 00135 { return PointerIterator<Value> (val); } 00136 template<typename Value> inline bool 00137 operator== (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00138 { return x.base() == y.base(); } 00139 template<typename Value> inline bool 00140 operator!= (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00141 { return x.base() != y.base(); } 00142 template<typename Value> inline bool 00143 operator< (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00144 { return x.base() < y.base(); } 00145 template<typename Value> inline bool 00146 operator<= (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00147 { return x.base() <= y.base(); } 00148 template<typename Value> inline bool 00149 operator> (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00150 { return x.base() > y.base(); } 00151 template<typename Value> inline bool 00152 operator>= (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00153 { return x.base() >= y.base(); } 00154 template<typename Value> inline typename PointerIterator<Value>::difference_type 00155 operator- (const PointerIterator<Value> &x, const PointerIterator<Value> &y) 00156 { return x.base() - y.base(); } 00157 template<typename Value> inline PointerIterator<Value> 00158 operator+ (typename PointerIterator<Value>::difference_type n, const PointerIterator<Value> &x) 00159 { return x.operator+ (n); } 00160 00161 /* --- ValueIterator - dereferencing iterator --- */ 00162 template<class Iterator> struct ValueIterator : public Iterator { 00163 typedef Iterator iterator_type; 00164 typedef typename Iterator::iterator_category iterator_category; 00165 typedef typename Iterator::difference_type difference_type; 00166 typedef typename Iterator::value_type pointer; 00167 typedef typename Dereference<pointer>::Value value_type; 00168 typedef value_type& reference; 00169 Iterator base () const { return *this; } 00170 reference operator* () const { return *Iterator::operator*(); } 00171 pointer operator-> () const { return *Iterator::operator->(); } 00172 ValueIterator& operator= (const Iterator &it) { Iterator::operator= (it); return *this; } 00173 explicit ValueIterator (Iterator it) : Iterator (it) {} 00174 /*Copy*/ ValueIterator (const ValueIterator &dup) : Iterator (dup.base()) {} 00175 /*Con*/ ValueIterator () : Iterator() {} 00176 template<typename Iter> 00177 /*Con*/ ValueIterator (const ValueIterator<Iter> &src) : Iterator (src.base()) {} 00178 }; 00179 template<class Iterator> ValueIterator<Iterator> 00180 value_iterator (const Iterator &iter) 00181 { return ValueIterator<Iterator> (iter); } 00182 template<class Iterator> inline bool 00183 operator== (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00184 { return x.base() == y.base(); } 00185 template<class Iterator> inline bool 00186 operator!= (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00187 { return x.base() != y.base(); } 00188 template<class Iterator> inline bool 00189 operator< (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00190 { return x.base() < y.base(); } 00191 template<class Iterator> inline bool 00192 operator<= (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00193 { return x.base() <= y.base(); } 00194 template<class Iterator> inline bool 00195 operator> (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00196 { return x.base() > y.base(); } 00197 template<class Iterator> inline bool 00198 operator>= (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00199 { return x.base() >= y.base(); } 00200 template<class Iterator> inline typename ValueIterator<Iterator>::difference_type 00201 operator- (const ValueIterator<Iterator> &x, const ValueIterator<Iterator> &y) 00202 { return x.base() - y.base(); } 00203 template<class Iterator> inline ValueIterator<Iterator> 00204 operator+ (typename ValueIterator<Iterator>::difference_type n, const ValueIterator<Iterator> &x) 00205 { return x.operator+ (n); } 00206 00207 /* --- IteratorRange (iterator range wrappers) --- */ 00208 template<class Iterator> class IteratorRange { 00209 Iterator ibegin, iend; 00210 public: 00211 typedef Iterator iterator; 00212 typedef typename Iterator::iterator_category iterator_category; 00213 typedef typename Iterator::value_type value_type; 00214 typedef typename Iterator::difference_type difference_type; 00215 typedef typename Iterator::reference reference; 00216 typedef typename Iterator::pointer pointer; 00217 explicit IteratorRange (const Iterator &cbegin, const Iterator &cend) : 00218 ibegin (cbegin), iend (cend) 00219 {} 00220 explicit IteratorRange() 00221 {} 00222 iterator begin() const { return ibegin; } 00223 iterator end() const { return iend; } 00224 bool done() const { return ibegin == iend; } 00225 bool has_next() const { return ibegin != iend; } 00226 reference operator* () const { return ibegin.operator*(); } 00227 pointer operator-> () const { return ibegin.operator->(); } 00228 IteratorRange& operator++ () { ibegin.operator++(); return *this; } 00229 IteratorRange operator++ (int) { IteratorRange dup (*this); ibegin.operator++(); return dup; } 00230 bool operator== (const IteratorRange &w) const 00231 { 00232 return ibegin == w.begin && iend == w.end; 00233 } 00234 bool operator!= (const IteratorRange &w) const 00235 { 00236 return ibegin != w.begin || iend != w.end; 00237 } 00238 }; 00239 template<class Iterator> IteratorRange<Iterator> 00240 iterator_range (const Iterator &begin, const Iterator &end) 00241 { 00242 return IteratorRange<Iterator> (begin, end); 00243 } 00244 00245 /* --- ValueIteratorRange (iterator range wrappers) --- */ 00246 template<class Iterator> class ValueIteratorRange { 00247 Iterator ibegin, iend; 00248 public: 00249 typedef Iterator iterator; 00250 typedef typename Iterator::iterator_category iterator_category; 00251 typedef typename Iterator::value_type pointer; 00252 // typedef typeof (*(pointer) 0) value_type; 00253 typedef typename Dereference<pointer>::Value value_type; 00254 typedef typename Iterator::difference_type difference_type; 00255 typedef value_type& reference; 00256 explicit ValueIteratorRange (const Iterator &cbegin, const Iterator &cend) : 00257 ibegin (cbegin), iend (cend) 00258 {} 00259 explicit ValueIteratorRange() 00260 {} 00261 iterator begin() const { return ibegin; } 00262 iterator end() const { return iend; } 00263 bool done() const { return ibegin == iend; } 00264 bool has_next() const { return ibegin != iend; } 00265 reference operator* () const { return *ibegin.operator*(); } 00266 pointer operator-> () const { return ibegin.operator*(); } 00267 ValueIteratorRange& operator++ () { ibegin.operator++(); return *this; } 00268 ValueIteratorRange operator++ (int) { ValueIteratorRange dup (*this); ibegin.operator++(); return dup; } 00269 bool operator== (const ValueIteratorRange &w) const 00270 { 00271 return ibegin == w.begin && iend == w.end; 00272 } 00273 bool operator!= (const ValueIteratorRange &w) const 00274 { 00275 return ibegin != w.begin || iend != w.end; 00276 } 00277 }; 00278 template<class Iterator> ValueIteratorRange<Iterator> 00279 value_iterator_range (const Iterator &begin, const Iterator &end) 00280 { 00281 return ValueIteratorRange<Iterator> (begin, end); 00282 } 00283 00284 /* --- Walker (easy to use iterator_range (begin(), end()) substitute) --- */ 00285 template<typename Value> class Walker { 00286 /* auxillary Walker classes */ 00287 struct AdapterBase { 00288 virtual bool done () = 0; 00289 virtual void inc () = 0; 00290 virtual AdapterBase* clone () const = 0; 00291 virtual void* element () const = 0; 00292 virtual bool equals (const AdapterBase &eb) const = 0; 00293 virtual ~AdapterBase () {} 00294 }; 00295 AdapterBase *adapter; 00296 public: 00297 template<class Iterator> class Adapter : public AdapterBase { 00298 Iterator ibegin, iend; 00299 public: 00300 explicit Adapter (const Iterator &cbegin, const Iterator &cend) : 00301 ibegin (cbegin), iend (cend) 00302 {} 00303 virtual bool done () { return ibegin == iend; } 00304 virtual void inc () { ibegin.operator++(); } 00305 virtual AdapterBase* clone () const { return new Adapter (ibegin, iend); } 00306 virtual void* element () const { return (void*) ibegin.operator->(); } 00307 virtual bool equals (const AdapterBase &eb) const 00308 { 00309 const Adapter *we = dynamic_cast<const Adapter*> (&eb); 00310 return we && ibegin == we->ibegin && iend == we->iend; 00311 } 00312 }; 00313 /* Walker API */ 00314 typedef Value value_type; 00315 typedef value_type& reference; 00316 typedef value_type* pointer; 00317 Walker& operator= (const Walker &w) { adapter = w.adapter ? w.adapter->clone() : NULL; return *this; } 00318 bool done () const { return !adapter || adapter->done(); } 00319 bool has_next () const { return !done(); } 00320 pointer operator-> () const { return reinterpret_cast<pointer> (adapter ? adapter->element() : NULL); } 00321 reference operator* () const { return *operator->(); } 00322 Walker& operator++ () { if (adapter) adapter->inc(); return *this; } 00323 Walker operator++ (int) { Walker dup (*this); if (adapter) adapter->inc(); return dup; } 00324 bool operator== (const Walker &w) const { return w.adapter == adapter || (adapter && w.adapter && adapter->equals (*w.adapter)); } 00325 bool operator!= (const Walker &w) const { return !operator== (w); } 00326 ~Walker() { if (adapter) delete adapter; adapter = NULL; } 00327 Walker (const Walker &w) : adapter (w.adapter ? w.adapter->clone() : NULL) {} 00328 Walker (AdapterBase *cadapter = NULL) : adapter (cadapter) {} 00329 }; 00330 template<class Container> Walker<const typename Container::const_iterator::value_type> 00331 walker (const Container &container) 00332 { 00333 typedef typename Container::const_iterator Iterator; 00334 typedef Walker<const typename Iterator::value_type> Walker; 00335 typedef typename Walker::template Adapter<Iterator> Adapter; 00336 return Walker (new Adapter (container.begin(), container.end())); 00337 } 00338 template<class Container> Walker<typename Container::iterator::value_type> 00339 walker (Container &container) 00340 { 00341 typedef typename Container::iterator Iterator; 00342 typedef Walker<typename Iterator::value_type> Walker; 00343 typedef typename Walker::template Adapter<Iterator> Adapter; 00344 return Walker (new Adapter (container.begin(), container.end())); 00345 } 00346 template<class Container> Walker<typename Dereference<const typename Container::const_iterator::value_type>::Value> 00347 value_walker (const Container &container) 00348 { 00349 typedef typename Container::const_iterator Iterator; 00350 typedef typename Dereference<const typename Iterator::value_type>::Value Value; 00351 typedef Walker<Value> Walker; 00352 typedef ValueIterator<Iterator> VIterator; 00353 typedef typename Walker::template Adapter<VIterator> Adapter; 00354 return Walker (new Adapter (VIterator (container.begin()), VIterator (container.end()))); 00355 } 00356 template<class Container> Walker<typename Dereference<typename Container::iterator::value_type>::Value> 00357 value_walker (Container &container) 00358 { 00359 typedef typename Container::iterator Iterator; 00360 typedef typename Dereference<typename Iterator::value_type>::Value Value; 00361 typedef Walker<Value> Walker; 00362 typedef ValueIterator<Iterator> VIterator; 00363 typedef typename Walker::template Adapter<VIterator> Adapter; 00364 return Walker (new Adapter (VIterator (container.begin()), VIterator (container.end()))); 00365 } 00366 template<class Iterator> Walker<typename Iterator::value_type> 00367 walker (const Iterator &begin, const Iterator &end) 00368 { 00369 typedef Walker<typename Iterator::value_type> Walker; 00370 typedef typename Walker::template Adapter<Iterator> Adapter; 00371 return Walker (new Adapter (begin, end)); 00372 } 00373 template<class Iterator> Walker<typename Dereference<typename Iterator::value_type>::Value> 00374 value_walker (const Iterator &begin, const Iterator &end) 00375 { 00376 typedef typename Dereference<typename Iterator::value_type>::Value Value; 00377 typedef Walker<Value> Walker; 00378 typedef ValueIterator<Iterator> VIterator; 00379 typedef typename Walker::template Adapter<VIterator> Adapter; 00380 return Walker (new Adapter (VIterator (begin), VIterator (end))); 00381 } 00382 00383 } // Rapicorn 00384 00385 #endif /* __RAPICORN_UTILITIES_HH__ */