"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.193/src/libnxsl/stack.cpp" (4 Mar 2021, 1920 Bytes) of package /linux/misc/netxms-3.8.193.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stack.cpp" see the Fossies "Dox" file reference documentation.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** NetXMS Scripting Language Interpreter
    4 ** Copyright (C) 2003-2015 Victor Kirhenshtein
    5 **
    6 ** This program is free software; you can redistribute it and/or modify
    7 ** it under the terms of the GNU Lesser General Public License as published by
    8 ** the Free Software Foundation; either version 3 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 Lesser 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 ** File: stack.cpp
   21 **
   22 **/
   23 
   24 #include "libnxsl.h"
   25 
   26 /**
   27  * Constructor
   28  */
   29 NXSL_Stack::NXSL_Stack()
   30 {
   31    m_nStackSize = 128;
   32    m_nStackPos = 0;
   33    m_ppData = (void **)malloc(sizeof(void *) * m_nStackSize);
   34 }
   35 
   36 /**
   37  * Destructor
   38  */
   39 NXSL_Stack::~NXSL_Stack()
   40 {
   41    free(m_ppData);
   42 }
   43 
   44 /**
   45  * Push value to stack
   46  */
   47 void NXSL_Stack::push(void *pData)
   48 {
   49    if (m_nStackPos >= m_nStackSize)
   50    {
   51       m_nStackSize += 64;
   52       m_ppData = (void **)realloc(m_ppData, sizeof(void *) * m_nStackSize);
   53    }
   54    m_ppData[m_nStackPos++] = pData;
   55 }
   56 
   57 /**
   58  * Pop value from stack
   59  */
   60 void *NXSL_Stack::pop()
   61 {
   62    if (m_nStackPos > 0)
   63       return m_ppData[--m_nStackPos];
   64    return NULL;
   65 }
   66 
   67 /**
   68  * Peek (get without removing) value from stack
   69  */
   70 void *NXSL_Stack::peek()
   71 {
   72    if (m_nStackPos > 0)
   73       return m_ppData[m_nStackPos - 1];
   74    return NULL;
   75 }
   76 
   77 /**
   78  * Peek (get without removing) value from stack at given offset from top
   79  */
   80 void *NXSL_Stack::peekAt(int offset)
   81 {
   82    if ((offset > 0) && (m_nStackPos > offset - 1))
   83       return m_ppData[m_nStackPos - offset];
   84    return NULL;
   85 }