1 | #ifndef _ASSOCIATE_H_ |
---|
2 | #define _ASSOCIATE_H_ |
---|
3 | |
---|
4 | #include <map> |
---|
5 | #include <list> |
---|
6 | #include <string> |
---|
7 | #include <vector> |
---|
8 | #include <iostream> |
---|
9 | #include <stdexcept> |
---|
10 | #include <algorithm> |
---|
11 | |
---|
12 | #include "SynTree/Expression.h" |
---|
13 | #include "diet_map.h" |
---|
14 | |
---|
15 | class Association; |
---|
16 | class SingleName; |
---|
17 | class PointAssociation; |
---|
18 | class RangeAssociation; |
---|
19 | |
---|
20 | // ** exceptions |
---|
21 | class AssocException : public std::exception { |
---|
22 | public: |
---|
23 | AssocException() {} |
---|
24 | AssocException( std::string _what ) : what( _what ) {} |
---|
25 | ~AssocException() throw () {} |
---|
26 | |
---|
27 | std::string get_what() const { return what; } |
---|
28 | void set_what( std::string newValue ) { what = newValue; } |
---|
29 | private: |
---|
30 | std::string what; |
---|
31 | }; |
---|
32 | |
---|
33 | // ** visitors |
---|
34 | class AssociationVisitor { |
---|
35 | public: |
---|
36 | AssociationVisitor() {} |
---|
37 | virtual ~AssociationVisitor() {} |
---|
38 | |
---|
39 | virtual void visit( SingleName * ) = 0; |
---|
40 | virtual void visit( PointAssociation * ) = 0; |
---|
41 | virtual void visit( RangeAssociation * ) = 0; |
---|
42 | }; |
---|
43 | |
---|
44 | // ** containers |
---|
45 | class Association { |
---|
46 | public: |
---|
47 | virtual ~Association(); |
---|
48 | |
---|
49 | virtual Association *clone() = 0; |
---|
50 | virtual long int add_single( std::string, Expression *) = 0; |
---|
51 | virtual long int add_single( long int, Expression *expr) = 0; |
---|
52 | |
---|
53 | virtual Association *operator[]( int idx ) = 0; |
---|
54 | virtual Association *operator[]( std::string ) = 0; |
---|
55 | |
---|
56 | // virtual AssociationIterator *get_iterator() = 0; |
---|
57 | |
---|
58 | virtual void accept( AssociationVisitor & ) = 0; |
---|
59 | virtual void display( std::ostream & ) = 0; |
---|
60 | }; |
---|
61 | |
---|
62 | class SingleName : public Association { |
---|
63 | public: |
---|
64 | SingleName( Expression *initExpr = 0 ) : expr( initExpr ) {} |
---|
65 | virtual ~SingleName(); |
---|
66 | |
---|
67 | virtual SingleName *clone() { |
---|
68 | return 0; // XXX! |
---|
69 | } |
---|
70 | |
---|
71 | virtual long int add_single( long int idx, Expression *newExpr) { |
---|
72 | if ( expr == 0 ) //|| *expr == *newExpr ) |
---|
73 | expr = newExpr; |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | virtual long int add_single( std::string str, Expression *newExpr) { |
---|
78 | if ( expr == 0 ) //|| *expr == *newExpr ) |
---|
79 | expr = newExpr; |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | virtual Association *operator[]( int idx ) { assert(false); } |
---|
84 | virtual Association *operator[]( std::string idx ) { assert(false); } |
---|
85 | |
---|
86 | virtual void accept( AssociationVisitor &v ) { v.visit( this ); } |
---|
87 | virtual void display( std::ostream &os ) { |
---|
88 | os << "Single association" << std::endl; |
---|
89 | } |
---|
90 | |
---|
91 | Expression *get_expr() const { return expr; } |
---|
92 | |
---|
93 | private: |
---|
94 | Expression *expr; |
---|
95 | Expression *deflt; |
---|
96 | }; |
---|
97 | |
---|
98 | class PointAssociation : public Association { |
---|
99 | public: |
---|
100 | typedef std::map< std::string, std::pair< long int, Association *> > map_type; |
---|
101 | |
---|
102 | PointAssociation() {} |
---|
103 | PointAssociation( const PointAssociation &other ) { |
---|
104 | copy( other.anonym.begin(), other.anonym.end(), back_inserter( anonym )); |
---|
105 | } |
---|
106 | |
---|
107 | virtual ~PointAssociation(); |
---|
108 | |
---|
109 | virtual PointAssociation *clone() { |
---|
110 | return ( new PointAssociation( *this ) ); |
---|
111 | } |
---|
112 | |
---|
113 | virtual long int add_single( long int idx, Expression *expr) { |
---|
114 | long int ret; |
---|
115 | |
---|
116 | if ( idx >= (long int)ordering.size() ) throw AssocException("extra (spurious) members"); |
---|
117 | |
---|
118 | if ( ordering[ idx ] == "") |
---|
119 | std::cerr << "Checkpoint 2" << std::endl; |
---|
120 | else { |
---|
121 | assert( table[ordering[idx]].second != 0 ); |
---|
122 | ret = idx; |
---|
123 | table[ ordering[idx] ].second->add_single("", expr ); |
---|
124 | } |
---|
125 | return ret; |
---|
126 | } |
---|
127 | |
---|
128 | virtual long int add_single( std::string idx, Expression *expr) { |
---|
129 | if ( idx == "" ) |
---|
130 | std::cerr << "Checkpoint 1" << std::endl; |
---|
131 | else { |
---|
132 | map_type::iterator j; |
---|
133 | if ( (j = table.find( idx )) == table.end() ) // this doesn't amount for reachable members deeper down the structure, fix |
---|
134 | throw AssocException("No such member"); |
---|
135 | else |
---|
136 | return add_single( j->second.first, expr ); |
---|
137 | } |
---|
138 | |
---|
139 | return -1; |
---|
140 | } |
---|
141 | |
---|
142 | void add_member( std::string str ) { |
---|
143 | if ( table.find( str ) != table.end() ) return; |
---|
144 | ordering.push_back( str ); |
---|
145 | if ( str != "" ) { |
---|
146 | std::pair<long int, Association *> p( ordering.size() - 1, 0 ); |
---|
147 | table.insert( std::pair< std::string, std::pair<long int, Association *> >(str, p) ); |
---|
148 | } |
---|
149 | return; |
---|
150 | } |
---|
151 | |
---|
152 | virtual void set_member( std::string str, Association *assoc ) { |
---|
153 | if ( str == "" ) |
---|
154 | anonym.push_back( assoc ); |
---|
155 | else if ( table.find( str ) == table.end() ) |
---|
156 | throw AssocException( "no such member" ); |
---|
157 | else |
---|
158 | table[ str ] = std::pair<long int, Association *>(ordering.size() - 1, assoc); |
---|
159 | |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | virtual Association *operator[]( int idx ) { |
---|
164 | if ( ordering[idx] == "" ) { |
---|
165 | std::cerr << "Error, anonymous members not implemented yet" << std::endl; |
---|
166 | throw 0; |
---|
167 | } else |
---|
168 | return table[ ordering[idx] ].second; |
---|
169 | } |
---|
170 | |
---|
171 | virtual Association *operator[]( std::string idx ) { |
---|
172 | if ( table.find( idx ) == table.end() ) |
---|
173 | throw AssocException("Member not found"); |
---|
174 | else |
---|
175 | return table[ idx ].second; |
---|
176 | } |
---|
177 | |
---|
178 | /* |
---|
179 | virtual AssociationIterator *get_iterator() { |
---|
180 | PointAssocIterator *it; |
---|
181 | return it; |
---|
182 | } |
---|
183 | */ |
---|
184 | |
---|
185 | void accept( AssociationVisitor &v ) { v.visit( this ); } |
---|
186 | |
---|
187 | virtual void display( std::ostream &os ) { |
---|
188 | os << "Point association: " << std::endl; |
---|
189 | for ( map_type::iterator i = table.begin(); i != table.end(); i++ ) { |
---|
190 | os << "Member [" << i->first << ", index = " << i->second.first << "]"; |
---|
191 | if ( i->second.second != 0 ) |
---|
192 | i->second.second->display( os ); |
---|
193 | else |
---|
194 | std::cerr << "No recursive association" << std::endl; |
---|
195 | |
---|
196 | os << std::endl; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | const int size() const { return ordering.size(); } |
---|
201 | |
---|
202 | private: |
---|
203 | PointAssociation &operator=(const PointAssociation &); |
---|
204 | std::vector<std::string> ordering; |
---|
205 | std::list< Association * > anonym; |
---|
206 | std::map< std::string, std::pair<long int, Association *> > table; |
---|
207 | }; |
---|
208 | |
---|
209 | class RangeAssociation : public Association { |
---|
210 | public: |
---|
211 | static const int UNDEF; |
---|
212 | RangeAssociation( int _hi= UNDEF ) : hi( _hi ) { |
---|
213 | std::cerr << "Constructed RangeAssociation with: [" << hi << "]" << std::endl; |
---|
214 | } |
---|
215 | |
---|
216 | virtual ~RangeAssociation(); |
---|
217 | |
---|
218 | virtual RangeAssociation *clone() { |
---|
219 | return 0; // XXX !!!! |
---|
220 | } |
---|
221 | |
---|
222 | virtual Association *operator[]( int idx ) { |
---|
223 | return 0; // XXX !!! |
---|
224 | } |
---|
225 | |
---|
226 | virtual Association *operator[]( std::string idx ) { assert(false); return 0; } |
---|
227 | |
---|
228 | /* |
---|
229 | virtual AssociationIterator *get_iterator() { |
---|
230 | RangeAssocIterator *it; |
---|
231 | return it; |
---|
232 | } |
---|
233 | */ |
---|
234 | |
---|
235 | virtual long int add_single( long int idx, Expression *newExpr) { return 0; } |
---|
236 | virtual long int add_single( std::string, Expression *) { return 0; } |
---|
237 | void accept( AssociationVisitor &v ) { v.visit( this ); } |
---|
238 | virtual void display( std::ostream &os ) { |
---|
239 | os << "Range association, with limit: " << std::endl; |
---|
240 | } |
---|
241 | |
---|
242 | private: |
---|
243 | int hi; |
---|
244 | diet::diet_tree<int> tree; |
---|
245 | /* |
---|
246 | for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ ) |
---|
247 | std::cout << "--(" << (*i).first << ", " << (*i).second << ")--" << std::endl; |
---|
248 | diet_tree<int> tree; |
---|
249 | tree.insert(100,200); |
---|
250 | */ |
---|
251 | }; |
---|
252 | |
---|
253 | // ** builders |
---|
254 | class AssociationBuilder { |
---|
255 | public: |
---|
256 | /* AssociationBuilder( Declaration * ) */ |
---|
257 | virtual ~AssociationBuilder() {} |
---|
258 | virtual Association *get_assoc() = 0; |
---|
259 | virtual Association *grab_assoc() = 0; |
---|
260 | virtual void set_assoc( Association * ) = 0; |
---|
261 | }; |
---|
262 | |
---|
263 | class AssociationFiller { |
---|
264 | public: |
---|
265 | // AssociationFiller( Declaration * ) {} |
---|
266 | virtual ~AssociationFiller() {} |
---|
267 | virtual Association *get_assoc() = 0; |
---|
268 | virtual void set_assoc( Association * ) = 0; |
---|
269 | }; |
---|
270 | |
---|
271 | #endif //#define _ASSOCIATE_H_ |
---|
272 | |
---|
273 | /* |
---|
274 | Local Variables: |
---|
275 | mode: c++ |
---|
276 | End: |
---|
277 | */ |
---|