sqrat  0.9
sqrat
 All Classes Functions Variables Enumerations Enumerator Pages
sqratUtil.h
1 //
2 // SqratUtil: Squirrel Utilities
3 //
4 
5 //
6 // Copyright (c) 2009 Brandon Jones
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
27 
28 #if !defined(_SCRAT_UTIL_H_)
29 #define _SCRAT_UTIL_H_
30 
31 #include <cassert>
32 #include <squirrel.h>
33 #include <string.h>
34 
35 namespace Sqrat {
36 
41 template <typename T>
42 void UNUSED(const T&) {
43 }
45 
49 typedef std::basic_string<SQChar> string;
50 
52 #ifdef SQUNICODE
53 /* from http://stackoverflow.com/questions/15333259/c-stdwstring-to-stdstring-quick-and-dirty-conversion-for-use-as-key-in,
54  only works for ASCII chars */
58 static std::wstring ascii_string_to_wstring(const std::string& str)
59 {
60  return std::wstring(str.begin(), str.end());
61 }
62 
66 static std::string ascii_wstring_to_string(const std::wstring& wstr)
67 {
68  return std::string(wstr.begin(), wstr.end());
69 }
70 
71 static std::wstring (*string_to_wstring)(const std::string& str) = ascii_string_to_wstring;
72 static std::string (*wstring_to_string)(const std::wstring& wstr) = ascii_wstring_to_string;
73 #endif // SQUNICODE
74 
79 class DefaultVM {
80 private:
81  static HSQUIRRELVM& staticVm() {
82  static HSQUIRRELVM vm;
83  return vm;
84  }
85 public:
92  static HSQUIRRELVM Get() {
93  return staticVm();
94  }
95 
102  static void Set(HSQUIRRELVM vm) {
103  staticVm() = vm;
104  }
105 };
106 
107 #if !defined (SCRAT_NO_ERROR_CHECKING)
108 class Error {
127 public:
134  static Error& Instance() {
135  static Error instance;
136  return instance;
137  }
138 
149  static string FormatTypeError(HSQUIRRELVM vm, SQInteger idx, const string& expectedType) {
150  string err = _SC("wrong type (") + expectedType + _SC(" expected");
151  if (SQ_SUCCEEDED(sq_typeof(vm, idx))) {
152  const SQChar* actualType;
153  sq_tostring(vm, -1);
154  sq_getstring(vm, -1, &actualType);
155  sq_pop(vm, 1);
156  err = err + _SC(", got ") + actualType + _SC(")");
157  } else {
158  err = err + _SC(", got unknown)");
159  }
160  sq_pop(vm, 1);
161  return err;
162  }
163 
170  void Clear(HSQUIRRELVM vm) {
171  //TODO: use mutex to lock errMap in multithreaded environment
172  errMap.erase(vm);
173  }
174 
183  string Message(HSQUIRRELVM vm) {
184  //TODO: use mutex to lock errMap in multithreaded environment
185  string err = errMap[vm];
186  errMap.erase(vm);
187  return err;
188  }
189 
198  bool Occurred(HSQUIRRELVM vm) {
199  //TODO: use mutex to lock errMap in multithreaded environment
200  return errMap.find(vm) != errMap.end();
201  }
202 
210  void Throw(HSQUIRRELVM vm, const string& err) {
211  //TODO: use mutex to lock errMap in multithreaded environment
212  if (errMap.find(vm) == errMap.end()) {
213  errMap[vm] = err;
214  }
215  }
216 
217 private:
218  Error() {}
219 
220  std::map< HSQUIRRELVM, string > errMap;
221 };
222 #endif
223 
232 private:
233  static bool& errorHandling() {
234  static bool eh = true;
235  return eh;
236  }
237 public:
244  static bool IsEnabled() {
245  return errorHandling();
246  }
247 
254  static void Enable(bool enable) {
255  errorHandling() = enable;
256  }
257 };
258 
267 inline string LastErrorString( HSQUIRRELVM vm ) {
268  const SQChar* sqErr;
269  sq_getlasterror(vm);
270  if(sq_gettype(vm, -1) == OT_NULL) {
271  return string();
272  }
273  sq_tostring(vm, -1);
274  sq_getstring(vm, -1, &sqErr);
275  return string(sqErr);
276 }
277 
290 template <class T>
292 {
293 private:
294  T* m_Ptr;
295  unsigned int* m_RefCount;
296 public:
302  m_Ptr (NULL),
303  m_RefCount(NULL)
304  {
305 
306  }
307 
314  SharedPtr(T* ptr)
315  {
316  Init(ptr);
317  }
318 
327  template <class U>
328  SharedPtr(U* ptr)
329  {
330  Init(ptr);
331  }
332 
339  SharedPtr(const SharedPtr<T>& copy)
340  {
341  if (copy.Get() != NULL)
342  {
343  m_Ptr = copy.Get();
344 
345  m_RefCount = copy.m_RefCount;
346  *m_RefCount += 1;
347  }
348  else
349  {
350  m_Ptr = NULL;
351  m_RefCount = NULL;
352  }
353  }
354 
363  template <class U>
364  SharedPtr(const SharedPtr<U>& copy)
365  {
366  if (copy.Get() != NULL)
367  {
368  m_Ptr = static_cast<T*>(copy.Get());
369 
370  m_RefCount = copy.m_RefCount;
371  *m_RefCount += 1;
372  }
373  else
374  {
375  m_Ptr = NULL;
376  m_RefCount = NULL;
377  }
378  }
379 
385  {
386  Reset();
387  }
388 
398  {
399  if (this != &copy)
400  {
401  Reset();
402 
403  if (copy.Get() != NULL)
404  {
405  m_Ptr = copy.Get();
406 
407  m_RefCount = copy.m_RefCount;
408  *m_RefCount += 1;
409  }
410  else
411  {
412  m_Ptr = NULL;
413  m_RefCount = NULL;
414  }
415  }
416 
417  return *this;
418  }
419 
430  template <class U>
432  {
433  Reset();
434 
435  if (copy.Get() != NULL)
436  {
437  m_Ptr = static_cast<T*>(copy.Get());
438 
439  m_RefCount = copy.m_RefCount;
440  *m_RefCount += 1;
441  }
442  else
443  {
444  m_Ptr = NULL;
445  m_RefCount = NULL;
446  }
447 
448  return *this;
449  }
450 
457  void Init(T* ptr)
458  {
459  Reset();
460 
461  m_Ptr = ptr;
462 
463  m_RefCount = new unsigned int;
464  *m_RefCount = 1;
465  }
466 
475  template <class U>
476  void Init(U* ptr)
477  {
478  Reset();
479 
480  m_Ptr = static_cast<T*>(ptr);
481 
482  m_RefCount = new unsigned int;
483  *m_RefCount = 1;
484  }
485 
490  void Reset()
491  {
492  if (m_Ptr != NULL)
493  {
494  if (*m_RefCount == 1)
495  {
496  delete m_Ptr;
497  delete m_RefCount;
498 
499  m_Ptr = NULL;
500  m_RefCount = NULL;
501  }
502  else
503  *m_RefCount -= 1;
504  }
505  }
506 
511  operator bool() const
512  {
513  return m_Ptr != NULL;
514  }
515 
520  bool operator!() const
521  {
522  return m_Ptr == NULL;
523  }
524 
529  template <typename U>
530  bool operator ==(const SharedPtr<U>& right) const
531  {
532  return m_Ptr == right.m_Ptr;
533  }
534 
539  bool operator ==(const SharedPtr<T>& right) const
540  {
541  return m_Ptr == right.m_Ptr;
542  }
543 
548  template <typename U>
549  bool friend operator ==(const SharedPtr<T>& left, const U* right)
550  {
551  return left.m_Ptr == right;
552  }
553 
558  bool friend operator ==(const SharedPtr<T>& left, const T* right)
559  {
560  return left.m_Ptr == right;
561  }
562 
567  template <typename U>
568  bool friend operator ==(const U* left, const SharedPtr<T>& right)
569  {
570  return left == right.m_Ptr;
571  }
572 
577  bool friend operator ==(const T* left, const SharedPtr<T>& right)
578  {
579  return left == right.m_Ptr;
580  }
581 
586  template <typename U>
587  bool operator !=(const SharedPtr<U>& right) const
588  {
589  return m_Ptr != right.m_Ptr;
590  }
591 
596  bool operator !=(const SharedPtr<T>& right) const
597  {
598  return m_Ptr != right.m_Ptr;
599  }
600 
605  template <typename U>
606  bool friend operator !=(const SharedPtr<T>& left, const U* right)
607  {
608  return left.m_Ptr != right;
609  }
610 
615  bool friend operator !=(const SharedPtr<T>& left, const T* right)
616  {
617  return left.m_Ptr != right;
618  }
619 
624  template <typename U>
625  bool friend operator !=(const U* left, const SharedPtr<T>& right)
626  {
627  return left != right.m_Ptr;
628  }
629 
634  bool friend operator !=(const T* left, const SharedPtr<T>& right)
635  {
636  return left != right.m_Ptr;
637  }
638 
643  T& operator*() const
644  {
645  assert(m_Ptr != NULL);
646  return *m_Ptr;
647  }
648 
653  T* operator->() const
654  {
655  assert(m_Ptr != NULL);
656  return m_Ptr;
657  }
658 
665  T* Get() const
666  {
667  return m_Ptr;
668  }
669 };
670 
671 }
672 
673 #endif
static HSQUIRRELVM Get()
Definition: sqratUtil.h:92
SharedPtr< T > & operator=(const SharedPtr< T > &copy)
Definition: sqratUtil.h:397
SharedPtr< T > & operator=(const SharedPtr< U > &copy)
Definition: sqratUtil.h:431
void Init(T *ptr)
Definition: sqratUtil.h:457
SharedPtr()
Definition: sqratUtil.h:301
static bool IsEnabled()
Definition: sqratUtil.h:244
void Reset()
Definition: sqratUtil.h:490
T * Get() const
Definition: sqratUtil.h:665
static void Enable(bool enable)
Definition: sqratUtil.h:254
~SharedPtr()
Definition: sqratUtil.h:384
static string FormatTypeError(HSQUIRRELVM vm, SQInteger idx, const string &expectedType)
Definition: sqratUtil.h:149
Definition: sqratUtil.h:126
Definition: sqratUtil.h:231
void Clear(HSQUIRRELVM vm)
Definition: sqratUtil.h:170
SharedPtr(const SharedPtr< U > &copy)
Definition: sqratUtil.h:364
void Init(U *ptr)
Definition: sqratUtil.h:476
bool operator==(const SharedPtr< U > &right) const
Definition: sqratUtil.h:530
string Message(HSQUIRRELVM vm)
Definition: sqratUtil.h:183
Definition: sqratUtil.h:291
T & operator*() const
Definition: sqratUtil.h:643
SharedPtr(const SharedPtr< T > &copy)
Definition: sqratUtil.h:339
static Error & Instance()
Definition: sqratUtil.h:134
bool operator!=(const SharedPtr< U > &right) const
Definition: sqratUtil.h:587
T * operator->() const
Definition: sqratUtil.h:653
void Throw(HSQUIRRELVM vm, const string &err)
Definition: sqratUtil.h:210
static void Set(HSQUIRRELVM vm)
Definition: sqratUtil.h:102
SharedPtr(U *ptr)
Definition: sqratUtil.h:328
SharedPtr(T *ptr)
Definition: sqratUtil.h:314
Helper class that defines a VM that can be used as a fallback VM in case no other one is given to a p...
Definition: sqratUtil.h:79
bool operator!() const
Definition: sqratUtil.h:520
bool Occurred(HSQUIRRELVM vm)
Definition: sqratUtil.h:198