HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  UT_FunctorImpl<void> *clone() const override
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  void operator()() const override
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 
75  UT_FunctorImpl1<void, P1> *clone() const override
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  void operator()(Parm1Type parm1) const override
81  { ((*myPointerToObj).*myPointerToMemFn)(parm1); }
82 
83  int64 getMemoryUsage(bool inclusive) const override
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  void operator()(Parm1Type parm1, Parm2Type parm2) const override
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:
129  typedef void ReturnType;
130  typedef P1 Parm1Type;
131 
133 
134  explicit UT_Functor1(
135  UT_FunctorImpl1<void, Parm1Type> *functor_implementation)
136  : myFunctorImpl(functor_implementation)
137  {}
138 
140  {
141  myFunctorImpl.myFunctorImpl = src.myFunctorImpl.myFunctorImpl
142  ? src.myFunctorImpl.myFunctorImpl->clone() : nullptr;
143  }
144 
145  template <typename FunctorType>
146  UT_Functor1(FunctorType functor)
147  : myFunctorImpl(new UT_FunctorHandler1<UT_Functor1, FunctorType>(functor))
148  {}
149 
150  template <typename PointerToObj, typename PointerToMemFn>
151  UT_Functor1(const PointerToObj &pointer, PointerToMemFn method)
152  : myFunctorImpl(new UT_MemFunHandler1<UT_Functor1, PointerToObj,
153  PointerToMemFn>(pointer, method))
154  {}
155 
157  {
158  delete myFunctorImpl.myFunctorImpl;
159  myFunctorImpl.myFunctorImpl = 0;
160  }
161 
163  {
164  if (&functor != this)
165  {
166  delete myFunctorImpl.myFunctorImpl;
167  myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
168  ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
169  }
170  return *this;
171  }
172 
173  // There is no "return" here, because Irix doesn't support it.
174  void operator()(Parm1Type parm1) const
175  {
176  UT_ASSERT(myFunctorImpl.myFunctorImpl);
177  (*myFunctorImpl.myFunctorImpl)(parm1);
178  }
179 
180  bool isSet() const
181  { return myFunctorImpl.myFunctorImpl != 0; }
182 
183  int64 getMemoryUsage(bool inclusive) const
184  {
185  int64 mem = inclusive ? sizeof(*this) : 0;
186  if (myFunctorImpl.myFunctorImpl)
187  mem += myFunctorImpl.myFunctorImpl->getMemoryUsage(true);
188  return mem;
189  }
190 
191 private:
192  class Helper
193  {
194  public:
195  Helper() : myFunctorImpl(0) {}
196 
197  // Clone the other functor's implementation in the copy constructor.
198  Helper(const Helper &helper)
199  { myFunctorImpl = helper.myFunctorImpl
200  ? helper.myFunctorImpl->clone() : 0; }
201 
202  explicit Helper(
203  UT_FunctorImpl1<void, Parm1Type> *functor_implementation)
204  : myFunctorImpl(functor_implementation) {}
205 
206  template<typename U>
207  explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
208 
209  UT_FunctorImpl1<void, Parm1Type> *myFunctorImpl;
210  };
211 
212  Helper myFunctorImpl;
213 };
214 
215 // ---------------------------------------------------------------------------
216 
217 template <typename P1, typename P2>
218 class UT_Functor2<void, P1, P2>
219 {
220 public:
222  typedef void ReturnType;
223  typedef P1 Parm1Type;
224  typedef P2 Parm2Type;
225 
227 
228  explicit UT_Functor2(
230  : myFunctorImpl(functor_impl)
231  {}
232 
234  {
235  myFunctorImpl.myFunctorImpl = src.myFunctorImpl.myFunctorImpl
236  ? src.myFunctorImpl.myFunctorImpl->clone() : nullptr;
237  }
238 
239  template <typename FunctorType>
240  UT_Functor2(FunctorType functor)
241  : myFunctorImpl(new UT_FunctorHandler2<UT_Functor2, FunctorType>(functor))
242  {}
243 
244  template <typename PointerToObj, typename PointerToMemFn>
245  UT_Functor2(const PointerToObj &pointer, PointerToMemFn method)
246  : myFunctorImpl(new UT_MemFunHandler2<UT_Functor2, PointerToObj,
247  PointerToMemFn>(pointer, method))
248  {}
249 
251  {
252  delete myFunctorImpl.myFunctorImpl;
253  myFunctorImpl.myFunctorImpl = 0;
254  }
255 
257  {
258  if (&functor != this)
259  {
260  delete myFunctorImpl.myFunctorImpl;
261  myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
262  ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
263  }
264  return *this;
265  }
266 
267  // There is no "return" here, because Irix doesn't support it.
268  void operator()(Parm1Type parm1, Parm2Type parm2) const
269  {
270  UT_ASSERT(myFunctorImpl.myFunctorImpl);
271  (*myFunctorImpl.myFunctorImpl)(parm1, parm2);
272  }
273 
274  bool isSet() const
275  { return myFunctorImpl.myFunctorImpl != 0; }
276 
277 private:
278  class Helper
279  {
280  public:
281  Helper() : myFunctorImpl(0) {}
282 
283  // Clone the other functor's implementation in the copy constructor.
284  Helper(const Helper &helper)
285  { myFunctorImpl = helper.myFunctorImpl
286  ? helper.myFunctorImpl->clone() : 0; }
287 
288  explicit Helper(
290  : myFunctorImpl(functor_impl) {}
291 
292  template<typename U>
293  explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
294 
296  };
297 
298  Helper myFunctorImpl;
299 };
300 
301 #endif
UT_Functor2(FunctorType functor)
Definition: UT_Functor.C:240
void operator()(Parm1Type parm1, Parm2Type parm2) const override
Definition: UT_Functor.C:116
void
Definition: png.h:1083
UT_Functor1(FunctorType functor)
Definition: UT_Functor.C:146
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:245
UT_Functor2(UT_FunctorImpl2< void, Parm1Type, Parm2Type > *functor_impl)
Definition: UT_Functor.C:228
UT_Functor2 & operator=(const UT_Functor2 &functor)
Definition: UT_Functor.C:256
UT_Functor1(const PointerToObj &pointer, PointerToMemFn method)
Definition: UT_Functor.C:151
UT_Functor1(UT_FunctorImpl1< void, Parm1Type > *functor_implementation)
Definition: UT_Functor.C:134
UT_MemFunHandler(const PointerToObj &pointer_to_obj, const PointerToMemFn pointer_to_mem_fn)
Definition: UT_Functor.C:42
UT_FunctorImpl1< void, P1 > * clone() const override
Definition: UT_Functor.C:75
long long int64
Definition: SYS_Types.h:116
UT_Functor1(const this_type &src)
Definition: UT_Functor.C:139
UT_Functor2(const this_type &src)
Definition: UT_Functor.C:233
void operator()(Parm1Type parm1, Parm2Type parm2) const
Definition: UT_Functor.C:268
void operator()(Parm1Type parm1) const
Definition: UT_Functor.C:174
GLenum void ** pointer
Definition: glcorearb.h:810
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Functor.C:183
bool isSet() const
Definition: UT_Functor.C:180
auto ptr(T p) -> const void *
Definition: format.h:2448
UT_MemFunHandler2(const PointerToObj &pointer_to_obj, const PointerToMemFn pointer_to_mem_fn)
Definition: UT_Functor.C:106
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
UT_FunctorImpl2< void, Parm1Type, Parm2Type > * clone() const override
Definition: UT_Functor.C:111
UT_Functor1 & operator=(const UT_Functor1 &functor)
Definition: UT_Functor.C:162
ReturnType operator()() const
Definition: UT_Functor.h:363
GLenum src
Definition: glcorearb.h:1793