| [03286aa] | 1 | #pragma once | 
|---|
|  | 2 |  | 
|---|
| [73abe95] | 3 | #include <fstream.hfa> | 
|---|
| [03286aa] | 4 | #include <stddef.h> | 
|---|
| [73abe95] | 5 | #include <stdlib.hfa> | 
|---|
| [13488ca] | 6 |  | 
|---|
|  | 7 | //============================================================================== | 
|---|
|  | 8 | // type safe malloc / free | 
|---|
|  | 9 |  | 
|---|
|  | 10 | forall(otype T) | 
|---|
|  | 11 | T* new() | 
|---|
|  | 12 | { | 
|---|
|  | 13 | T* p = malloc(); | 
|---|
|  | 14 | p{}; | 
|---|
|  | 15 | return p; | 
|---|
|  | 16 | } | 
|---|
|  | 17 |  | 
|---|
|  | 18 | forall(otype T) | 
|---|
|  | 19 | void delete(T* p) | 
|---|
|  | 20 | { | 
|---|
|  | 21 | ^p{}; | 
|---|
|  | 22 | free(p); | 
|---|
|  | 23 | } | 
|---|
|  | 24 |  | 
|---|
|  | 25 | //============================================================================== | 
|---|
|  | 26 | // ref counter content | 
|---|
| [03286aa] | 27 |  | 
|---|
|  | 28 | struct content_t | 
|---|
|  | 29 | { | 
|---|
|  | 30 | int value; | 
|---|
|  | 31 | size_t count; | 
|---|
|  | 32 | }; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | void ?{}(content_t* this) | 
|---|
|  | 35 | { | 
|---|
|  | 36 | sout | "Constructing content" | endl; | 
|---|
|  | 37 | this->count = 0; | 
|---|
|  | 38 | } | 
|---|
|  | 39 |  | 
|---|
|  | 40 | void ^?{}(content_t* this) | 
|---|
|  | 41 | { | 
|---|
|  | 42 | sout | "Destroying content" | endl; | 
|---|
|  | 43 | } | 
|---|
|  | 44 |  | 
|---|
| [13488ca] | 45 | //============================================================================== | 
|---|
|  | 46 | // ref counter wrapper | 
|---|
|  | 47 |  | 
|---|
|  | 48 | struct wrapper_t | 
|---|
|  | 49 | { | 
|---|
|  | 50 | content_t* ptr; | 
|---|
|  | 51 | }; | 
|---|
|  | 52 |  | 
|---|
| [bf1ee05] | 53 | void ?{}(wrapper_t* this) | 
|---|
| [13488ca] | 54 | { | 
|---|
| [21995bc] | 55 | sout | "Constructing empty ref pointer" | endl | endl; | 
|---|
| [bf1ee05] | 56 | this->ptr = NULL; | 
|---|
| [13488ca] | 57 | } | 
|---|
|  | 58 |  | 
|---|
| [bf1ee05] | 59 | void ?{}(wrapper_t* this, wrapper_t rhs) | 
|---|
| [13488ca] | 60 | { | 
|---|
| [bf1ee05] | 61 | sout | "Constructing ref pointer from copy" | endl; | 
|---|
|  | 62 | this->ptr = rhs.ptr; | 
|---|
| [13488ca] | 63 | this->ptr->count++; | 
|---|
| [21995bc] | 64 | sout | "Reference is " | this->ptr->count | endl | endl; | 
|---|
| [13488ca] | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | void ^?{}(wrapper_t* this) | 
|---|
|  | 68 | { | 
|---|
| [bf1ee05] | 69 | if(this->ptr) | 
|---|
| [13488ca] | 70 | { | 
|---|
| [bf1ee05] | 71 | sout | "Destroying ref pointer" | endl; | 
|---|
| [13488ca] | 72 | this->ptr->count--; | 
|---|
| [21995bc] | 73 | sout | "Reference is " | this->ptr->count | endl | endl; | 
|---|
| [13488ca] | 74 | if(!this->ptr->count) delete(this->ptr); | 
|---|
|  | 75 | } | 
|---|
|  | 76 | else | 
|---|
|  | 77 | { | 
|---|
| [21995bc] | 78 | sout | "Destroying empty ref pointer" | endl | endl; | 
|---|
| [13488ca] | 79 | } | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [21995bc] | 82 | wrapper_t ?=?(wrapper_t* this, wrapper_t rhs) | 
|---|
|  | 83 | { | 
|---|
|  | 84 | sout | "Setting ref pointer" | endl; | 
|---|
|  | 85 | if(this->ptr) | 
|---|
|  | 86 | { | 
|---|
|  | 87 | this->ptr->count--; | 
|---|
|  | 88 | sout | "Reference is " | this->ptr->count | endl | endl; | 
|---|
|  | 89 | if(!this->ptr->count) delete(this->ptr); | 
|---|
|  | 90 | } | 
|---|
|  | 91 | this->ptr = rhs.ptr; | 
|---|
|  | 92 | this->ptr->count++; | 
|---|
|  | 93 | sout | "Reference is " | this->ptr->count | endl | endl; | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [13488ca] | 96 | void set(wrapper_t* this, content_t* c) | 
|---|
|  | 97 | { | 
|---|
|  | 98 | this->ptr = c; | 
|---|
| [bf1ee05] | 99 | this->ptr->count++; | 
|---|
| [13488ca] | 100 | sout | "Setting ref pointer" | endl; | 
|---|
| [21995bc] | 101 | sout | "Reference is " | this->ptr->count | endl | endl; | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | void clear(wrapper_t* this) | 
|---|
|  | 105 | { | 
|---|
|  | 106 | sout | "Clearing ref pointer" | endl; | 
|---|
|  | 107 | this->ptr->count--; | 
|---|
|  | 108 | sout | "Reference is " | this->ptr->count | endl | endl; | 
|---|
|  | 109 | if(!this->ptr->count) delete(this->ptr); | 
|---|
|  | 110 | this->ptr = NULL; | 
|---|
| [13488ca] | 111 | } | 
|---|
| [bf1ee05] | 112 |  | 
|---|
| [21995bc] | 113 |  | 
|---|
| [bf1ee05] | 114 | wrapper_t wrap(int val) | 
|---|
|  | 115 | { | 
|---|
|  | 116 | wrapper_t w; | 
|---|
|  | 117 | content_t* c = malloc(); | 
|---|
|  | 118 | c{}; | 
|---|
|  | 119 | c->value = val; | 
|---|
|  | 120 | set(&w, c); | 
|---|
|  | 121 | return w; | 
|---|
|  | 122 | } | 
|---|