HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PRM_AutoDeleter.h
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  * COMMENTS:
7  * This class provides a way to track a set of objects that were allocated
8  * and then delete them later on. It is particularly useful for deleting
9  * elements pointed to by a PRM_Template.
10  *
11  * Note that the objects will be deleted in the *same* order they are
12  * added to the PRM_AutoDeleter.
13  *
14  * Sample usage:
15  *
16  * static void
17  * deleteDefaults(PRM_Default *defaults)
18  * {
19  * for (int i=0; i<defaults->getVectorSize(); ++i)
20  * defaults[i].setString(NULL, true); // free existing string
21  * delete defaults;
22  * }
23  *
24  * PRM_Template *
25  * newTemplate(PRM_AutoDeleter &deleter)
26  * {
27  * return deleter.append(new PRM_Template(
28  * ...,
29  * deleter.append(new PRM_Range(...)),
30  * deleter.appendCallback(
31  * deleteDefaults, new PRM_Default(...)),
32  * ...));
33  * }
34  *
35  * void
36  * foo()
37  * {
38  * PRM_AutoDeleter deleter;
39  * PRM_Template *prm_template = newTemplate(deleter);
40  *
41  * ...do something with prm_template...
42  *
43  * // When "deleter" is destroyed, it will delete the PRM_Template
44  * // and the things earlier allocated.
45  * }
46  *
47  * Note that if you want a callback to take additional arguments, you
48  * can use hboost::bind.
49  */
50 
51 #ifndef __PRM_AutoDeleter_h__
52 #define __PRM_AutoDeleter_h__
53 
54 #include "PRM_API.h"
55 #include <UT/UT_NonCopyable.h>
56 #include <UT/UT_SharedPtr.h>
57 #include <UT/UT_SharedArray.h>
58 #include <hboost/function.hpp>
59 #include <vector>
60 
62 {
63 private:
64  // This base class exists so we can have an array of different types of
65  // deleters.
66  class prm_Deleter : public UT_NonCopyable
67  {
68  public:
69  virtual ~prm_Deleter()
70  {}
71  };
72 
73  // This templated class calls delete on a pointer.
74  template <typename T>
75  class prm_ValueDeleter : public prm_Deleter
76  {
77  public:
78  prm_ValueDeleter(T *value)
79  : mySharedPointer(value)
80  {}
81  private:
82  UT_SharedPtr<T> mySharedPointer;
83  };
84 
85  // This templated class calls delete [] on a pointer.
86  template <typename T>
87  class prm_ArrayDeleter : public prm_Deleter
88  {
89  public:
90  prm_ArrayDeleter(T *array)
91  : mySharedArray(array)
92  {}
93  private:
94  UT_SharedArray<T> mySharedArray;
95  };
96 
97  // This templated class runs a callback with a pointer. It is up to
98  // the callback to call delete if it wants to.
99  template <typename T>
100  class prm_CallbackDeleter : public prm_Deleter
101  {
102  public:
103  prm_CallbackDeleter(hboost::function<void (T *)> callback, T *value)
104  : myCallback(callback)
105  , myValue(value)
106  {}
107 
108  virtual ~prm_CallbackDeleter()
109  { myCallback(myValue); }
110 
111  private:
112  hboost::function<void (T *)> myCallback;
113  T *myValue;
114  };
115 
116 public:
117  // Note that we want objects to be deleted in the order they're added
118  // to the std::vector, so we purposely append them to the end.
119 
120  template <typename T>
122  {
123  if (!value)
124  return NULL;
125 
126  myDeleters.push_back(UT_SharedPtr<prm_Deleter>(
127  new prm_ValueDeleter<T>(value)));
128  return value;
129  }
130 
131  template <typename T>
132  T *appendArray(T *array)
133  {
134  if (!array)
135  return NULL;
136 
137  myDeleters.push_back(UT_SharedPtr<prm_Deleter>(
138  new prm_ArrayDeleter<T>(array)));
139  return array;
140  }
141 
142  // Note that appendCallback does not delete the pointer. It's up to you
143  // to delete it from within the callback if that's what you want.
144  template <typename T>
145  T *appendCallback(hboost::function<void (T *)> callback, T *pointer)
146  {
147  if (!pointer)
148  return NULL;
149 
150  myDeleters.push_back(UT_SharedPtr<prm_Deleter>(
151  new prm_CallbackDeleter<T>(callback, pointer)));
152  return pointer;
153  }
154 
155 private:
156  std::vector<UT_SharedPtr<prm_Deleter> > myDeleters;
157 };
158 
159 #endif
Wrapper around hboost::shared_ptr.
Definition: UT_SharedPtr.h:27
A shared ptr for encapsulating dynamically allocated arrays.
T * appendArray(T *array)
GLsizei const GLfloat * value
Definition: glcorearb.h:823
GLenum void ** pointer
Definition: glcorearb.h:809
T * append(T *value)
T * appendCallback(hboost::function< void(T *)> callback, T *pointer)