source: src/examples/wrapper/src/pointer.h @ bf1ee05

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since bf1ee05 was bf1ee05, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

test with single copy from return

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#pragma once
2
3#include <fstream>
4#include <stddef.h>
5#include <stdlib>
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" | endl;
37        this->count = 0;
38}
39
40void ^?{}(content_t* this)
41{
42        sout | "Destroying content" | endl;
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" | endl;
56        this->ptr = NULL;
57}
58
59void ?{}(wrapper_t* this, wrapper_t rhs)
60{
61        sout | "Constructing ref pointer from copy" | endl;
62        this->ptr = rhs.ptr;
63        this->ptr->count++;
64}
65
66void ^?{}(wrapper_t* this)
67{
68        if(this->ptr)
69        {
70                sout | "Destroying ref pointer" | endl;
71                this->ptr->count--;
72                sout | "Reference is " | (int)this->ptr->count | endl;
73                if(!this->ptr->count) delete(this->ptr);
74        }
75        else
76        {
77                sout | "Destroying empty ref pointer" | endl;
78        }
79}
80
81void set(wrapper_t* this, content_t* c)
82{
83        this->ptr = c;
84        this->ptr->count++;
85        sout | "Setting ref pointer" | endl;
86        sout | "Reference is " | this->ptr->count | endl;
87}
88
89wrapper_t wrap(int val)
90{
91        wrapper_t w;
92        content_t* c = malloc();
93        c{};
94        c->value = val;
95        set(&w, c);
96        return w;
97}
Note: See TracBrowser for help on using the repository browser.