source: git/factory/parseutil.cc @ 8d2c11

spielwiese
Last change on this file since 8d2c11 was 362fc67, checked in by Martin Lee <martinlee84@…>, 12 years ago
chg: remove $Id$
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#include "config.h"
4
5#include <string.h>
6#include <stdlib.h>
7
8#include "cf_assert.h"
9
10#include "cf_defs.h"
11#include "parseutil.h"
12
13class PUtilBase
14{
15public:
16    PUtilBase() {};
17    virtual ~PUtilBase() {}
18    virtual PUtilBase * copy() const = 0;
19    virtual CanonicalForm getval() const = 0;
20    virtual int getintval() const = 0;
21    virtual bool isInt() const = 0;
22    virtual bool isCF() const = 0;
23    virtual bool isVar() const = 0;
24};
25
26class PUtilInt : public PUtilBase
27{
28private:
29    int val;
30public:
31    PUtilInt() { val = 0; }
32    PUtilInt( int i ) { val = i; }
33    ~PUtilInt() {}
34    PUtilBase * copy() const { return new PUtilInt( val ); }
35    CanonicalForm getval() const { return CanonicalForm( val ); }
36    int getintval() const { return val; }
37    bool isInt() const { return true; }
38    bool isCF() const { return false; }
39    bool isVar() const { return false; }
40};
41
42class PUtilCF : public PUtilBase
43{
44private:
45    CanonicalForm val;
46public:
47    PUtilCF() { val = 0; }
48    PUtilCF( const CanonicalForm & cf ) { val = cf; }
49    ~PUtilCF() {}
50    PUtilBase * copy() const { return new PUtilCF( val ); }
51    CanonicalForm getval() const { return val; }
52    int getintval() const { return val.intval(); }
53    bool isInt() const { return false; }
54    bool isCF() const { return true; }
55    bool isVar() const { return false; }
56};
57
58class PUtilVar : public PUtilBase
59{
60private:
61    Variable val;
62public:
63    PUtilVar() { val = Variable(); }
64    PUtilVar( const Variable & v ) { val = v; }
65    ~PUtilVar() {}
66    PUtilBase * copy() const { return new PUtilVar( val ); }
67    CanonicalForm getval() const { return CanonicalForm( val ); }
68    int getintval() const { return 0; }
69    bool isInt() const { return false; }
70    bool isCF() const { return false; }
71    bool isVar() const { return true; }
72};
73
74class PUtilFactory
75{
76public:
77    static PUtilBase * create( ) { return new PUtilInt( 0 ); }
78    static PUtilBase * create( int val ) { return new PUtilInt( val ); }
79    static PUtilBase * create( const CanonicalForm & cf ) { return new PUtilCF( cf ); }
80    static PUtilBase * create( const Variable & v ) { return new PUtilVar( v ); }
81    static PUtilBase * create( const char * str )
82    {
83        if ( strlen( str ) < 9 )
84            return new PUtilInt( atoi( str ) );
85        else
86            return new PUtilCF( CanonicalForm( str ) );
87    }
88};
89
90ParseUtil::ParseUtil()
91{
92    value = PUtilFactory::create();
93}
94
95ParseUtil::ParseUtil( const ParseUtil &pu )
96{
97    value = pu.value->copy();
98}
99
100ParseUtil::ParseUtil( const CanonicalForm &f )
101{
102    value = PUtilFactory::create( f );
103}
104
105ParseUtil::ParseUtil( int i )
106{
107    value = PUtilFactory::create( i );
108}
109
110ParseUtil::ParseUtil( char * str )
111{
112    value = PUtilFactory::create( str );
113}
114
115ParseUtil::~ParseUtil()
116{
117    delete value;
118}
119
120ParseUtil& ParseUtil::operator= ( const ParseUtil &pu )
121{
122    if ( this != &pu ) {
123        delete value;
124        value = pu.value->copy();
125    }
126    return *this;
127}
128
129ParseUtil& ParseUtil::operator= ( const CanonicalForm &f )
130{
131    delete value;
132    value = PUtilFactory::create( f );
133    return *this;
134}
135
136ParseUtil& ParseUtil::operator= ( int i )
137{
138    delete value;
139    value = PUtilFactory::create( i );
140    return *this;
141}
142
143ParseUtil& ParseUtil::operator= ( const Variable & v )
144{
145    delete value;
146    value = PUtilFactory::create( v );
147    return *this;
148}
149
150CanonicalForm ParseUtil::getval()
151{
152    return value->getval();
153}
154
155int ParseUtil::getintval()
156{
157    return value->getintval();
158}
Note: See TracBrowser for help on using the repository browser.