HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Functor.C
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Functor.C
7  *
8  * This file contains template specializations for UT_Functor. They
9  * cannot go into UT_Functor.h because compilers that don't use the
10  * Borland template compilation model will fail because of multiply
11  * defined symbols.
12  */
13 
14 #ifndef __UT_Functor_C__
15 #define __UT_Functor_C__
16 
17 #include "UT_Functor.h"
18 
19 // It's okay for a functor with a void return type to not do anything if
20 // it was never initialized. This can happen if you just declare a
21 // functor with the default constructor but never initialize it.
22 template <>
23 inline void
25 {
26  if (myFunctorImpl.myFunctorImpl)
27  (*myFunctorImpl.myFunctorImpl)();
28 }
29 
30 // ---------------------------------------------------------------------------
31 
32 // Irix doesn't let you use "return <expression>" when <expression>'s type is
33 // void. We can't partially specialize member functions, so instead we have
34 // to partially specialize the classes to when a void return type is used.
35 template <typename PointerToObj, typename PointerToMemFn>
36 class UT_MemFunHandler<UT_Functor<void>, PointerToObj, PointerToMemFn>
37  : public UT_FunctorImpl<void>
38 {
39 public:
40  typedef void ReturnType;
41 
42  UT_MemFunHandler(const PointerToObj &pointer_to_obj,
43  const PointerToMemFn pointer_to_mem_fn)
44  : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
45  {}
46 
47  virtual UT_FunctorImpl<void> *clone() const
48  { return new UT_MemFunHandler<UT_Functor<void>, PointerToObj,
49  PointerToMemFn>(*this); }
50 
51  // There is no "return" here, because Irix doesn't support it.
52  virtual void operator()() const
53  { ((*myPointerToObj).*myPointerToMemFn)(); }
54 
55 private:
56  PointerToObj myPointerToObj;
57  PointerToMemFn myPointerToMemFn;
58 };
59 
60 // ---------------------------------------------------------------------------
61 
62 template <typename P1, typename PointerToObj, typename PointerToMemFn>
63 class UT_MemFunHandler1<UT_Functor1<void, P1>, PointerToObj, PointerToMemFn>
64  : public UT_FunctorImpl1<void, P1>
65 {
66 public:
67  typedef void ReturnType;
68  typedef P1 Parm1Type;
69 
70  UT_MemFunHandler1(const PointerToObj &pointer_to_obj,
71  const PointerToMemFn pointer_to_mem_fn)
72  : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
73  {}
74 
76  { return new UT_MemFunHandler1<UT_Functor1<void, P1>, PointerToObj,
77  PointerToMemFn>(*this); }
78 
79  // There is no "return" here, because Irix doesn't support it.
80  virtual void operator()(Parm1Type parm1) const
81  { ((*myPointerToObj).*myPointerToMemFn)(parm1); }
82 
83  virtual int64 getMemoryUsage(bool inclusive) const
84  {
85  int64 mem = inclusive ? sizeof(*this) : 0;
86  return mem;
87  }
88 
89 private:
90  PointerToObj myPointerToObj;
91  PointerToMemFn myPointerToMemFn;
92 };
93 
94 // ---------------------------------------------------------------------------
95 
96 template <typename P1, typename P2,
97  typename PointerToObj, typename PointerToMemFn>
98 class UT_MemFunHandler2<UT_Functor2<void, P1, P2>, PointerToObj, PointerToMemFn>
99  : public UT_FunctorImpl2<void, P1, P2>
100 {
101 public:
102  typedef void ReturnType;
103  typedef P1 Parm1Type;
104  typedef P2 Parm2Type;
105 
106  UT_MemFunHandler2(const PointerToObj &pointer_to_obj,
107  const PointerToMemFn pointer_to_mem_fn)
108  : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
109  {}
110 
112  { return new UT_MemFunHandler2<UT_Functor2<void, P1, P2>, PointerToObj,
113  PointerToMemFn>(*this); }
114 
115  // There is no "return" here, because Irix doesn't support it.
116  virtual void operator()(Parm1Type parm1, Parm2Type parm2) const
117  { ((*myPointerToObj).*myPointerToMemFn)(parm1, parm2); }
118 
119 private:
120  PointerToObj myPointerToObj;
121  PointerToMemFn myPointerToMemFn;
122 };
123 
124 template <typename P1>
125 class UT_Functor1<void, P1>
126 {
127 public:
128  typedef void ReturnType;
129  typedef P1 Parm1Type;
130 
132 
133  explicit UT_Functor1(
134  UT_FunctorImpl1<void, Parm1Type> *functor_implementation)
135  : myFunctorImpl(functor_implementation)
136  {}
137 
138  template <typename FunctorType>
139  UT_Functor1(FunctorType functor)
140  : myFunctorImpl(new UT_FunctorHandler1<UT_Functor1, FunctorType>(functor))
141  {}
142 
143  template <typename PointerToObj, typename PointerToMemFn>
144  UT_Functor1(const PointerToObj &pointer, PointerToMemFn method)
145  : myFunctorImpl(new UT_MemFunHandler1<UT_Functor1, PointerToObj,
146  PointerToMemFn>(pointer, method))
147  {}
148 
150  {
151  delete myFunctorImpl.myFunctorImpl;
152  myFunctorImpl.myFunctorImpl = 0;
153  }
154 
156  {
157  if (&functor != this)
158  {
159  delete myFunctorImpl.myFunctorImpl;
160  myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
161  ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
162  }
163  return *this;
164  }
165 
166  // There is no "return" here, because Irix doesn't support it.
167  void operator()(Parm1Type parm1) const
168  {
169  UT_ASSERT(myFunctorImpl.myFunctorImpl);
170  (*myFunctorImpl.myFunctorImpl)(parm1);
171  }
172 
173  bool isSet() const
174  { return myFunctorImpl.myFunctorImpl != 0; }
175 
176  int64 getMemoryUsage(bool inclusive) const
177  {
178  int64 mem = inclusive ? sizeof(*this) : 0;
179  if (myFunctorImpl.myFunctorImpl)
180  mem += myFunctorImpl.myFunctorImpl->getMemoryUsage(true);
181  return mem;
182  }
183 
184 private:
185  class Helper
186  {
187  public:
188  Helper() : myFunctorImpl(0) {}
189 
190  // Clone the other functor's implementation in the copy constructor.
191  Helper(const Helper &helper)
192  { myFunctorImpl = helper.myFunctorImpl
193  ? helper.myFunctorImpl->clone() : 0; }
194 
195  explicit Helper(
196  UT_FunctorImpl1<void, Parm1Type> *functor_implementation)
197  : myFunctorImpl(functor_implementation) {}
198 
199  template<typename U>
200  explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
201 
202  UT_FunctorImpl1<void, Parm1Type> *myFunctorImpl;
203  };
204 
205  Helper myFunctorImpl;
206 };
207 
208 // ---------------------------------------------------------------------------
209 
210 template <typename P1, typename P2>
211 class UT_Functor2<void, P1, P2>
212 {
213 public:
214  typedef void ReturnType;
215  typedef P1 Parm1Type;
216  typedef P2 Parm2Type;
217 
219 
220  explicit UT_Functor2(
222  : myFunctorImpl(functor_impl)
223  {}
224 
225  template <typename FunctorType>
226  UT_Functor2(FunctorType functor)
227  : myFunctorImpl(new UT_FunctorHandler2<UT_Functor2, FunctorType>(functor))
228  {}
229 
230  template <typename PointerToObj, typename PointerToMemFn>
231  UT_Functor2(const PointerToObj &pointer, PointerToMemFn method)
232  : myFunctorImpl(new UT_MemFunHandler2<UT_Functor2, PointerToObj,
233  PointerToMemFn>(pointer, method))
234  {}
235 
237  {
238  delete myFunctorImpl.myFunctorImpl;
239  myFunctorImpl.myFunctorImpl = 0;
240  }
241 
243  {
244  if (&functor != this)
245  {
246  delete myFunctorImpl.myFunctorImpl;
247  myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
248  ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
249  }
250  return *this;
251  }
252 
253  // There is no "return" here, because Irix doesn't support it.
254  void operator()(Parm1Type parm1, Parm2Type parm2) const
255  {
256  UT_ASSERT(myFunctorImpl.myFunctorImpl);
257  (*myFunctorImpl.myFunctorImpl)(parm1, parm2);
258  }
259 
260  bool isSet() const
261  { return myFunctorImpl.myFunctorImpl != 0; }
262 
263 private:
264  class Helper
265  {
266  public:
267  Helper() : myFunctorImpl(0) {}
268 
269  // Clone the other functor's implementation in the copy constructor.
270  Helper(const Helper &helper)
271  { myFunctorImpl = helper.myFunctorImpl
272  ? helper.myFunctorImpl->clone() : 0; }
273 
274  explicit Helper(
276  : myFunctorImpl(functor_impl) {}
277 
278  template<typename U>
279  explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
280 
282  };
283 
284  Helper myFunctorImpl;
285 };
286 
287 #endif
UT_Functor2(FunctorType functor)
Definition: UT_Functor.C:226
png_voidp ptr
Definition: png.h:2145
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
virtual void operator()(Parm1Type parm1, Parm2Type parm2) const
Definition: UT_Functor.C:116
UT_Functor1(FunctorType functor)
Definition: UT_Functor.C:139
UT_MemFunHandler1(const PointerToObj &pointer_to_obj, const PointerToMemFn pointer_to_mem_fn)
Definition: UT_Functor.C:70
UT_Functor2(const PointerToObj &pointer, PointerToMemFn method)
Definition: UT_Functor.C:231
UT_Functor2(UT_FunctorImpl2< void, Parm1Type, Parm2Type > *functor_impl)
Definition: UT_Functor.C:220
UT_Functor2 & operator=(const UT_Functor2 &functor)
Definition: UT_Functor.C:242
virtual UT_FunctorImpl1< void, P1 > * clone() const
Definition: UT_Functor.C:75
UT_Functor1(const PointerToObj &pointer, PointerToMemFn method)
Definition: UT_Functor.C:144
UT_Functor1(UT_FunctorImpl1< void, Parm1Type > *functor_implementation)
Definition: UT_Functor.C:133
long long int64
Definition: SYS_Types.h:100
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
UT_MemFunHandler(const PointerToObj &pointer_to_obj, const PointerToMemFn pointer_to_mem_fn)
Definition: UT_Functor.C:42
int method
Definition: png.h:1924
void operator()(Parm1Type parm1, Parm2Type parm2) const
Definition: UT_Functor.C:254
void operator()(Parm1Type parm1) const
Definition: UT_Functor.C:167
GLenum void ** pointer
Definition: glcorearb.h:809
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Functor.C:176
bool isSet() const
Definition: UT_Functor.C:173
virtual UT_FunctorImpl2< void, Parm1Type, Parm2Type > * clone() const
Definition: UT_Functor.C:111
UT_MemFunHandler2(const PointerToObj &pointer_to_obj, const PointerToMemFn pointer_to_mem_fn)
Definition: UT_Functor.C:106
UT_Functor1 & operator=(const UT_Functor1 &functor)
Definition: UT_Functor.C:155
ReturnType operator()() const
Definition: UT_Functor.h:343