sqrat  0.9
sqrat
 All Classes Functions Variables Enumerations Enumerator Pages
sqratAllocator.h
1 //
2 // SqratAllocator: Custom Class Allocation/Deallocation
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_ALLOCATOR_H_)
29 #define _SCRAT_ALLOCATOR_H_
30 
31 #include <squirrel.h>
32 #include <string.h>
33 
34 #include "sqratObject.h"
35 #include "sqratTypes.h"
36 
37 namespace Sqrat {
38 
44 template< class T >
45 class is_default_constructible {
46  template<int x>
47  class receive_size{};
48 
49  template< class U >
50  static int sfinae( receive_size< sizeof U() > * );
51 
52  template< class U >
53  static char sfinae( ... );
54 
55 public:
56  enum { value = sizeof( sfinae<T>(0) ) == sizeof(int) };
57 };
59 
69 template<class C>
71 
72  static void setInstance(HSQUIRRELVM vm, C* instance)
73  {
74  sq_setinstanceup(vm, 1, instance);
75  sq_setreleasehook(vm, 1, &Delete);
76  }
77 
78  template <class T, bool b>
79  struct NewC
80  {
81  T* p;
82  NewC()
83  {
84  p = new T();
85  }
86  };
87 
88  template <class T>
89  struct NewC<T, false>
90  {
91  T* p;
92  NewC()
93  {
94  p = 0;
95  }
96  };
97 
98 public:
99 
108  static SQInteger New(HSQUIRRELVM vm) {
109  C* instance = NewC<C, is_default_constructible<C>::value >().p;
110  setInstance(vm, instance);
111  return 0;
112  }
113 
118  template <int count>
119  static SQInteger iNew(HSQUIRRELVM vm) {
120  return New(vm);
121  }
122 
123  template <typename A1>
124  static SQInteger iNew(HSQUIRRELVM vm) {
125  Var<A1> a1(vm, 2);
126 
127  if (!Error::Instance().Occurred(vm)) {
128  setInstance(vm, new C(
129  a1.value
130  ));
131  }
132  return 0;
133  }
134  template <typename A1,typename A2>
135  static SQInteger iNew(HSQUIRRELVM vm) {
136  Var<A1> a1(vm, 2);
137  Var<A2> a2(vm, 3);
138 
139  if (!Error::Instance().Occurred(vm)) {
140  setInstance(vm, new C(
141  a1.value,
142  a2.value
143  ));
144  }
145  return 0;
146  }
147  template <typename A1,typename A2,typename A3>
148  static SQInteger iNew(HSQUIRRELVM vm) {
149  Var<A1> a1(vm, 2);
150  Var<A2> a2(vm, 3);
151  Var<A3> a3(vm, 4);
152 
153  if (!Error::Instance().Occurred(vm)) {
154  setInstance(vm, new C(
155  a1.value,
156  a2.value,
157  a3.value
158  ));
159  }
160  return 0;
161  }
162  template <typename A1,typename A2,typename A3,typename A4>
163  static SQInteger iNew(HSQUIRRELVM vm) {
164  Var<A1> a1(vm, 2);
165  Var<A2> a2(vm, 3);
166  Var<A3> a3(vm, 4);
167  Var<A4> a4(vm, 5);
168 
169  if (!Error::Instance().Occurred(vm)) {
170  setInstance(vm, new C(
171  a1.value,
172  a2.value,
173  a3.value,
174  a4.value
175  ));
176  }
177  return 0;
178  }
179  template <typename A1,typename A2,typename A3,typename A4,typename A5>
180  static SQInteger iNew(HSQUIRRELVM vm) {
181  Var<A1> a1(vm, 2);
182  Var<A2> a2(vm, 3);
183  Var<A3> a3(vm, 4);
184  Var<A4> a4(vm, 5);
185  Var<A5> a5(vm, 6);
186 
187  if (!Error::Instance().Occurred(vm)) {
188  setInstance(vm, new C(
189  a1.value,
190  a2.value,
191  a3.value,
192  a4.value,
193  a5.value
194  ));
195  }
196  return 0;
197  }
198  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6>
199  static SQInteger iNew(HSQUIRRELVM vm) {
200  Var<A1> a1(vm, 2);
201  Var<A2> a2(vm, 3);
202  Var<A3> a3(vm, 4);
203  Var<A4> a4(vm, 5);
204  Var<A5> a5(vm, 6);
205  Var<A6> a6(vm, 7);
206 
207  if (!Error::Instance().Occurred(vm)) {
208  setInstance(vm, new C(
209  a1.value,
210  a2.value,
211  a3.value,
212  a4.value,
213  a5.value,
214  a6.value
215  ));
216  }
217  return 0;
218  }
219  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6,typename A7>
220  static SQInteger iNew(HSQUIRRELVM vm) {
221  Var<A1> a1(vm, 2);
222  Var<A2> a2(vm, 3);
223  Var<A3> a3(vm, 4);
224  Var<A4> a4(vm, 5);
225  Var<A5> a5(vm, 6);
226  Var<A6> a6(vm, 7);
227  Var<A7> a7(vm, 8);
228 
229  if (!Error::Instance().Occurred(vm)) {
230  setInstance(vm, new C(
231  a1.value,
232  a2.value,
233  a3.value,
234  a4.value,
235  a5.value,
236  a6.value,
237  a7.value
238  ));
239  }
240  return 0;
241  }
242  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6,typename A7,typename A8>
243  static SQInteger iNew(HSQUIRRELVM vm) {
244  Var<A1> a1(vm, 2);
245  Var<A2> a2(vm, 3);
246  Var<A3> a3(vm, 4);
247  Var<A4> a4(vm, 5);
248  Var<A5> a5(vm, 6);
249  Var<A6> a6(vm, 7);
250  Var<A7> a7(vm, 8);
251  Var<A8> a8(vm, 9);
252 
253  if (!Error::Instance().Occurred(vm)) {
254  setInstance(vm, new C(
255  a1.value,
256  a2.value,
257  a3.value,
258  a4.value,
259  a5.value,
260  a6.value,
261  a7.value,
262  a8.value
263  ));
264  }
265  return 0;
266  }
267  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6,typename A7,typename A8,typename A9>
268  static SQInteger iNew(HSQUIRRELVM vm) {
269  Var<A1> a1(vm, 2);
270  Var<A2> a2(vm, 3);
271  Var<A3> a3(vm, 4);
272  Var<A4> a4(vm, 5);
273  Var<A5> a5(vm, 6);
274  Var<A6> a6(vm, 7);
275  Var<A7> a7(vm, 8);
276  Var<A8> a8(vm, 9);
277  Var<A9> a9(vm, 10);
278 
279  if (!Error::Instance().Occurred(vm)) {
280  setInstance(vm, new C(
281  a1.value,
282  a2.value,
283  a3.value,
284  a4.value,
285  a5.value,
286  a6.value,
287  a7.value,
288  a8.value,
289  a9.value
290  ));
291  }
292  return 0;
293  }
295 
296 public:
297 
308  static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void* value) {
309  C* instance = new C(*static_cast<const C*>(value));
310  sq_setinstanceup(vm, idx, instance);
311  sq_setreleasehook(vm, idx, &Delete);
312  return 0;
313  }
314 
324  static SQInteger Delete(SQUserPointer ptr, SQInteger size) {
325  UNUSED(size);
326  C* instance = reinterpret_cast<C*>(ptr);
327  delete instance;
328  return 0;
329  }
330 };
331 
338 template<class C>
340 public:
341 
350  static SQInteger New(HSQUIRRELVM vm) {
351 #if !defined (SCRAT_NO_ERROR_CHECKING)
352  return sq_throwerror(vm, (ClassType<C>::ClassName(vm) + string(_SC(" constructing is not allowed"))).c_str());
353 #else
354  UNUSED(vm);
355  return 0;
356 #endif
357  }
358 
369  static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void* value) {
370  UNUSED(vm);
371  UNUSED(idx);
372  UNUSED(value);
373  return 0;
374  }
375 
385  static SQInteger Delete(SQUserPointer ptr, SQInteger size) {
386  UNUSED(ptr);
387  UNUSED(size);
388  return 0;
389  }
390 };
391 
398 template<class C>
399 class CopyOnly {
400 public:
401 
410  static SQInteger New(HSQUIRRELVM vm) {
411 #if !defined (SCRAT_NO_ERROR_CHECKING)
412  return sq_throwerror(vm, (ClassType<C>::ClassName(vm) + string(_SC(" constructing is not allowed"))).c_str());
413 #else
414  UNUSED(vm);
415  return 0;
416 #endif
417  }
418 
429  static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void* value) {
430  C* instance = new C(*static_cast<const C*>(value));
431  sq_setinstanceup(vm, idx, instance);
432  sq_setreleasehook(vm, idx, &Delete);
433  return 0;
434  }
435 
445  static SQInteger Delete(SQUserPointer ptr, SQInteger size) {
446  UNUSED(size);
447  C* instance = reinterpret_cast<C*>(ptr);
448  delete instance;
449  return 0;
450  }
451 };
452 
453 
463 template<class C>
464 class NoCopy {
465 
466  static void setInstance(HSQUIRRELVM vm, C* instance)
467  {
468  sq_setinstanceup(vm, 1, instance);
469  sq_setreleasehook(vm, 1, &Delete);
470  }
471 
472  template <class T, bool b>
473  struct NewC
474  {
475  T* p;
476  NewC()
477  {
478  p = new T();
479  }
480  };
481 
482  template <class T>
483  struct NewC<T, false>
484  {
485  T* p;
486  NewC()
487  {
488  p = 0;
489  }
490  };
491 
492 public:
493 
502  static SQInteger New(HSQUIRRELVM vm) {
503  C* instance = NewC<C, is_default_constructible<C>::value >().p;
504  setInstance(vm, instance);
505  return 0;
506  }
507 
512  template <int count>
513  static SQInteger iNew(HSQUIRRELVM vm) {
514  return New(vm);
515  }
516 
517  template <typename A1>
518  static SQInteger iNew(HSQUIRRELVM vm) {
519  Var<A1> a1(vm, 2);
520 
521  if (!Error::Instance().Occurred(vm)) {
522  setInstance(vm, new C(
523  a1.value
524  ));
525  }
526  return 0;
527  }
528  template <typename A1,typename A2>
529  static SQInteger iNew(HSQUIRRELVM vm) {
530  Var<A1> a1(vm, 2);
531  Var<A2> a2(vm, 3);
532 
533  if (!Error::Instance().Occurred(vm)) {
534  setInstance(vm, new C(
535  a1.value,
536  a2.value
537  ));
538  }
539  return 0;
540  }
541  template <typename A1,typename A2,typename A3>
542  static SQInteger iNew(HSQUIRRELVM vm) {
543  Var<A1> a1(vm, 2);
544  Var<A2> a2(vm, 3);
545  Var<A3> a3(vm, 4);
546 
547  if (!Error::Instance().Occurred(vm)) {
548  setInstance(vm, new C(
549  a1.value,
550  a2.value,
551  a3.value
552  ));
553  }
554  return 0;
555  }
556  template <typename A1,typename A2,typename A3,typename A4>
557  static SQInteger iNew(HSQUIRRELVM vm) {
558  Var<A1> a1(vm, 2);
559  Var<A2> a2(vm, 3);
560  Var<A3> a3(vm, 4);
561  Var<A4> a4(vm, 5);
562 
563  if (!Error::Instance().Occurred(vm)) {
564  setInstance(vm, new C(
565  a1.value,
566  a2.value,
567  a3.value,
568  a4.value
569  ));
570  }
571  return 0;
572  }
573  template <typename A1,typename A2,typename A3,typename A4,typename A5>
574  static SQInteger iNew(HSQUIRRELVM vm) {
575  Var<A1> a1(vm, 2);
576  Var<A2> a2(vm, 3);
577  Var<A3> a3(vm, 4);
578  Var<A4> a4(vm, 5);
579  Var<A5> a5(vm, 6);
580 
581  if (!Error::Instance().Occurred(vm)) {
582  setInstance(vm, new C(
583  a1.value,
584  a2.value,
585  a3.value,
586  a4.value,
587  a5.value
588  ));
589  }
590  return 0;
591  }
592  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6>
593  static SQInteger iNew(HSQUIRRELVM vm) {
594  Var<A1> a1(vm, 2);
595  Var<A2> a2(vm, 3);
596  Var<A3> a3(vm, 4);
597  Var<A4> a4(vm, 5);
598  Var<A5> a5(vm, 6);
599  Var<A6> a6(vm, 7);
600 
601  if (!Error::Instance().Occurred(vm)) {
602  setInstance(vm, new C(
603  a1.value,
604  a2.value,
605  a3.value,
606  a4.value,
607  a5.value,
608  a6.value
609  ));
610  }
611  return 0;
612  }
613  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6,typename A7>
614  static SQInteger iNew(HSQUIRRELVM vm) {
615  Var<A1> a1(vm, 2);
616  Var<A2> a2(vm, 3);
617  Var<A3> a3(vm, 4);
618  Var<A4> a4(vm, 5);
619  Var<A5> a5(vm, 6);
620  Var<A6> a6(vm, 7);
621  Var<A7> a7(vm, 8);
622 
623  if (!Error::Instance().Occurred(vm)) {
624  setInstance(vm, new C(
625  a1.value,
626  a2.value,
627  a3.value,
628  a4.value,
629  a5.value,
630  a6.value,
631  a7.value
632  ));
633  }
634  return 0;
635  }
636  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6,typename A7,typename A8>
637  static SQInteger iNew(HSQUIRRELVM vm) {
638  Var<A1> a1(vm, 2);
639  Var<A2> a2(vm, 3);
640  Var<A3> a3(vm, 4);
641  Var<A4> a4(vm, 5);
642  Var<A5> a5(vm, 6);
643  Var<A6> a6(vm, 7);
644  Var<A7> a7(vm, 8);
645  Var<A8> a8(vm, 9);
646 
647  if (!Error::Instance().Occurred(vm)) {
648  setInstance(vm, new C(
649  a1.value,
650  a2.value,
651  a3.value,
652  a4.value,
653  a5.value,
654  a6.value,
655  a7.value,
656  a8.value
657  ));
658  }
659  return 0;
660  }
661  template <typename A1,typename A2,typename A3,typename A4,typename A5,typename A6,typename A7,typename A8,typename A9>
662  static SQInteger iNew(HSQUIRRELVM vm) {
663  Var<A1> a1(vm, 2);
664  Var<A2> a2(vm, 3);
665  Var<A3> a3(vm, 4);
666  Var<A4> a4(vm, 5);
667  Var<A5> a5(vm, 6);
668  Var<A6> a6(vm, 7);
669  Var<A7> a7(vm, 8);
670  Var<A8> a8(vm, 9);
671  Var<A9> a9(vm, 10);
672 
673  if (!Error::Instance().Occurred(vm)) {
674  setInstance(vm, new C(
675  a1.value,
676  a2.value,
677  a3.value,
678  a4.value,
679  a5.value,
680  a6.value,
681  a7.value,
682  a8.value,
683  a9.value
684  ));
685  }
686  return 0;
687  }
689 
700  static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void* value) {
701  UNUSED(vm);
702  UNUSED(idx);
703  UNUSED(value);
704  return 0;
705  }
706 
716  static SQInteger Delete(SQUserPointer ptr, SQInteger size) {
717  UNUSED(size);
718  C* instance = reinterpret_cast<C*>(ptr);
719  delete instance;
720  return 0;
721  }
722 };
723 
724 }
725 
726 #endif
static SQInteger Delete(SQUserPointer ptr, SQInteger size)
Definition: sqratAllocator.h:385
Definition: sqratAllocator.h:70
Definition: sqratAllocator.h:399
static SQInteger Delete(SQUserPointer ptr, SQInteger size)
Definition: sqratAllocator.h:324
static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void *value)
Definition: sqratAllocator.h:308
static SQInteger New(HSQUIRRELVM vm)
Definition: sqratAllocator.h:502
static SQInteger New(HSQUIRRELVM vm)
Definition: sqratAllocator.h:410
static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void *value)
Definition: sqratAllocator.h:700
static Error & Instance()
Definition: sqratUtil.h:134
Definition: sqratAllocator.h:464
static SQInteger Delete(SQUserPointer ptr, SQInteger size)
Definition: sqratAllocator.h:716
static SQInteger New(HSQUIRRELVM vm)
Definition: sqratAllocator.h:108
Definition: sqratAllocator.h:339
static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void *value)
Definition: sqratAllocator.h:369
static SQInteger New(HSQUIRRELVM vm)
Definition: sqratAllocator.h:350
static SQInteger Delete(SQUserPointer ptr, SQInteger size)
Definition: sqratAllocator.h:445
static SQInteger Copy(HSQUIRRELVM vm, SQInteger idx, const void *value)
Definition: sqratAllocator.h:429