source: examples/wrapper/src/pointer.h @ 959f6ad

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 959f6ad was 200fcb3, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add auto newline to sout, change endl to nl

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#pragma once
2
3#include <fstream.hfa>
4#include <stddef.h>
5#include <stdlib.hfa>
6
7//==============================================================================
8// type safe malloc / free
9
10forall(otype T)
11T* new()
12{
13        T* p = malloc();
14        p{};
15        return p;
16}
17
18forall(otype T)
19void delete(T* p)
20{
21        ^p{};
22        free(p);
23}
24
25//==============================================================================
26// ref counter content
27
28struct content_t
29{
30        int value;
31        size_t count;
32};
33
34void ?{}(content_t* this)
35{
36        sout | "Constructing content";
37        this->count = 0;
38}
39
40void ^?{}(content_t* this)
41{
42        sout | "Destroying content";
43}
44
45//==============================================================================
46// ref counter wrapper
47
48struct wrapper_t
49{
50        content_t* ptr;
51};
52
53void ?{}(wrapper_t* this)
54{
55        sout | "Constructing empty ref pointer" | nl;
56        this->ptr = NULL;
57}
58
59void ?{}(wrapper_t* this, wrapper_t rhs)
60{
61        sout | "Constructing ref pointer from copy";
62        this->ptr = rhs.ptr;
63        this->ptr->count++;
64        sout | "Reference is " | this->ptr->count | nl;
65}
66
67void ^?{}(wrapper_t* this)
68{
69        if(this->ptr)
70        {
71                sout | "Destroying ref pointer";
72                this->ptr->count--;
73                sout | "Reference is " | this->ptr->count | nl;
74                if(!this->ptr->count) delete(this->ptr);
75        }
76        else
77        {
78                sout | "Destroying empty ref pointer" | nl;
79        }
80}
81
82wrapper_t ?=?(wrapper_t* this, wrapper_t rhs)
83{
84        sout | "Setting ref pointer";
85        if(this->ptr)
86        {
87                this->ptr->count--;
88                sout | "Reference is " | this->ptr->count | nl;
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 | nl;
94}
95
96void set(wrapper_t* this, content_t* c)
97{
98        this->ptr = c;
99        this->ptr->count++;
100        sout | "Setting ref pointer";
101        sout | "Reference is " | this->ptr->count | nl;
102}
103
104void clear(wrapper_t* this)
105{
106        sout | "Clearing ref pointer";
107        this->ptr->count--;
108        sout | "Reference is " | this->ptr->count | nl;
109        if(!this->ptr->count) delete(this->ptr);
110        this->ptr = NULL;
111}
112
113
114wrapper_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}
Note: See TracBrowser for help on using the repository browser.