SOL9 2.0 Class: Stack

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Stack.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
#pragma once

#include <sol\Object.h>
#include <sol\InvalidArgumentException.h>
//#include <sol\OutOfRangeException.h>

namespace SOL {

class Stack :public Object {
private:
    Object** array;
    unsigned int sp;
    unsigned int size;

public:
    Stack(unsigned int size1 = 100)
        :size(size1), array(NULL), sp(0)
    {
        if (size>0) {
            array = new Object*[size];
        } else {
            throw InvalidArgumentException("Stack::Stack,1,InvaliArgument");
        }
    }

public:
    Stack::~Stack()
    {
        if (array) {
            delete [] array;
            array = NULL;
        }
    }

public:
    void push(Object* object) 
    {
        if(sp < size) {
            array[sp++] = object;
        }
    }

public:
    Object* Stack::pop() 
    {
        Object* object = NULL;
        if(sp >0) {
            object = array[--sp];
        }
        return object;
    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.