source: src/examples/gc_no_raii/bug-repro/push_back.h @ f739788

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f739788 was df4aea7, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

all implemented files compile

  • Property mode set to 100644
File size: 1.9 KB
Line 
1//------------------------------------------------------------------------------
2//Declaration
3trait allocator_c(otype T, otype allocator_t) {
4        void ctor(allocator_t* const);
5        void dtor(allocator_t* const);
6        void realloc(allocator_t* const, size_t);
7        T* data(allocator_t* const);
8};
9
10forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
11struct vector
12{
13        allocator_t storage;
14        size_t size;
15};
16
17//------------------------------------------------------------------------------
18//Initialization
19forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
20void vector_ctor(vector(T, allocator_t) *const this);
21
22forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
23void dtor(vector(T, allocator_t) *const this);
24
25//------------------------------------------------------------------------------
26//Allocator
27forall(otype T)
28struct heap_allocator
29{
30        T* storage;
31        size_t capacity;
32};
33
34forall(otype T)
35void ctor(heap_allocator(T) *const this);
36
37forall(otype T)
38void dtor(heap_allocator(T) *const this);
39
40forall(otype T)
41void realloc(heap_allocator(T) *const this, size_t size);
42
43forall(otype T)
44inline T* data(heap_allocator(T) *const this)
45{
46        return this->storage;
47}
48
49//------------------------------------------------------------------------------
50//Capacity
51forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
52inline bool empty(vector(T, allocator_t) *const this)
53{
54        return this->size == 0;
55}
56
57forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
58inline bool size(vector(T, allocator_t) *const this)
59{
60        return this->size;
61}
62
63forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
64inline void reserve(vector(T, allocator_t) *const this, size_t size)
65{
66        realloc(&this->storage, this->size+1);
67}
68
69//------------------------------------------------------------------------------
70//Modifiers
71forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
72void push_back(vector(T, allocator_t) *const this, T value);
Note: See TracBrowser for help on using the repository browser.