summaryrefslogtreecommitdiff
path: root/lib/wx/c_src/wxe_memory.h
blob: faf7b3aac232b3293a00cee05a96c5aba6f859b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2014-2016. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */

#ifndef _WXE_MEMORY_H
#define	_WXE_MEMORY_H

class intListElement {
 public:
    intListElement(int Element) {car = Element; cdr = NULL;};
    intListElement(int Element, intListElement *list)
    {car = Element; cdr = list;};
    int car;
    intListElement *cdr;
};

class intList {
 public:
    intList() {list = NULL;};
    ~intList() {
	intListElement *head = list;
	while(head) {
	    intListElement *tail=head->cdr;
	    delete head;
	    head = tail;
	} };
    bool IsEmpty() {return list == NULL;};
    void Append(int Element) { list = new intListElement(Element, list); };
    int Pop() {
	intListElement *temp = list;
	int res = list->car;
	list = temp->cdr;
	delete temp;
	return res;
    }
    intListElement *list;
};

class wxe_badarg
{
 public:
  wxe_badarg(int Ref) : ref(Ref) { var = NULL; } ;
  wxe_badarg(const char * Var) : var(Var) { } ;
  int ref;
  const char * var;
};

class wxeMemEnv {
 public:
    wxeMemEnv() {
        create();
    };
    void create() {
        ref2ptr = (void **) enif_alloc(128*sizeof(void *));
	ref2ptr[0] = NULL;
	next = 1;
	max = 128;
        tmp_env = enif_alloc_env();
        free.list = NULL;
    };

    // ~wxeMemEnv() {}; // done in WxeApp::destroyMemEnv

    void * getPtr(ErlNifEnv *env, ERL_NIF_TERM term, const char *arg, ERL_NIF_TERM * type = NULL) {
        int index, tpl_sz;
        const ERL_NIF_TERM *tpl;
        if(!enif_get_tuple(env, term, &tpl_sz, &tpl) && tpl_sz != 4) throw wxe_badarg(arg);
        if(!enif_get_int(env, tpl[1], &index)) throw wxe_badarg(arg);
        if(type) *type = tpl[2];
        void * temp = ref2ptr[index];
        if((index < next) && ((index == 0) || (temp != (void *)NULL)))
            return temp;
        throw wxe_badarg(arg);
    };

    int  next;
    int  max;
    void ** ref2ptr;
    intList   free;
    wxe_me_ref *me_ref;  // backreference
    ErlNifPid owner;
    ErlNifEnv *tmp_env;
};

class wxeRefData {
 public:
 wxeRefData(unsigned int dref, int ttype, int is_new, wxeMemEnv *menv) :
    ref(dref), type(ttype), memenv(menv), alloc_in_erl(is_new) { enif_set_pid_undefined(&pid); } ;
    int ref;
    int type;
    // 0 = wxWindow subclasses, 1 = wxObject subclasses
    // 2 = wxDialog subclasses, 3 = allocated wxObjects but not returned from new
    // 4 = wxGraphicsObjects or it's subclasses that can no be overloaded
    // 8 = wxObjects that should always be deleted directly (wxDC derivates)
    // > 10 classes which lack virtual destr, or are supposed to be allocated on
    //     the stack
    wxeMemEnv *memenv;
    bool alloc_in_erl;
    ErlNifPid pid;
};

// WX_DECLARE_HASH_MAP(ErlNifPid, wxeMemEnv*, wxIntegerHash, wxIntegerEqual, wxeMemMap);

WX_DECLARE_VOIDPTR_HASH_MAP(wxeRefData *, ptrMap);

#endif