summaryrefslogtreecommitdiff
path: root/lib/wx/c_src/wxe_helpers.cpp
blob: d8cca316542f0d89e3b034e9cfbf4dfd94d2b1a5 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2014-2022. 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%
 */

#include <wx/wx.h>
#include "wxe_impl.h"

/* ****************************************************************************
 * Erlang Commands
 * ****************************************************************************/

wxeCommand::wxeCommand()
{
  env = enif_alloc_env();
}

wxeCommand::~wxeCommand()
{
  if(env)
    enif_free_env(env);
}

void wxeCommand::Init(int u_argc, const ERL_NIF_TERM u_argv[], int u_op, wxe_me_ref *u_mr, ErlNifPid u_caller)
{
  op  = u_op;
  caller = u_caller;

  argc = u_argc;
  for(int i=0; i<argc; i++)
    args[i] = enif_make_copy(env, u_argv[i]);
  me_ref = u_mr;
}


/* ****************************************************************************
 * wxeFifo
 * ****************************************************************************/
wxeFifo::wxeFifo(unsigned int sz)
{
  size = 0;
}

wxeFifo::~wxeFifo() {

  for(std::deque<wxeCommand *>::iterator it = m_q.begin(); it != m_q.end(); ++it) {
    delete *it;
  }

  for(std::vector<wxeCommand *>::iterator it = free.begin(); it != free.end(); ++it) {
    delete *it;
 }

}

wxeCommand * wxeFifo::Get()
{
  wxeCommand * cmd;
  do {
    if(m_q.empty())
      return NULL;
    else {
      cmd = m_q.front();
      m_q.pop_front();
    }
  } while (cmd == NULL);
  size--;
  return cmd;
}

int wxeFifo::Add(int argc, const ERL_NIF_TERM argv[], int op, wxe_me_ref *mr, ErlNifPid caller)
{
  wxeCommand * curr;
  if(free.empty()) {
    curr = new wxeCommand();
  } else {
    curr = free.back();
    free.pop_back();
  }
  curr->Init(argc, argv, op, mr, caller);
  m_q.push_back(curr);
  size++;
  return size;
}

void wxeFifo::DelQueue(unsigned int i)
{
  size--;
  m_q[i] = NULL;
}


void wxeFifo::DeleteCmd(wxeCommand *orig)
{
  orig->op = -2;  // Assert: will crash if op is negative
  enif_clear_env(orig->env);
  free.push_back(orig);
}

unsigned int wxeFifo::Size()
{
  return size;
}

void wxeFifo::Append(wxeCommand *orig)
{
  wxeCommand * curr;
  if(free.empty()) {
    curr = new wxeCommand();
  } else {
    curr = free.back();
    free.pop_back();
  }

  curr->op  = orig->op;
  curr->caller = orig->caller;
  curr->argc = orig->argc;
  for(int i=0; i<curr->argc; i++)
    curr->args[i] = orig->args[i];
  ErlNifEnv * temp = curr->env;
  curr->env = orig->env;
  orig->env = temp;
  curr->me_ref = orig->me_ref;
  orig->op = -1; // Assert: will crash if op is negative
  m_q.push_back(curr);
}