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