Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
3 | // All rights reserved. This program and the accompanying materials |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #ifndef _Ttcn_Quadruple_HH | |
9 | #define _Ttcn_Quadruple_HH | |
10 | ||
11 | class QuadInterval; | |
12 | ||
13 | /** \class Quad | |
14 | * \brief Represents a quadruple. | |
15 | * | |
16 | * The stored value can be accessed as an integer or as fields of the | |
17 | * quadruple. The aim is to help the generation of the posix regular expression | |
18 | * from TTCN-3 pattern of type universal charstring. This class can create the | |
19 | * string representation needed by pattern matching functions. | |
20 | * | |
21 | * <b>The string representation:</b> | |
22 | * Every field of the quadruple is converted to its hexadecimal representation | |
23 | * but the regular 0..F interval is substituted with the continuous A..P | |
24 | * interval. One quadruple is represented with 8 characters, each from A-P. | |
25 | */ | |
26 | class Quad { | |
27 | ||
28 | union { | |
29 | unsigned int value; | |
30 | #if defined(__sparc__) || defined(__sparc) | |
31 | struct { | |
32 | unsigned char group; /* big endian, MSB */ | |
33 | unsigned char plane; | |
34 | unsigned char row; | |
35 | unsigned char cell; | |
36 | } comp; | |
37 | #else | |
38 | struct { | |
39 | unsigned char cell; /* little endian, LSB */ | |
40 | unsigned char row; | |
41 | unsigned char plane; | |
42 | unsigned char group; | |
43 | } comp; | |
44 | #endif | |
45 | } u; | |
46 | ||
47 | public: | |
48 | ||
49 | /** | |
50 | * Default constructor initializes the quadruple to \q{0,0,0,0} | |
51 | */ | |
52 | Quad(); | |
53 | Quad(unsigned int value); | |
54 | Quad(unsigned char group, unsigned char plane, unsigned char row, | |
55 | unsigned char cell); | |
56 | Quad(const Quad& rhs); | |
57 | ||
58 | unsigned int get_value() const; | |
59 | ||
60 | void set(unsigned char group, unsigned char plane, unsigned char row, | |
61 | unsigned char cell); | |
62 | ||
63 | /** | |
64 | * Sets the given field of the quadruple. | |
65 | * @param field 0 - group ... 3 - cell. | |
66 | * @param c Value to set. | |
67 | */ | |
68 | void set(int field, unsigned char c); | |
69 | ||
70 | const Quad operator-(const Quad& rhs) const; | |
71 | const Quad& operator=(const Quad& rhs); | |
72 | bool operator==(unsigned int i) const; | |
73 | bool operator==(const Quad& rhs) const; | |
74 | bool operator<=(const Quad& rhs) const; | |
75 | bool operator>=(const Quad& rhs) const; | |
76 | ||
77 | bool operator<(const Quad& rhs) const; | |
78 | bool operator<(const QuadInterval& rhs) const; | |
79 | ||
80 | /** | |
81 | * Getter function for easy access to fields. | |
82 | * @param i Field selector: 0 - group ... 3 - cell | |
83 | * @return Value of the field. | |
84 | */ | |
85 | unsigned char operator[](int i) const; | |
86 | ||
87 | /** | |
88 | * Returns the hex representation of this quadruple. [A-P]{8} | |
89 | * The string must be deallocated by the caller with Free() | |
90 | */ | |
91 | char* get_hexrepr() const; | |
92 | ||
93 | /** | |
94 | * Returns the hex representation of \p value. [A-P]{8} | |
95 | * The string must be deallocated by the caller with Free() | |
96 | */ | |
97 | static char* get_hexrepr(unsigned int value); | |
98 | ||
99 | /** Fills \p str with the hex representation. | |
100 | * str[0] gets the upper half of the most significant byte (group) | |
101 | * str[7] gets the lower half of the least significant byte (cell) | |
102 | */ | |
103 | static void get_hexrepr(const Quad& q, char* const str); | |
104 | ||
105 | /** | |
106 | * Returns the hex representation of one field of the quadruple. [A-P]{2} | |
107 | * The string must be deallocated by the caller with Free() | |
108 | */ | |
109 | static char* char_hexrepr(unsigned char c); | |
110 | }; | |
111 | ||
112 | /** \class QuadInterval | |
113 | * \brief Represents an interval in a regular expression set. | |
114 | * | |
115 | * The interval is given with its lower and upper boundary. Boundaries are | |
116 | * given as pointers to quadruples: \a lower and \a upper. | |
117 | * | |
118 | * To help creating more optimal regular expressions some basic operation is | |
119 | * implemented: \a contains, \a has_intersection, \a join. | |
120 | */ | |
121 | class QuadInterval { | |
122 | Quad lower; | |
123 | Quad upper; | |
124 | ||
125 | friend class Quad; | |
126 | ||
127 | public: | |
128 | QuadInterval(Quad p_lower, Quad p_upper); | |
129 | ||
130 | QuadInterval(const QuadInterval& rhs); | |
131 | ||
132 | const Quad& get_lower() const; | |
133 | const Quad& get_upper() const; | |
134 | ||
135 | bool contains(const Quad& rhs) const; | |
136 | bool contains(const QuadInterval& rhs) const; | |
137 | bool has_intersection(const QuadInterval& rhs) const; | |
138 | void join(const QuadInterval& rhs); | |
139 | ||
140 | /** | |
141 | * operator<() | |
142 | * @param rhs A quadruple. | |
143 | * @return true if value of \a rhs is larger than value of the upper boundary. | |
144 | */ | |
145 | bool operator<(const Quad& rhs) const; | |
146 | /** | |
147 | * operator<() | |
148 | * @param rhs A QuadInterval. | |
149 | * @return true if \a rhs does not intersect with \a this and lower boundary | |
150 | * of \a rhs is larger than upper boundary of \a this. | |
151 | */ | |
152 | bool operator<(const QuadInterval& rhs) const; | |
153 | ||
154 | unsigned int width() const; | |
155 | ||
156 | /** | |
157 | * Generate a posix regular expression for the interval. | |
158 | * @return pointer to the string which should be freed by the caller. | |
159 | */ | |
160 | char* generate_posix(); | |
161 | ||
162 | private: | |
163 | char* generate_hex_interval(unsigned char source, unsigned char dest); | |
164 | }; | |
165 | ||
166 | /** \class QuadSet | |
167 | * \brief Represents a set in a regular expression. | |
168 | * | |
169 | * Stores the quadruples and the intervals in the set and creates a posix | |
170 | * regular expression. | |
171 | */ | |
172 | class QuadSet { | |
173 | ||
174 | enum elemtype_t { | |
175 | QSET_QUAD, | |
176 | QSET_INTERVAL | |
177 | }; | |
178 | ||
179 | /** | |
180 | * Linked list storing the quadruples and intervals. This list is kept sorted | |
181 | * to ease the POSIX regular expression generation in case of negated sets. | |
182 | */ | |
183 | typedef struct _quadset_node_t { | |
184 | union { | |
185 | Quad* p_quad; | |
186 | QuadInterval* p_interval; | |
187 | } u; | |
188 | _quadset_node_t* next; | |
189 | elemtype_t etype; | |
190 | } quadset_node_t; | |
191 | ||
192 | quadset_node_t* set; | |
193 | bool negate; | |
194 | ||
195 | /// Copy constructor disabled | |
196 | QuadSet(const QuadSet&); | |
197 | /// Assignment disabled | |
198 | QuadSet& operator=(const QuadSet&); | |
199 | public: | |
200 | QuadSet(); | |
201 | ||
202 | bool add(Quad* p_quad); | |
203 | void add(QuadInterval* interval); | |
204 | ||
205 | void join(QuadSet* rhs); | |
206 | ||
207 | void set_negate(bool neg); | |
208 | ||
209 | bool has_quad(const Quad& q) const; | |
210 | bool is_empty() const; | |
211 | ||
212 | /** | |
213 | * Generate a posix regular expression for the set. | |
214 | * @return pointer to the string which should be freed by the caller. | |
215 | */ | |
216 | char* generate_posix(); | |
217 | ||
218 | ~QuadSet(); | |
219 | ||
220 | private: | |
221 | void clean(quadset_node_t* start); | |
222 | ||
223 | /** | |
224 | * Generate the disjoint set of \a this. | |
225 | */ | |
226 | void do_negate(); | |
227 | ||
228 | void add_negate_interval(const Quad& q1, const Quad& q2); | |
229 | void join_if_possible(quadset_node_t* start); | |
230 | }; | |
231 | ||
232 | #endif |