LightWrap++
AC++wrapperfortheLightWave3DSDK
global.h
Go to the documentation of this file.
1 #ifndef LWPP_GLOBAL_H
2 #define LWPP_GLOBAL_H
3 #include <lwpp/debug.h>
4 #include <lwrender.h>
5 #include <lwglobsrv.h>
6 #include <lwpp/lw_version.h>
7 #include <lwpp/lw_server.h>
8 #include <lwpp/exception.h>
9 #include <lwhost.h>
10 
11 #ifndef NULL
12 #define NULL 0
13 #endif // 0
14 
15 #if defined(_MSC_VER)
16 // C4661: '...' : no suitable definition provided for explicit template instantiation request
17 #pragma warning(disable: 4661)
18 #endif
19 
20 #define TOKENIZE(x) x
21 
22 #define IMPLEMENT_GLOBAL(base, name) \
23 template<> base * GlobalBase< base >::globPtr = 0; \
24 template<> size_t GlobalBase< base >::usage_count = 0; \
25 template<> const char * GlobalBase< base >::m_globalName = name;
26 
27 #define IMPLEMENT_NAMED_GLOBAL(base, name) \
28 template<> base * nGlobalBase< base, name>::globPtr = 0; \
29 template<> size_t nGlobalBase< base, name>::usage_count = 0;
30 
32 namespace lwpp
33 {
34  // LW version information
36  class LightWave
37  {
38  static unsigned int systemID;
39  static unsigned int productInfo;
40  public:
41  void static Init(void);
43  unsigned int static Context(void)
44  {
45  return (systemID & LWSYS_TYPEBITS);
46  }
48  unsigned int static DongleID(void)
49  {
50  Init();
51  #define LWSYS_NEW_SERIALBITS 0x0000FFFF // redefine, this seems to be a bug in the SDK ... or modeler
52  return (systemID & LWSYS_NEW_SERIALBITS);
53  //return (systemID & LWSYS_SERIALBITS);
54  }
56  bool static isLayout()
57  {
58  return (Context() == LWSYS_LAYOUT);
59  }
61  bool static isModeler()
62  {
63  return (Context() == LWSYS_MODELER);
64  }
66  bool static isScreamernet()
67  {
68  return (Context() == LWSYS_SCREAMERNET);
69  }
71  unsigned int static Major()
72  {
73  return LWINF_GETMAJOR(productInfo);
74  }
76  unsigned int static Minor()
77  {
78  return LWINF_GETMINOR(productInfo);
79  }
81 
82  unsigned int static Build()
83  {
84  return LWINF_GETBUILD(productInfo);
85  }
87  static bool isAtLeast(const unsigned int major, const unsigned int minor)
88  {
89  if (major < Major()) return true;
90  if (major > Major()) return false;
91  if (minor <= Minor()) return true;
92  return false;
93  }
94  static bool is(const unsigned int major, const unsigned int minor)
95  {
96  return (major == Major()) && (minor == Minor());
97  }
99  static bool isAtLeast(const unsigned int major, const unsigned int minor, const unsigned build)
100  {
101  if (isAtLeast(major, minor))
102  {
103  return (build >= Build());
104  }
105  return false;
106  }
107  int static GetNumCores();
108  };
109 
111 
114  extern GlobalFunc *SuperGlobal;
116 
119  void SetSuperGlobal(GlobalFunc *g);
120 
121 
123  template<class G>
125  {
126  private:
127  static const char *m_globalName;
128  static size_t usage_count;
129  protected:
130  static G *globPtr;
132  {
133  return (globPtr = reinterpret_cast<G *> (SuperGlobal(m_globalName, GFUSE_ACQUIRE))) != 0;
134  }
135  public:
138  {
139  if (usage_count < 1)
140  {
141  if (_acquireGlobal())
142  {
143  usage_count++;
144  }
145  else
146  {
147  //throw MissingGlobal(name);
148  }
149  }
150  }
152  virtual ~GlobalBase()
153  {
154 #ifdef _DEBUG
155  //lwpp::dostream dout;
156  //dout << "Releasing Global: " << m_name << " (Post)\n";
157 #endif
158  usage_count--;
159  if (usage_count == 0)
160  {
161  SuperGlobal(m_globalName, GFUSE_RELEASE); // release global
162  }
163  }
164 
166  virtual bool available() const
167  {
168  return (globPtr != 0);
169  }
170 
172  G* operator->() {return globPtr;}
173 
175  virtual G *getGlobal() {return globPtr;}
176  };
177 
179  {
180  public:
182  virtual ~genericGlobal() {}
183  };
184 
185  template<typename G, const char *globalName>
186  class nGlobalBase : public genericGlobal
187  {
188  private:
189  static size_t usage_count;
190  protected:
191  static G *globPtr;
192  public:
194  explicit nGlobalBase()
195  {
196  if ((globPtr = reinterpret_cast<G *> (SuperGlobal(globalName, GFUSE_ACQUIRE))) != 0)
197  {
198  usage_count++;
199  }
200  else
201  {
202  //throw MissingGlobal(name);
203  }
205  }
207  virtual ~nGlobalBase()
208  {
209 #ifdef _DEBUG
210  //lwpp::dostream dout;
211  //dout << "Releasing Global: " << m_name << " (Post)\n";
212 #endif
213  usage_count--;
214  if (usage_count == 0)
215  {
216  SuperGlobal(globalName, GFUSE_RELEASE); // release global
217  }
218  }
219 
221  virtual bool available()
222  {
223  return (globPtr != 0);
224  }
225 
227  G* operator->() {return globPtr;}
228 
230  virtual G *getGlobal() {return globPtr;}
231  };
232 
234  template<class T>
236  {
237  protected:
238  static T *globPtr;
239  public:
241  explicit TransientGlobalBase(const char *global_name)
242  {
243  globPtr = static_cast<T *> (SuperGlobal(global_name, GFUSE_TRANSIENT));
244  }
245  };
246 
247  template <class T>
248  T *getTransGlobal(const char *globalName)
249  {
250  return static_cast<T *> (SuperGlobal(globalName, GFUSE_TRANSIENT));
251  }
252 
253 
254  class GlobalMemory : protected GlobalBase<LWGlobalPool>
255  {
256  public:
258  LWMemChunk first(void) {return globPtr->first();}
259  LWMemChunk next(LWMemChunk chunk) {return globPtr->next(chunk);}
260  const char *ID(LWMemChunk chunk) {return globPtr->ID(chunk);}
261  int size(LWMemChunk chunk) {return globPtr->size(chunk);}
262  LWMemChunk find(const char *ID) {return globPtr->find(ID);}
263  LWMemChunk create(const char *ID, int size) {return globPtr->create(ID, size);}
264 
265  template <class T>
266  T *Find (const char *ID)
267  {
268  return static_cast<T *>(globPtr->find(ID));
269  }
270  template <class T>
271  T* Create(const char *ID)
272  {
273  return static_cast<T *>(globPtr->create(ID, sizeof(T)));
274  }
278  /*
279  template <class T>
280  bool Get(const char *ID, T*& type)
281  {
282  type = Find<T>(ID);
283  if (type) return true;
284  type = Create<T>(ID);
285  return false;
286  }
287  */
289 
295  template <class T>
296  bool Get(const char *ID, T*& type, T& defaultVal)
297  {
298  if (available())
299  {
300  type = Find<T>(ID);
301  if (type) return true;
302  type = Create<T>(ID);
303  return false;
304  }
305  else // if the global is not available, return a default value
306  {
307  type = &defaultVal;
308  return true;
309  }
310  }
311  };
312 
313 
314  class ServerInfo : public GlobalBase<LWServerInfo>
315  {
316  public:
317  const char *getPath(std::string server_class, std::string server_name)
318  {
319  if (available()) return globPtr->path(server_class.c_str(), server_name.c_str());
320  return 0;
321  }
322  };
323 
325  {
326  public:
327  CustomGlobal(const char *name, ActivateFunc *activate, ServerTagInfo *tags)
328  {
329  LWServer::AddPlugin(LWGLOBALSERVICE_CLASS, name, activate, tags);
330  }
331  };
332 
333  namespace lwsdk10
334  {
335  //#define LWSERVERINFO_GLOBAL "LW Server Info"
336 
337  typedef struct st_LWServerInfo
338  {
339  const char* (*path) (const char* server_class,const char* server_name);
340  const char** (*list) (const char* server_class);
341  } LWServerInfo;
342  }
343 
344  class ServerInfo10 : public GlobalBase<lwsdk10::LWServerInfo>
345  {
346  public:
347  const char *getPath(std::string server_class, std::string server_name)
348  {
349  if (available()) return globPtr->path(server_class.c_str(), server_name.c_str());
350  return 0;
351  }
352  std::vector<std::string> getList (std::string server_class)
353  {
354  std::vector<std::string> pList;
355  const char **plugList = globPtr->list(server_class.c_str());
356  if (plugList)
357  {
358  int i = 0;
359  while (plugList[i])
360  {
361  pList.push_back(plugList[i]);
362  ++i;
363  }
364  }
365  return pList;
366  }
367  };
368 
369 } // end namespace lwpp
370 
371 #endif // LWPP_GLOBAL