1 | #ifndef _BOUBBLE_HH_ |
---|
2 | #define _BOUBBLE_HH_ |
---|
3 | |
---|
4 | #include <list> |
---|
5 | #include <iostream> |
---|
6 | #include <sstream> |
---|
7 | |
---|
8 | #include "thesymbols.hh" |
---|
9 | |
---|
10 | |
---|
11 | enum Dir {UP=0,DOWN=1,AT_TARGET=2}; |
---|
12 | |
---|
13 | // class BoubbleAction |
---|
14 | // { |
---|
15 | // public: |
---|
16 | // void apply() {}; |
---|
17 | // private: |
---|
18 | |
---|
19 | // }; |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | class Boubble |
---|
24 | { |
---|
25 | public: |
---|
26 | Boubble(list<Role> u, list<Role> d, LongRel l, int s=-1); |
---|
27 | Boubble(const char* pathstr, const char* l, int s=-1); |
---|
28 | |
---|
29 | Dir dir(); |
---|
30 | LongRel rel(); |
---|
31 | int src(); |
---|
32 | void src(int s); |
---|
33 | |
---|
34 | Role next(); |
---|
35 | |
---|
36 | Boubble* step(Role r, Dir d); |
---|
37 | |
---|
38 | bool is_at_target(); |
---|
39 | |
---|
40 | bool operator==(Boubble const& b) const; |
---|
41 | bool operator!=(Boubble const& b) const; |
---|
42 | bool operator<(Boubble const& b) const; |
---|
43 | |
---|
44 | void as_cstr(char* s); |
---|
45 | friend std::ostream& operator<<(std::ostream&, const Boubble&); |
---|
46 | |
---|
47 | private: |
---|
48 | |
---|
49 | int _src; |
---|
50 | list<Role> _upath; |
---|
51 | list<Role> _dpath; |
---|
52 | LongRel _rel; |
---|
53 | |
---|
54 | }; |
---|
55 | |
---|
56 | //---------------------------------------------------------------------------------------------------- |
---|
57 | |
---|
58 | inline |
---|
59 | Boubble::Boubble(list<Role> u, list<Role> d, LongRel l, int s) : _upath(u), _dpath(d), _rel(l), _src(s) {} |
---|
60 | |
---|
61 | //---------------------------------------------------------------------------------------------------- |
---|
62 | |
---|
63 | inline |
---|
64 | Boubble::Boubble(const char* pathstr, const char* l, int s) |
---|
65 | { |
---|
66 | Dir dir = UP; |
---|
67 | const char* p = pathstr; |
---|
68 | while(*p) |
---|
69 | { |
---|
70 | if(*p=='^') { dir = DOWN; p++; } |
---|
71 | else if(isalpha(*p)) |
---|
72 | { |
---|
73 | char buf[80]; |
---|
74 | sscanf(p,"%[a-zA-Z0-9_]",buf); |
---|
75 | dir == UP ? _upath.push_back(Role(buf)) : _dpath.push_back(Role(buf)); |
---|
76 | p += strlen(buf); |
---|
77 | } |
---|
78 | else |
---|
79 | p++; |
---|
80 | } |
---|
81 | |
---|
82 | _rel = LongRel(l); |
---|
83 | _src = s; |
---|
84 | } |
---|
85 | |
---|
86 | //---------------------------------------------------------------------------------------------------- |
---|
87 | |
---|
88 | inline |
---|
89 | Dir Boubble::dir() |
---|
90 | { |
---|
91 | if(!_upath.empty()) |
---|
92 | return UP; |
---|
93 | else if(!_dpath.empty()) |
---|
94 | return DOWN; |
---|
95 | else return AT_TARGET; |
---|
96 | } |
---|
97 | |
---|
98 | //---------------------------------------------------------------------------------------------------- |
---|
99 | |
---|
100 | inline |
---|
101 | LongRel Boubble::rel() |
---|
102 | { return _rel; } |
---|
103 | |
---|
104 | //---------------------------------------------------------------------------------------------------- |
---|
105 | |
---|
106 | inline |
---|
107 | int Boubble::src() |
---|
108 | { return _src; } |
---|
109 | |
---|
110 | //---------------------------------------------------------------------------------------------------- |
---|
111 | |
---|
112 | inline |
---|
113 | void Boubble::src(int s) |
---|
114 | { _src=s; } |
---|
115 | |
---|
116 | //---------------------------------------------------------------------------------------------------- |
---|
117 | |
---|
118 | inline |
---|
119 | Role Boubble::next() |
---|
120 | { |
---|
121 | if(!_upath.empty()) |
---|
122 | return _upath.front(); |
---|
123 | else if(!_dpath.empty()) |
---|
124 | return _dpath.front(); |
---|
125 | else return Role("NULL"); |
---|
126 | } |
---|
127 | |
---|
128 | //---------------------------------------------------------------------------------------------------- |
---|
129 | |
---|
130 | inline |
---|
131 | Boubble* Boubble::step(Role r, Dir d) |
---|
132 | { |
---|
133 | if(d==UP && !_upath.empty() && _upath.front() == r) |
---|
134 | { |
---|
135 | Boubble* newboubble = new Boubble(_upath,_dpath,_rel,_src); |
---|
136 | newboubble->_upath.pop_front(); |
---|
137 | return newboubble; |
---|
138 | } |
---|
139 | |
---|
140 | if(d==DOWN && _upath.empty() && !_dpath.empty()) |
---|
141 | { |
---|
142 | Boubble* newboubble = new Boubble(_upath,_dpath,_rel,_src); |
---|
143 | newboubble->_dpath.pop_front(); |
---|
144 | return newboubble; |
---|
145 | } |
---|
146 | return NULL; |
---|
147 | } |
---|
148 | |
---|
149 | //---------------------------------------------------------------------------------------------------- |
---|
150 | |
---|
151 | inline |
---|
152 | bool Boubble::is_at_target() |
---|
153 | { return _upath.empty() && _dpath.empty(); } |
---|
154 | |
---|
155 | //---------------------------------------------------------------------------------------------------- |
---|
156 | |
---|
157 | inline |
---|
158 | bool Boubble::operator==(Boubble const& b) const |
---|
159 | { |
---|
160 | return _src==b._src && _upath==b._upath && _dpath==b._dpath && _rel==b._rel; |
---|
161 | } |
---|
162 | |
---|
163 | //---------------------------------------------------------------------------------------------------- |
---|
164 | |
---|
165 | inline |
---|
166 | bool Boubble::operator!=(Boubble const& b) const |
---|
167 | { |
---|
168 | return !(*this==b); |
---|
169 | } |
---|
170 | |
---|
171 | //---------------------------------------------------------------------------------------------------- |
---|
172 | |
---|
173 | inline |
---|
174 | bool Boubble::operator<(Boubble const& b) const |
---|
175 | { |
---|
176 | if(_src < b._src) return true; |
---|
177 | if(_rel < b._rel) return true; |
---|
178 | if(this < &b) return true; |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | //---------------------------------------------------------------------------------------------------- |
---|
183 | |
---|
184 | inline |
---|
185 | std::ostream& operator<<(std::ostream& o, const Boubble& b) |
---|
186 | { |
---|
187 | o << "["; |
---|
188 | |
---|
189 | o << b._src << "|"; |
---|
190 | |
---|
191 | bool cont=false; |
---|
192 | for(list<Role>::const_iterator i = b._upath.begin(); i != b._upath.end(); ++i) |
---|
193 | { |
---|
194 | if(cont) o << ','; |
---|
195 | o << i->str(); |
---|
196 | cont = true; |
---|
197 | } |
---|
198 | o << '^'; |
---|
199 | cont=false; |
---|
200 | for(list<Role>::const_iterator i = b._dpath.begin(); i != b._dpath.end(); ++i) |
---|
201 | { |
---|
202 | if(cont) o << ','; |
---|
203 | o << i->str(); |
---|
204 | cont = true; |
---|
205 | } |
---|
206 | o << ':'; |
---|
207 | o << b._rel.str(); |
---|
208 | o << "]"; |
---|
209 | } |
---|
210 | |
---|
211 | //---------------------------------------------------------------------------------------------------- |
---|
212 | |
---|
213 | inline |
---|
214 | void Boubble::as_cstr(char* s) |
---|
215 | { |
---|
216 | stringstream oss; |
---|
217 | oss << *this; |
---|
218 | strcpy(s,oss.str().c_str()); |
---|
219 | } |
---|
220 | |
---|
221 | //==================================================================================================== |
---|
222 | |
---|
223 | #endif |
---|