Sync with 5.4.3
[deliverable/titan.core.git] / core / Bitstring.hh
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
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 BITSTRING_HH
9 #define BITSTRING_HH
10
11 #include "Types.h"
12 #include "Basetype.hh"
13 #include "Template.hh"
14 #include "Optional.hh"
15 #include "RAW.hh"
16 #include "BER.hh"
17
18 class INTEGER;
19 class HEXSTRING;
20 class OCTETSTRING;
21 class CHARSTRING;
22 class BITSTRING_ELEMENT;
23
24 class Module_Param;
25
26 /** bitstring value class.
27 * Refcounted copy-on-write implementation */
28 class BITSTRING : public Base_Type {
29
30 friend class BITSTRING_ELEMENT;
31 friend class BITSTRING_template;
32
33 friend BITSTRING int2bit(const INTEGER& value, int length);
34 friend BITSTRING hex2bit(const HEXSTRING& value);
35 friend BITSTRING oct2bit(const OCTETSTRING& value);
36 friend BITSTRING str2bit(const CHARSTRING& value);
37 friend BITSTRING substr(const BITSTRING& value, int index, int returncount);
38 friend BITSTRING replace(const BITSTRING& value, int index, int len,
39 const BITSTRING& repl);
40
41 struct bitstring_struct;
42 bitstring_struct *val_ptr;
43
44 /** Allocate memory if needed.
45 * @param n_bits the number of bits needed.
46 * @pre n_bits >= 0
47 * @post val_ptr != 0
48 * If n_bits > 0, allocates n_bits/8 bytes of memory
49 * */
50 void init_struct(int n_bits);
51 /// Get the bit at the given index.
52 boolean get_bit(int bit_index) const;
53 /// Assign \p new_value to the bit at the given index
54 void set_bit(int bit_index, boolean new_value);
55 /// Copy-on-write
56 void copy_value();
57 /** Ensures that if the bitstring length is not a multiple of 8 then
58 * the unused bits in the last byte are all cleared. */
59 void clear_unused_bits() const;
60 /// Creates a BITSTRING with pre-allocated but uninitialised memory.
61 BITSTRING(int n_bits);
62
63 public:
64 BITSTRING();
65 BITSTRING(int init_n_bits, const unsigned char* init_bits);
66
67 /** Copy constructor.
68 *
69 * @pre \p other_value must be bound */
70 BITSTRING(const BITSTRING& other_value);
71
72 /** Creates a BITSTRING with a single bit.
73 * @pre \p other_value must be bound */
74 BITSTRING(const BITSTRING_ELEMENT& other_value);
75
76 ~BITSTRING();
77 /// Decrement the reference count and free the memory if it's the last reference
78 void clean_up();
79
80 BITSTRING& operator=(const BITSTRING& other_value);
81 BITSTRING& operator=(const BITSTRING_ELEMENT& other_value);
82
83 boolean operator==(const BITSTRING& other_value) const;
84 boolean operator==(const BITSTRING_ELEMENT& other_value) const;
85
86 inline boolean operator!=(const BITSTRING& other_value) const
87 { return !(*this == other_value); }
88 inline boolean operator!=(const BITSTRING_ELEMENT& other_value) const
89 { return !(*this == other_value); }
90
91 BITSTRING operator+(const BITSTRING& other_value) const;
92 BITSTRING operator+(const BITSTRING_ELEMENT& other_value) const;
93
94 BITSTRING operator~() const;
95 BITSTRING operator&(const BITSTRING& other_value) const;
96 BITSTRING operator&(const BITSTRING_ELEMENT& other_value) const;
97 BITSTRING operator|(const BITSTRING& other_value) const;
98 BITSTRING operator|(const BITSTRING_ELEMENT& other_value) const;
99 BITSTRING operator^(const BITSTRING& other_value) const;
100 BITSTRING operator^(const BITSTRING_ELEMENT& other_value) const;
101
102 BITSTRING operator<<(int shift_count) const;
103 BITSTRING operator<<(const INTEGER& shift_count) const;
104 BITSTRING operator>>(int shift_count) const;
105 BITSTRING operator>>(const INTEGER& shift_count) const;
106 BITSTRING operator<<=(int rotate_count) const;
107 BITSTRING operator<<=(const INTEGER& rotate_count) const;
108 BITSTRING operator>>=(int rotate_count) const;
109 BITSTRING operator>>=(const INTEGER& rotate_count) const;
110
111 BITSTRING_ELEMENT operator[](int index_value);
112 BITSTRING_ELEMENT operator[](const INTEGER& index_value);
113 const BITSTRING_ELEMENT operator[](int index_value) const;
114 const BITSTRING_ELEMENT operator[](const INTEGER& index_value) const;
115
116 inline boolean is_bound() const { return val_ptr != NULL; }
117 inline boolean is_value() const { return val_ptr != NULL; }
118 inline void must_bound(const char *err_msg) const
119 { if (val_ptr == NULL) TTCN_error("%s", err_msg); }
120
121 int lengthof() const;
122
123 operator const unsigned char*() const;
124
125 void log() const;
126
127 #ifdef TITAN_RUNTIME_2
128 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const BITSTRING*>(other_value)); }
129 void set_value(const Base_Type* other_value) { *this = *(static_cast<const BITSTRING*>(other_value)); }
130 Base_Type* clone() const { return new BITSTRING(*this); }
131 const TTCN_Typedescriptor_t* get_descriptor() const { return &BITSTRING_descr_; }
132 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
133 #else
134 inline boolean is_present() const { return is_bound(); }
135 #endif
136
137 void set_param(Module_Param& param);
138 Module_Param* get_param(Module_Param_Name& param_name) const;
139
140 void encode_text(Text_Buf& text_buf) const;
141 void decode_text(Text_Buf& text_buf);
142
143 private:
144 void BER_encode_putbits(unsigned char *target,
145 unsigned int bitnum_start,
146 unsigned int bit_count) const;
147
148 void BER_decode_getbits(const unsigned char *source,
149 size_t s_len, unsigned int& bitnum_start);
150
151 void BER_decode_TLV_(const ASN_BER_TLV_t& p_tlv, unsigned L_form,
152 unsigned int& bitnum_start);
153 public:
154 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
155 TTCN_EncDec::coding_t p_coding, ...) const;
156
157 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
158 TTCN_EncDec::coding_t p_coding, ...);
159
160 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
161 unsigned p_coding) const;
162
163 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
164 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
165
166 /** Encodes the value of the variable according to the
167 * TTCN_Typedescriptor_t. It must be public because called by
168 * another types during encoding. */
169 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
170 /** Decodes the value of the variable according to the
171 * TTCN_Typedescriptor_t. It must be public because called by
172 * another types during encoding. Returns the number of decoded
173 * bits. */
174 int RAW_decode(const TTCN_Typedescriptor_t& , TTCN_Buffer&, int, raw_order_t,
175 boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
176
177 int XER_encode(const XERdescriptor_t&, TTCN_Buffer&, unsigned int, int, embed_values_enc_struct_t*) const;
178 int XER_decode(const XERdescriptor_t&, XmlReaderWrap& reader, unsigned int, unsigned int, embed_values_dec_struct_t*);
179
180 /** Encodes accordingly to the JSON encoding rules.
181 * Returns the length of the encoded data. */
182 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
183
184 /** Decodes accordingly to the JSON decoding rules.
185 * Returns the length of the encoded data. */
186 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
187 };
188
189 class BITSTRING_ELEMENT {
190 boolean bound_flag;
191 BITSTRING& str_val;
192 int bit_pos;
193
194 public:
195 BITSTRING_ELEMENT(boolean par_bound_flag, BITSTRING& par_str_val,
196 int par_bit_pos);
197
198 BITSTRING_ELEMENT& operator=(const BITSTRING& other_value);
199 BITSTRING_ELEMENT& operator=(const BITSTRING_ELEMENT& other_value);
200
201 boolean operator==(const BITSTRING& other_value) const;
202 boolean operator==(const BITSTRING_ELEMENT& other_value) const;
203
204 boolean operator!=(const BITSTRING& other_value) const
205 { return !(*this == other_value); }
206 boolean operator!=(const BITSTRING_ELEMENT& other_value) const
207 { return !(*this == other_value); }
208
209 BITSTRING operator+(const BITSTRING& other_value) const;
210 BITSTRING operator+(const BITSTRING_ELEMENT& other_value) const;
211
212 BITSTRING operator~() const;
213 BITSTRING operator&(const BITSTRING& other_value) const;
214 BITSTRING operator&(const BITSTRING_ELEMENT& other_value) const;
215 BITSTRING operator|(const BITSTRING& other_value) const;
216 BITSTRING operator|(const BITSTRING_ELEMENT& other_value) const;
217 BITSTRING operator^(const BITSTRING& other_value) const;
218 BITSTRING operator^(const BITSTRING_ELEMENT& other_value) const;
219
220 inline boolean is_bound() const { return bound_flag; }
221 inline boolean is_present() const { return bound_flag; }
222 inline boolean is_value() const { return bound_flag; }
223 inline void must_bound(const char *err_msg) const
224 { if (!bound_flag) TTCN_error("%s", err_msg); }
225
226 boolean get_bit() const;
227
228 void log() const;
229 };
230
231 /// bitstring template class
232 class BITSTRING_template : public Restricted_Length_Template {
233 #ifdef __SUNPRO_CC
234 public:
235 #endif
236 struct bitstring_pattern_struct;
237 private:
238 BITSTRING single_value;
239 union {
240 struct {
241 unsigned int n_values;
242 BITSTRING_template *list_value;
243 } value_list;
244 bitstring_pattern_struct *pattern_value;
245 };
246
247 void copy_template(const BITSTRING_template& other_value);
248 static boolean match_pattern(const bitstring_pattern_struct *string_pattern,
249 const BITSTRING::bitstring_struct *string_value);
250
251 public:
252 BITSTRING_template();
253 BITSTRING_template(template_sel other_value);
254 BITSTRING_template(const BITSTRING& other_value);
255 BITSTRING_template(const BITSTRING_ELEMENT& other_value);
256 BITSTRING_template(const OPTIONAL<BITSTRING>& other_value);
257 BITSTRING_template(unsigned int n_elements,
258 const unsigned char *pattern_elements);
259 BITSTRING_template(const BITSTRING_template& other_value);
260 ~BITSTRING_template();
261 void clean_up();
262
263 BITSTRING_template& operator=(template_sel other_value);
264 BITSTRING_template& operator=(const BITSTRING& other_value);
265 BITSTRING_template& operator=(const BITSTRING_ELEMENT& other_value);
266 BITSTRING_template& operator=(const OPTIONAL<BITSTRING>& other_value);
267 BITSTRING_template& operator=(const BITSTRING_template& other_value);
268
269 BITSTRING_ELEMENT operator[](int index_value);
270 BITSTRING_ELEMENT operator[](const INTEGER& index_value);
271 const BITSTRING_ELEMENT operator[](int index_value) const;
272 const BITSTRING_ELEMENT operator[](const INTEGER& index_value) const;
273
274 boolean match(const BITSTRING& other_value, boolean legacy = FALSE) const;
275 const BITSTRING& valueof() const;
276
277 int lengthof() const;
278
279 void set_type(template_sel template_type, unsigned int list_length);
280 BITSTRING_template& list_item(unsigned int list_index);
281
282 void log() const;
283 void log_match(const BITSTRING& match_value, boolean legacy = FALSE) const;
284
285 void set_param(Module_Param& param);
286 Module_Param* get_param(Module_Param_Name& param_name) const;
287
288 void encode_text(Text_Buf& text_buf) const;
289 void decode_text(Text_Buf& text_buf);
290
291 boolean is_present(boolean legacy = FALSE) const;
292 boolean match_omit(boolean legacy = FALSE) const;
293 #ifdef TITAN_RUNTIME_2
294 void valueofv(Base_Type* value) const { *(static_cast<BITSTRING*>(value)) = valueof(); }
295 void set_value(template_sel other_value) { *this = other_value; }
296 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const BITSTRING*>(other_value)); }
297 Base_Template* clone() const { return new BITSTRING_template(*this); }
298 const TTCN_Typedescriptor_t* get_descriptor() const { return &BITSTRING_descr_; }
299 boolean matchv(const Base_Type* other_value, boolean legacy) const { return match(*(static_cast<const BITSTRING*>(other_value)), legacy); }
300 void log_matchv(const Base_Type* match_value, boolean legacy) const { log_match(*(static_cast<const BITSTRING*>(match_value)), legacy); }
301 #else
302 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
303 #endif
304 };
305
306 #endif
This page took 0.037174 seconds and 5 git commands to generate.