HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_ContextSpecificTable.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  * NAME: RE_ContextSpecificTable.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Contains context-specific objects, creating them as needed for new
10  * GL contexts.
11  */
12 #ifndef RE_ContextSpecificTable_h
13 #define RE_ContextSpecificTable_h
14 
15 #include "RE_API.h"
17 #include "RE_Render.h"
18 #include "RE_Server.h"
19 
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_ValArray.h>
22 
23 template <class CObj>
25 {
26 public:
29  {
30  for(int i=0; i<myObjects.entries(); i++)
31  if(myObjects(i))
32  {
33  // avoid an attempt to clear the GL object, since deleting
34  // the table implies we're exiting and contexts are not
35  // likely to be valid. Use clear() if you need to free
36  // the GL objects themselves.
38  myObjects(i)->clearContext();
39  delete myObjects(i);
40  }
41  }
43 
44  CObj *get(RE_Render *r)
45  {
46  int id = r->getContextID();
47  RE_ContextSpecificObject *obj = myObjects.forcedRef(id);
48  if(!obj)
49  {
50  obj = myObjects(id) = new CObj;
51  obj->init(r);
52  }
53 
54  return static_cast<CObj *>(obj);
55  }
56  CObj *peek(RE_Render *r)
57  {
58  int id = r->getContextID();
59  if(id < myObjects.entries())
60  return static_cast<CObj *>(myObjects(id));
61  return nullptr;
62  }
63 
64  void free(RE_Render *r)
65  {
66  int id = r->getContextID();
67 
68  if(id >= myObjects.entries())
69  return;
70 
71  delete myObjects(id);
72  myObjects(id) = nullptr;
73  }
74 
75  void clear()
76  {
77  for(int i=0; i<myObjects.entries(); i++)
78  {
79  delete myObjects(i);
80  myObjects(i) = nullptr;
81  }
82  myObjects.entries(0);
83  }
84 
85  /// Returns the amount of main memory (NOT graphics memory!)
86  /// owned by this RE_ContextSpecificTable.
87  int64 getMemoryUsage(bool inclusive) const
88  {
89  int64 mem = inclusive ? sizeof(*this) : 0;
90  mem += myObjects.getMemoryUsage(false);
91  return mem;
92  }
93 
94 private:
96 
97  friend class RE_OGLRender;
98 };
99 
100 #endif
virtual bool init(RE_Render *r)=0
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:664
int getContextID() const
Definition: RE_OGLRender.h:117
int64 getMemoryUsage(bool inclusive) const
static bool isAppExiting()
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:655
GLboolean r
Definition: glcorearb.h:1222
T & forcedRef(exint i)
Definition: UT_Array.h:808