d7a56d4ab8c24cfb9684af3cc85f92c86dec3a7f
[public/netxms.git] / src / libnxsl / stack.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2005 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** $module: stack.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // Constructor
29 //
30
31 NXSL_Stack::NXSL_Stack()
32 {
33 m_nStackSize = 128;
34 m_nStackPos = 0;
35 m_ppData = (void **)malloc(sizeof(void *) * m_nStackSize);
36 }
37
38
39 //
40 // Destructor
41 //
42
43 NXSL_Stack::~NXSL_Stack()
44 {
45 free(m_ppData);
46 }
47
48
49 //
50 // Push value to stack
51 //
52
53 void NXSL_Stack::push(void *pData)
54 {
55 if (m_nStackPos >= m_nStackSize)
56 {
57 m_nStackSize += 64;
58 m_ppData = (void **)realloc(m_ppData, sizeof(void *) * m_nStackSize);
59 }
60 m_ppData[m_nStackPos++] = pData;
61 }
62
63
64 //
65 // Pop value from stack
66 //
67
68 void *NXSL_Stack::pop()
69 {
70 if (m_nStackPos > 0)
71 return m_ppData[--m_nStackPos];
72 return NULL;
73 }
74
75
76 //
77 // Peek (get without removing) value from stack
78 //
79
80 void *NXSL_Stack::peek()
81 {
82 if (m_nStackPos > 0)
83 return m_ppData[m_nStackPos - 1];
84 return NULL;
85 }