OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
property.h
Go to the documentation of this file.
1//-------------------------------------------------------------------
2// ObjectWindows
3// Copyright(c) 1996 by Manic Software. All rights reserved.
4// Copyright (c) 1998 by Yura Bidus, All Rights Reserved
5/// \file
6/// Credits:
7/// The Property pattern is taken from "Patterns in Practice: A Property
8/// Template for C++", from "C++ Report", Nov/Dec 1995, p. 28-33, by Colin
9/// Hastie. I've in turn extended it to allow for "true" Delphi-like property
10/// syntax by allowing you to assign normal T objects to the TProperty<T>, as well
11/// as provide an implict conversion-to-T operator. Note that this implicit
12/// conversion is NOT highly held in regard by many C++-ers, and therefore can
13/// be \#defined out of existence by \#define-ing STRICT_PROPERTY_SYNTAX before
14/// \#include-ing this file.
15/// Delphi (and C++Builder) also allow for methods to be used instead of
16/// direct access to data members; this, too, is allowed, by making use of the
17/// callback mechanism within VDBT/BDTF.H. TProperty<T> can be constructed to
18/// take two callbacks, one returning T taking void (get), and the other returning
19/// void and taking a const T& (set). These methods will be called in place of
20/// accessing the m_content method of Property<>, below. Note that I'm assuming
21/// that if you write the 'get', you'll also write the 'set', and that both methods
22/// provide their own storage for T (instead of relying on m_content within the
23/// TProperty<T>).
24///
25/// Requirements; TProperty assumes that the type T has meaningful orthodox
26/// canonical form semantics (default ctor, copy ctor, assignment op, dtor)
27//
28//
29// Additions:
30// TRefProperty<>
31// TConstRefProperty<>
32// TObjProperty<>
33// TConstObjProperty<>
34// TFuncProperty<>
35// TFuncPropertyGet<>
36// TFuncPropertyPut<>
37// TFuncPropertyVarPut<>
38// TFuncPropertyIdxGet<>
39// TFuncPropertyIdxPut<>
40// TFuncPropertyIdx<>
41//
42//-------------------------------------------------------------------
43
44#if !defined(OWL_PROPERTY_H)
45#define OWL_PROPERTY_H
46
47#include <owl/private/defs.h>
48#if defined(BI_HAS_PRAGMA_ONCE)
49# pragma once
50#endif
51
52#include <owl/functor.h>
53
54namespace owl {
55
56//
57#include <owl/preclass.h>
58
59/// \addtogroup utility
60/// @{
61
62//------------------------------------------------------------------------------
63/// simple type/pointer
64template <class T>
66 // Object lifetime methods
67 //
68 public:
70 explicit TProperty(const T& t) : Content(t) {}
72
73 // Accessors
74 //
75 public:
76 T operator()() const { return Content; }
77#ifndef STRICT_PROPERTY_SYNTAX
78 operator T() { return Content; }
79#endif
80
81 // Mutators
82 //
83 public:
84 void operator()(const T& t) { Content = t; }
85#ifndef STRICT_PROPERTY_SYNTAX
86 TProperty<T>& operator=(const T& t) { Content = t; return *this; }
87#endif
88
89 // Disallowed methods
90 //
91 private:
92 void operator=(const TProperty<T>&);
93
94 // Internal Data
95 //
96 private:
97 T Content;
98};
99
100// simple type/pointer
101template <class T>
103 // Ctors & Dtors
104 //
105 public:
106 TConstProperty(const T& t) : Content(t) {}
108
109 // Accessor & Mutator
110 //
111 public:
112 T operator()() { return Content; }
113 const T operator()() const { return Content; }
114#ifndef STRICT_PROPERTY_SYNTAX
115 operator T() { return Content; }
116#endif
117
118 // Disallowed methods
119 //
120 private:
121 void operator=(const TConstProperty<T>&);
123
124 // Data members
125 //
126 private:
127 T Content;
128};
129
130//------------------------------------------------------------------------------
131// Additions made by Yura Bidus 1998
132//------------------------------------------------------------------------------
133/// Property container for object
134template <class T>
136 public:
137 explicit TObjProperty(const T& t);
141
142 public:
143 operator T*();
144 operator const T&() const;
146 const T& operator*();
147
148 T* operator=( T* t);
149 T& operator=( T& t);
151
152 const T& operator()() const;
153 void operator()(const T& t);
154
155 private:
156 void operator=(const TObjProperty<T>&);
157
158 private:
159 T* Content;
160};
161//------------------------------------------------------------------------------
162/// Read only Property container for object
163template <class T>
165 public:
166 TConstObjProperty(const T& t):Content(t){}
168
169 public:
170 T& operator()() { return Content; }
171 const T& operator()() const { return Content; }
172
173 operator T*() { return &Content;}
174 operator const T&() { return Content; }
175 T* operator->() { return &Content;}
176 const T& operator*() { return Content; }
177
178 private:
179 void operator=(const TConstObjProperty<T>&){}
180 TConstObjProperty(){}
181
182 private:
183 T& Content;
184};
185//------------------------------------------------------------------------------
186// array simple type
187template <class T,class I>
189 public:
190 class TSetter{
191 public:
193 operator T() { return Content; }
195 TSetter& operator=(const TSetter& val){Content=val.Content;return *this;}
196 protected:
199 friend class TSetter;
200
201 TArrayProperty(T* value) { Name.Content = value;}
202 TSetter& operator[]( I index) { Name.Index = index; return Name; }
203
204 private:
205 TArrayProperty<T,I>& operator=( const TArrayProperty<T,I>& ){;} // not allowed
206};
207//------------------------------------------------------------------------------
208/// array read only array
209template <class T,class I>
211 public:
212 class TGetter{
213#if defined(BI_COMP_GNUC)
214 friend class TConstArrayProperty;
215#else
216 friend TConstArrayProperty;
217#endif
218 public:
220
221 T& operator()() { return Content[Index]; }
222 const T& operator()() const { return Content[Index]; }
223 operator T*() { return &Content[Index];}
224 operator const T&() { return Content[Index]; }
225 const T& operator*() { return Content[Index]; }
226
227 protected:
230 friend class TGetter;
231
233 TGetter& operator[](I index) { Name.Index = index; return Name; }
234 private:
235 TConstArrayProperty<T,I>& operator=( const TConstArrayProperty<T,I>& ); // not allowed
236 T* Content;
237};
238//------------------------------------------------------------------------------
239/// array read only array
240template <class T,class I>
242{
243 public:
245 {
246 public:
248
250 { return Content[Index]; }
251 const T& operator()() const
252 { return Content[Index]; }
253 operator T*()
254 { return &Content[Index];}
255 operator const T&()
256 { return Content[Index]; }
258 { return &Content[Index];}
259 const T& operator*()
260 { return Content[Index]; }
261
262 protected:
265 friend class TGetter;
266
268 TGetter& operator[](I index) { Name.Index = index; return Name; }
269
270 private:
271 TConstObjArrayProperty<T,I>& operator=( const TConstObjArrayProperty<T,I>&){;} // not allowed
272};
273//------------------------------------------------------------------------------
274
275//
276// Functors
277//
278template <class T>
280 public:
282 :Get(get),Put(put){}
284
285 public:
286 T operator()() const { return Get(); }
287 operator T() { return Get(); }
288 void operator()(const T& t) { Put(t); }
289 TFuncProperty<T>& operator=(const T& t) { Put(t); return *this; }
290
291 private:
292 void operator=(const TFuncProperty<T>&){}
293 TFuncProperty(){}
294
295 TFunctor0<T> Get;
296 TFunctorV1<T> Put;
297};
298
299//
300// get/put object
301//
302template <class T>
304 public:
308
309 public:
310 // getters
311 operator T*() { return &Get(); }
312 operator const T&() const { return Get(); }
313 T* operator->() { return &Get();}
314 const T& operator*() { return Get(); }
315
316 //set/get
317 //T* operator=( T* t) { return Put(t); return &Get();}
318 T& operator=( T& t) { Put(t); return Get(); }
319 TFuncObjProperty<T>& operator=(const T& t){ Put(t); return *this; }
320 //setters
321 const T& operator()() const { return Get(); }
322 void operator()(const T& t) { Put(t);}
323
324 private:
325 void operator=(const TFuncObjProperty<T>&){}
326 TFuncObjProperty(){}
327
328 TFunctor0<T&> Get;
330
331};
332
333//
334template <class T>
336 public:
339
340 public:
341 T operator()() { return Get(); }
342 const T operator()() const { return Get(); }
343
344 private:
345 void operator=(const TFuncPropertyGet<T>&){}
346 TFuncPropertyGet(){}
347
348 TFunctor0<T> Get;
349};
350
351//
352template <class T>
354 public:
357
358 public:
359 T& operator()() { return Get(); }
360 const T& operator()() const { return Get(); }
361 operator T*() { return &Get(); }
362 operator const T&() { return Get(); }
363 T* operator->() { return &Get();}
364 const T& operator*() { return Get(); }
365
366 private:
367 void operator=(const TFuncObjPropertyGet<T>&){}
368 TFuncObjPropertyGet(){}
369
370 TFunctor0<T&> Get;
371};
372
373//
374template <class T>
376 public:
379
380 public:
381 void operator()(const T& t) { Put(t); }
382 TFuncPropertyPut<T>& operator=(const T& t) { Put(t); return *this; }
383
384 private:
385 void operator=(const TFuncPropertyPut<T>&){}
386 TFuncPropertyPut(){}
387
388 TFunctorV1<T> Put;
389};
390
391//
392template <class T>
394 public:
397
398 public:
399 void operator()(const T& t) { Put(t); }
400 TFuncObjPropertyPut<T>& operator=(const T& t) { Put(t); return *this; }
401
402 private:
403 void operator=(const TFuncObjPropertyPut<T>&){}
404 TFuncObjPropertyPut(){}
405
407};
408
409//
410template <class T>
412 public:
414 :Content(var),Put(put){}
416
417 public:
418 T operator()() const { return Content; }
419 operator T() { return Content; }
420
421 void operator()(const T& t) { Put(t); }
422 TFuncPropertyVarPut<T>& operator=(const T& t) { Put(t); return *this; }
423
424 private:
427
428 const T& Content;
429 TFunctorV1<T> Put;
430};
431
432
433//
434template <class T>
436 public:
438 :Content(&var),Put(put){}
440
441 public:
442 // getter
443 T& operator()() { return *Content; }
444 const T& operator()() const { return *Content; }
445 operator T*() { return Content; }
446 operator const T&() { return *Content; }
447 T* operator->() { return Content; }
448 const T& operator*() { return *Content; }
449
450 public:
451 //T* operator=( T* t) { return Put(t); return &Content;}
452 T& operator=( T& t) { Put(t); return *Content; }
453 TFuncObjPropertyVarPut<T>& operator=(const T& t) { Put(t); return *this; }
454 void operator()(const T& t){ Put(t);}
455
456 private:
458 TFuncObjPropertyVarPut(){}
459
461 T* Content;
462};
463
464//
465template <class T, int index>
467 public:
470
471 public:
472 T operator()() { return Get(index); }
473 const T operator()() const { return Get(index); }
474
475 private:
476 void operator=(const TFuncPropertyIdxGet<T,index>&){}
477 TFuncPropertyIdxGet(){}
478
480};
481
482//
483template <class T, int index>
485 public:
488
489 public:
490 T& operator()() { return Get(index); }
491 const T& operator()() const { return Get(index); }
492 operator T*() { return &Get(index);}
493 operator const T&() { return Get(index); }
494 T* operator->() { return &Get(index);}
495 const T& operator*() { return Get(index); }
496
497 private:
498 void operator=(const TFuncObjPropertyIdxGet<T,index>&){}
499 TFuncObjPropertyIdxGet(){}
500
502};
503
504//
505template <class T, int index>
507 public:
510
511 public:
512 void operator()(const T& t) { Put(index, t); }
513 TFuncPropertyIdxPut<T,index>& operator=(const T& t) { Put(index,t); return *this; }
514
515 private:
518
520};
521
522//
523template <class T, int index>
525 public:
528
529 public:
530 void operator()(const T& t) { Put(index, t); }
531 TFuncObjPropertyIdxPut<T,index>& operator=(const T& t) { Put(index,t); return *this; }
532
533 private:
536
538};
539
540//
541template <class T, int index>
543 public:
548
549 public:
550 T operator()() const { return Get(index); }
551 operator T() { return Get(index); }
552
553 void operator()(const T& t) { Put(index, t); }
554 TFuncPropertyIdx<T,index>& operator=(const T& t){Put(index,t);return *this;}
555
556 private:
559
562};
563
564//
565template <class T, int index>
567 public:
572
573 public:
574 // getters
575 operator T*() { return &Get(index);}
576 operator const T&() const { return Get(index); }
577 T* operator->() { return &Get(index);}
578 const T& operator*() { return Get(index); }
579
580 //set/get
581 //T* operator=( T* t) { Put(index,t); return &Get(index);}
582 T& operator=( T& t) { Put(index,t); return Get(index); }
583 TFuncObjPropertyIdx<T,index>& operator=(const T& t){ Put(index,t); return *this;}
584
585 //setters
586 const T& operator()() const { return Get(index); }
587 void operator()(const T& t) { Put(index,t);}
588
589 private:
591 TFuncObjPropertyIdx(){}
592
595};
596
597// support macros
598//
599#define GETFUNCTOR(T,func) \
600 Functor((TFunctor0<T>*)nullptr, func)
601#define PUTFUNCTOR(T,func) \
602 Functor((TFunctorV1<T>*)nullptr, func)
603#define GETPUTFUNCTOR(T,funcGet,funcPut) \
604 Functor((TFunctor0<T>*)nullptr, funcGet),\
605 Functor((TFunctorV1<T>*)nullptr, funcPut)
606//
607#define GETOBJFUNCTOR(T,func) \
608 Functor((TFunctor0<T&>*)nullptr, func)
609#define PUTOBJFUNCTOR(T,func) \
610 Functor((TFunctorV1<const T&>*)nullptr, func)
611#define GETPUTOBJFUNCTOR(T,funcGet,funcPut) \
612 Functor((TFunctor0<T&>*)nullptr, funcGet),\
613 Functor((TFunctorV1<const T&>*)nullptr, funcPut)
614//
615#define GETMFUNCTOR(T,type,memberFunc) \
616 Functor((TFunctor0<T>*)nullptr, *this, &type::memberFunc)
617#define PUTMFUNCTOR(T,type,memberFunc) \
618 Functor((TFunctorV1<T>*)nullptr, *this, &type::memberFunc)
619#define GETPUTMFUNCTOR(T,type,memberGet,memberPut) \
620 Functor((TFunctor0<T>*)nullptr, *this, &type::memberGet),\
621 Functor((TFunctorV1<T>*)nullptr, *this, &type::memberPut)
622
623#define GETOBJMFUNCTOR(T,type,memberFunc) \
624 Functor((TFunctor0<T&>*)nullptr, *this, &type::memberFunc)
625#define PUTOBJMFUNCTOR(T,type,memberFunc) \
626 Functor((TFunctorV1<const T&>*)nullptr, *this, &type::memberFunc)
627#define GETPUTOBJMFUNCTOR(T,type,memberGet,memberPut) \
628 Functor((TFunctor0<T&>*)nullptr, *this, &type::memberGet),\
629 Functor((TFunctorV1<const T&>*)nullptr, *this, &type::memberPut)
630//
631#define IDXGETFUNCTOR(T,func) \
632 Functor((TFunctor1<T,int>*)nullptr, func)
633#define IDXPUTFUNCTOR(T,func) \
634 Functor((TFunctorV2<int,T>*)nullptr, func)
635#define IDXGETPUTFUNCTOR(T,funcGet,funcPut) \
636 Functor((TFunctor1<T,int>*)nullptr, funcGet),\
637 Functor((TFunctorV2<int,T>*)nullptr, funcPut)
638#define IDXGETPUTOBJFUNCTOR(T,funcGet,funcPut) \
639 Functor((TFunctor1<T&,int>*)nullptr, funcGet),\
640 Functor((TFunctorV2<int,const T&>*)nullptr, funcPut)
641//
642#define IDXGETMFUNCTOR(T,type,memberFunc) \
643 Functor((TFunctor1<T,int>*)nullptr, *this, &type::memberFunc)
644#define IDXPUTMFUNCTOR(T,type,memberFunc) \
645 Functor((TFunctorV2<int,T>*)nullptr, *this, &type::memberFunc)
646#define IDXGETPUTMFUNCTOR(T,type,memberGet,memberPut) \
647 Functor((TFunctor1<T,int>*)nullptr, *this, &type::memberGet),\
648 Functor((TFunctorV2<int,T>*)nullptr, *this, &type::memberPut)
649#define IDXGETPUTOBJMFUNCTOR(T,type,memberGet,memberPut) \
650 Functor((TFunctor1<T,int>*)nullptr, *this, &type::memberGet),\
651 Functor((TFunctorV2<int,const T&>*)nullptr, *this, &type::memberPut)
652
653/// @}
654
655#include <owl/posclass.h>
656
657//
658// inlines
659//
660
661// TObjProperty inlines
662//
663template <class T> inline
665{
666 Content = new T(t);
667}
668
669template <class T> inline
671:
672 Content(t)
673{
674}
675
676//
677template <class T> inline
679:
680 Content(nullptr)
681{
682}
683
684//
685template <class T> inline
687{
688 delete Content;
689}
690
691//
692template <class T> inline
694{
695 return Content;
696}
697
698//
699template <class T> inline
701{
702 PRECONDITION(Content);
703 return *Content;
704}
705
706//
707template <class T> inline T*
709{
710 return Content;
711}
712
713//
714template <class T> inline const T&
716{
717 PRECONDITION(Content);
718 return *Content;
719}
720
721//
722template <class T> inline T*
724{
725 delete Content;
726 Content = t;
727 return Content;
728}
729
730//
731template <class T> inline T&
733{
734 delete Content;
735 Content = new T(t);
736 return *Content;
737}
738
739//
740template <class T> inline const T&
742{
743 PRECONDITION(Content);
744 return *Content;
745}
746
747//
748template <class T> inline void
750{
751 delete Content;
752 Content = new T(t);
753}
754
755//
756template <class T> inline TObjProperty<T>&
758{
759 delete Content;
760 Content = new T(t);
761 return *this;
762}
763
764} // OWL namespace
765
766//------------------------------------------------------------------------------
767#endif
#define PRECONDITION(condition)
Definition checks.h:227
TSetter & operator=(const TSetter &val)
Definition property.h:195
TSetter & operator[](I index)
Definition property.h:202
TArrayProperty(T *value)
Definition property.h:201
class owl::TArrayProperty::TSetter Name
array read only array
Definition property.h:210
TConstArrayProperty(T *value)
Definition property.h:232
TGetter & operator[](I index)
Definition property.h:233
class owl::TConstArrayProperty::TGetter Name
array read only array
Definition property.h:242
TGetter & operator[](I index)
Definition property.h:268
class owl::TConstObjArrayProperty::TGetter Name
Read only Property container for object.
Definition property.h:164
const T & operator*()
Definition property.h:176
TConstObjProperty(const T &t)
Definition property.h:166
const T & operator()() const
Definition property.h:171
TConstProperty(const T &t)
Definition property.h:106
const T operator()() const
Definition property.h:113
const T & operator()() const
Definition property.h:360
TFuncObjPropertyGet(const TFunctor0< T & > get)
Definition property.h:355
const T & operator()() const
Definition property.h:321
TFuncObjProperty< T > & operator=(const T &t)
Definition property.h:319
TFuncObjProperty(const TFunctor0< T & > get, const TFunctorV1< const T & > put)
Definition property.h:305
const T & operator*()
Definition property.h:314
void operator()(const T &t)
Definition property.h:322
const T & operator()() const
Definition property.h:491
TFuncObjPropertyIdxGet(const TFunctor1< T &, int > get)
Definition property.h:486
TFuncObjPropertyIdx(const TFunctor1< T &, int > get, const TFunctorV2< int, const T & > put)
Definition property.h:568
void operator()(const T &t)
Definition property.h:587
const T & operator()() const
Definition property.h:586
TFuncObjPropertyIdx< T, index > & operator=(const T &t)
Definition property.h:583
void operator()(const T &t)
Definition property.h:530
TFuncObjPropertyIdxPut< T, index > & operator=(const T &t)
Definition property.h:531
TFuncObjPropertyIdxPut(const TFunctorV2< int, const T & > put)
Definition property.h:526
TFuncObjPropertyPut< T > & operator=(const T &t)
Definition property.h:400
TFuncObjPropertyPut(const TFunctorV1< const T & > put)
Definition property.h:395
void operator()(const T &t)
Definition property.h:399
TFuncObjPropertyVarPut(T &var, const TFunctorV1< const T & > put)
Definition property.h:437
TFuncObjPropertyVarPut< T > & operator=(const T &t)
Definition property.h:453
const T & operator()() const
Definition property.h:444
void operator()(const T &t)
Definition property.h:454
const T operator()() const
Definition property.h:342
TFuncPropertyGet(const TFunctor0< T > get)
Definition property.h:337
void operator()(const T &t)
Definition property.h:288
TFuncProperty(const TFunctor0< T > get, const TFunctorV1< T > put)
Definition property.h:281
T operator()() const
Definition property.h:286
TFuncProperty< T > & operator=(const T &t)
Definition property.h:289
TFuncPropertyIdxGet(const TFunctor1< T, int > get)
Definition property.h:468
const T operator()() const
Definition property.h:473
TFuncPropertyIdx(const TFunctor1< T, int > get, const TFunctorV2< int, T > put)
Definition property.h:544
TFuncPropertyIdx< T, index > & operator=(const T &t)
Definition property.h:554
void operator()(const T &t)
Definition property.h:553
TFuncPropertyIdxPut(const TFunctorV2< int, T > put)
Definition property.h:508
void operator()(const T &t)
Definition property.h:512
TFuncPropertyIdxPut< T, index > & operator=(const T &t)
Definition property.h:513
TFuncPropertyPut< T > & operator=(const T &t)
Definition property.h:382
void operator()(const T &t)
Definition property.h:381
TFuncPropertyPut(const TFunctorV1< T > put)
Definition property.h:377
TFuncPropertyVarPut(const T &var, const TFunctorV1< T > put)
Definition property.h:413
void operator()(const T &t)
Definition property.h:421
TFuncPropertyVarPut< T > & operator=(const T &t)
Definition property.h:422
Property container for object.
Definition property.h:135
const T & operator*()
Definition property.h:715
TObjProperty(const T &t)
Definition property.h:664
TObjProperty< T > & operator=(const T &t)
Definition property.h:757
const T & operator()() const
Definition property.h:741
T & operator=(T &t)
Definition property.h:732
void operator()(const T &t)
Definition property.h:749
T * operator=(T *t)
Definition property.h:723
simple type/pointer
Definition property.h:65
TProperty< T > & operator=(const T &t)
Definition property.h:86
TProperty(const T &t)
Definition property.h:70
void operator()(const T &t)
Definition property.h:84
T operator()() const
Definition property.h:76
C++ Functor template implementation.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22