PStack.h

Go to the documentation of this file.
00001 #ifndef _PSTACK
00002 #define _PSTACK
00003 
00004 #include "global.h"
00005 #include "MercuryVector.h"
00006 #include "MercuryString.h"
00007 
00009 
00011 class PSElement
00012 {
00013 public:
00014     PSElement( const MString & sI ) { data.dataS = new MString(sI); type = STRING; }
00015     PSElement( const char * sI ) { data.dataS = new MString(sI); type = STRING; }
00016     PSElement( const char * sI, int Length ) { data.dataS = new MString; data.dataS->append( sI, Length ); type = STRING; }
00017     PSElement( float fI ) { data.f = fI; type = FLOAT; }
00018     PSElement( int iI ) { data.l = iI; type = INTEGER; }
00019     PSElement( unsigned iI ) { data.l = iI; type = INTEGER; }
00020     PSElement( bool bI ) { data.b = bI; type = BOOL; }
00021     PSElement( void * vI ) { data.v = vI; type = USERDATA; }
00022     PSElement( void ) { type = NIL; }
00023 
00024     PSElement( const PSElement & RHS ) { data = RHS.data; type = RHS.type; if ( type == STRING ) data.dataS = new MString( *RHS.data.dataS ); }
00025     PSElement & operator = ( const PSElement & RHS ) { data = RHS.data; type = RHS.type; if ( type == STRING ) data.dataS = new MString( *RHS.data.dataS ); return *this; }
00026 
00027     ~PSElement() { if ( type == STRING ) delete data.dataS; }
00028 
00029     MString GetValueS() const;
00030     int     GetValueI() const;
00031     float   GetValueF() const;
00032     bool    GetValueB() const;
00033     void *  GetValueV() const;
00034 
00035     inline operator MString() const { return GetValueS(); }
00036     inline operator int() const { return GetValueI(); }
00037     inline operator float() const { return GetValueF(); }
00038     inline operator bool() const { return GetValueB(); }
00039     inline operator void*() const { return GetValueV(); }
00040 
00041     bool    GetValueS( MString & ret );
00042     bool    GetValueI( long & ret );
00043     bool    GetValueF( float & ret );
00044     bool    GetValueB( bool & ret );
00045     bool    GetValueV( void * & ret );
00046 
00047     enum ParmType
00048     {
00049         NIL = 0,
00050         BOOL,
00051         FLOAT,  //In LUA, float == NUMBER
00052         INTEGER,
00053         STRING,
00054         PARAMETER,
00055         USERDATA,
00056     };
00057 
00058     ParmType    GetType() const;
00059     void        ForceType( ParmType tI ) { type = tI; } 
00060     PSElement( ParmType tI ) { type = tI; }
00061 
00062     PSElement Operate( const MString & sOperator, const PSElement & pRHS );
00063 
00064     void    SetValueF( const float fToSet ) { data.f = fToSet; }
00065 private:
00066     ParmType type;
00067     union ParmData
00068     {
00069         float   f;
00070         long    l;
00071         bool    b;
00072         void *  v;
00073         MString * dataS;
00074     } data;
00075 };
00076 
00078 class PStack
00079 {
00080 public:
00081     PStack( ) { };
00082     PStack( const char * sIn, int iMaxLength = 32767 );
00083     PStack( PSElement p1 );
00084     PStack( PSElement p1, PSElement p2 );
00085     PStack( PSElement p1, PSElement p2, PSElement p3 );
00086     PStack( PSElement p1, PSElement p2, PSElement p3, PSElement p4 );
00087     ~PStack();
00088 
00089     void PushItem( const PSElement & in );
00090     void PushItemBack( const PSElement & in );
00091     bool PopItem( PSElement & out );
00092     PSElement PopItem();
00093     bool PeekItem( PSElement & out );
00094     const PSElement& PeekItem( unsigned int iAhead = 0 ) const;
00095     inline const PSElement& operator[] ( unsigned int iAhead ) const { return PeekItem(iAhead); }
00096     void Clear() { m_stack.clear(); }
00097 
00098     inline unsigned int GetSize() const { return m_stack.size(); }
00099 private:
00100     MVector< PSElement > m_stack;
00101 };
00102 
00103 //Managed PStacks, in general this should only be used by applications that cannot use C++ classes.
00104 int HGEXPORT CreateMPStack();
00105 int HGEXPORT GetMPStackSize( unsigned int iMPS );
00106 const char * HGEXPORT GetMPStackAtS(  unsigned int iMPS, unsigned int iPL );    //Returns reference (DO NOT DELETE)
00107 int HGEXPORT GetMPStackAtI(  unsigned int iMPS, unsigned int iPL );
00108 float HGEXPORT GetMPStackAtF(  unsigned int iMPS, unsigned int iPL );
00109 void HGEXPORT PushMPStackBackS(  unsigned int iMPS, const char * s );
00110 void HGEXPORT PushMPStackBackI(  unsigned int iMPS, int i );
00111 void HGEXPORT PushMPStackBackF(  unsigned int iMPS, float f );
00112 void HGEXPORT DeleteMPStack(  unsigned int iMPS );
00113 void HGEXPORT FlushMPStacks();
00114 //Utility
00115 PStack * GetMPStack(  unsigned int iMPS );
00116 
00117 #endif
00118 /* 
00119  * Copyright (c) 2006 Charles Lohr
00120  * All rights reserved.
00121  *
00122  * Redistribution and use in source and binary forms, with or
00123  * without modification, are permitted provided that the following
00124  * conditions are met:
00125  *  -   Redistributions of source code must retain the above
00126  *      copyright notice, this list of conditions and the following disclaimer.
00127  *  -   Redistributions in binary form must reproduce the above copyright
00128  *      notice, this list of conditions and the following disclaimer in
00129  *      the documentation and/or other materials provided with the distribution.
00130  *  -   Neither the name of the Mercury Engine nor the names of its
00131  *      contributors may be used to endorse or promote products derived from
00132  *      this software without specific prior written permission.
00133  *
00134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00135  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00136  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00137  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00138  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00139  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00140  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00141  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00142  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00143  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00144  */

Hosted by SourceForge.net Logo