File: Quadruple.hh

package info (click to toggle)
eclipse-titan 6.5.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 101,128 kB
  • sloc: cpp: 259,139; ansic: 47,560; yacc: 22,554; makefile: 14,074; sh: 12,630; lex: 9,101; xml: 5,362; java: 4,849; perl: 3,784; awk: 48; php: 32; python: 13
file content (241 lines) | stat: -rw-r--r-- 6,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/******************************************************************************
 * Copyright (c) 2000-2018 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Raduly, Csaba
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
#ifndef _Ttcn_Quadruple_HH
#define _Ttcn_Quadruple_HH

class QuadInterval;

/** \class Quad
 *  \brief Represents a quadruple.
 *
 *  The stored value can be accessed as an integer or as fields of the
 *  quadruple. The aim is to help the generation of the posix regular expression
 *  from TTCN-3 pattern of type universal charstring. This class can create the
 *  string representation needed by pattern matching functions.
 *
 *  <b>The string representation:</b>
 *  Every field of the quadruple is converted to its hexadecimal representation
 *  but the regular 0..F interval is substituted with the continuous A..P
 *  interval. One quadruple is represented with 8 characters, each from A-P.
 */
class Quad {

  union {
    unsigned int value;
#if defined(__sparc__) || defined(__sparc)
    struct {
      unsigned char group; /* big endian, MSB */
      unsigned char plane;
      unsigned char row;
      unsigned char cell;
    } comp;
#else
    struct {
      unsigned char cell; /* little endian, LSB */
      unsigned char row;
      unsigned char plane;
      unsigned char group;
    } comp;
#endif
  } u;

public:

  /**
   *  Default constructor initializes the quadruple to \q{0,0,0,0}
   */
  Quad();
  Quad(unsigned int value);
  Quad(unsigned char group, unsigned char plane, unsigned char row,
    unsigned char cell);
  Quad(const Quad& rhs);

  unsigned int get_value() const;

  void set(unsigned char group, unsigned char plane, unsigned char row,
    unsigned char cell);

  /**
   * Sets the given field of the quadruple.
   * @param field 0 - group ... 3 - cell.
   * @param c Value to set.
   */
  void set(int field, unsigned char c);
  
  void set_hexrepr(const char* hex_repr);

  const Quad operator-(const Quad& rhs) const;
  const Quad& operator=(const Quad& rhs);
  bool operator==(unsigned int i) const;
  bool operator==(const Quad& rhs) const;
  bool operator<=(const Quad& rhs) const;
  bool operator>=(const Quad& rhs) const;

  bool operator<(const Quad& rhs) const;
  bool operator<(const QuadInterval& rhs) const;

  /**
   * Getter function for easy access to fields.
   * @param i Field selector: 0 - group ... 3 - cell
   * @return Value of the field.
   */
  unsigned char operator[](int i) const;

  /**
   * Returns the hex representation of this quadruple. [A-P]{8}
   * The string must be deallocated by the caller with Free()
   */
  char* get_hexrepr() const;

  /**
   * Returns the hex representation of \p value. [A-P]{8}
   * The string must be deallocated by the caller with Free()
   */
  static char* get_hexrepr(unsigned int value);

  /** Fills \p str with the hex representation.
   *  str[0] gets the upper half of the most significant byte (group)
   *  str[7] gets the lower half of the least significant byte (cell)
   */
  static void  get_hexrepr(const Quad& q, char* const str);

  /**
   * Returns the hex representation of one field of the quadruple. [A-P]{2}
   * The string must be deallocated by the caller with Free()
   */
  static char* char_hexrepr(unsigned char c);
};

/** \class QuadInterval
 *  \brief Represents an interval in a regular expression set.
 *
 *  The interval is given with its lower and upper boundary. Boundaries are
 *  given as pointers to quadruples: \a lower and \a upper.
 *
 *  To help creating more optimal regular expressions some basic operation is
 *  implemented: \a contains, \a has_intersection, \a join.
 */
class QuadInterval {
  Quad lower;
  Quad upper;

  friend class Quad;

public:
  QuadInterval(Quad p_lower, Quad p_upper);

  QuadInterval(const QuadInterval& rhs);

  const Quad& get_lower() const;
  const Quad& get_upper() const;

  bool contains(const Quad& rhs) const;
  bool contains(const QuadInterval& rhs) const;
  bool has_intersection(const QuadInterval& rhs) const;
  void join(const QuadInterval& rhs);

  /**
   * operator<()
   * @param rhs A quadruple.
   * @return true if value of \a rhs is larger than value of the upper boundary.
   */
  bool operator<(const Quad& rhs) const;
  /**
   * operator<()
   * @param rhs A QuadInterval.
   * @return true if \a rhs does not intersect with \a this and lower boundary
   * of \a rhs is larger than upper boundary of \a this.
   */
  bool operator<(const QuadInterval& rhs) const;

  unsigned int width() const;

  /**
   * Generate a posix regular expression for the interval.
   * @return pointer to the string which should be freed by the caller.
   */
  char* generate_posix();

private:
  char* generate_hex_interval(unsigned char source, unsigned char dest);
};

/** \class QuadSet
 *  \brief Represents a set in a regular expression.
 *
 *  Stores the quadruples and the intervals in the set and creates a posix
 *  regular expression.
 */
class QuadSet {

  enum elemtype_t {
    QSET_QUAD,
    QSET_INTERVAL
  };

  /**
   * Linked list storing the quadruples and intervals. This list is kept sorted
   * to ease the POSIX regular expression generation in case of negated sets.
   */
  typedef struct _quadset_node_t {
    union {
      Quad* p_quad;
      QuadInterval* p_interval;
    } u;
    _quadset_node_t* next;
    elemtype_t etype;
  } quadset_node_t;

  quadset_node_t* set;
  bool negate;

  /// Copy constructor disabled
  QuadSet(const QuadSet&);
  /// Assignment disabled
  QuadSet& operator=(const QuadSet&);
public:
  QuadSet();

  bool add(Quad* p_quad);
  void add(QuadInterval* interval);

  void join(QuadSet* rhs);

  void set_negate(bool neg);

  bool has_quad(const Quad& q) const;
  bool is_empty() const;

  /**
   * Generate a posix regular expression for the set.
   * @return pointer to the string which should be freed by the caller.
   */
  char* generate_posix();

  ~QuadSet();

private:
  void clean(quadset_node_t* start);

  /**
   * Generate the disjoint set of \a this.
   */
  void do_negate();

  void add_negate_interval(const Quad& q1, const Quad& q2);
  void join_if_possible(quadset_node_t* start);
};

#endif