Listing 6: The stack calculator engine
#include <factory.h> #include "engine.h" #include <iostream> #include <string> #include <stack> using namespace std; #ifdef _WIN32 string const factory_dll( "dll.dll" ); #elif defined( _DLSYM ) string const factory_dll( "./dll" ); #else #error Unsupported Platform. Please port me #endif class stackimpl : public calculator_stack { public: virtual void push( int i ) { m_data.push( i ); } virtual int pop( ) { if ( m_data.size() ) { int i = top( ); m_data.pop( ); return i; } return 0; } // pop( ) virtual int top( ) { if ( m_data.size() ) return m_data.top( ); else return 0; } // top( ) virtual int size( ) { return m_data.size(); } private: stack< int > m_data; }; // stackimpl typedef factory< operation > op_factory; int main( int , char** ) { stackimpl numbers; string line; op_factory factory( factory_dll , STRING( FACTORY_PREFIX ) ); while( 1 ) { cin >> line; if ( line == "bye" ) break; if ( factory.class_exists( line ) ) { op_factory::object_ptr object( factory.instantiate( line ) ); cout << object->operate( &numbers ) << endl; } else { int number = atoi( line.c_str() ); numbers.push( number ); } } return 0; }