fca5d1c0e6c47c859db089e93017a3f47a02c5d8
[deliverable/titan.core.git] / core / Basetype.hh
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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 BASETYPE_HH
9 #define BASETYPE_HH
10
11 #include "Types.h"
12 #include "Encdec.hh"
13 #include "RInt.hh"
14 #include "JSON_Tokenizer.hh"
15 #include "Vector.hh"
16 #ifdef TITAN_RUNTIME_2
17 #include "Struct_of.hh"
18 #endif
19
20 struct ASN_BERdescriptor_t;
21 struct ASN_BER_TLV_t;
22 struct TTCN_RAWdescriptor_t;
23 struct TTCN_TEXTdescriptor_t;
24 struct XERdescriptor_t;
25 struct TTCN_JSONdescriptor_t;
26 class XmlReaderWrap;
27 class Module_Param;
28
29 /** @brief Type descriptor
30 *
31 * There is one type descriptor object for each type.
32 * Descriptors for built-in types are supplied by the runtime.
33 * Descriptors for user-defined types are written by the compiler
34 * in the generated code.
35 */
36 struct TTCN_Typedescriptor_t {
37 const char * const name; /**< Name of the type, e.g INTEGER, REAL, verdicttype, etc */
38 const ASN_BERdescriptor_t * const ber; /**< Information for BER coding */
39 const TTCN_RAWdescriptor_t * const raw; /**< Information for RAW coding */
40 const TTCN_TEXTdescriptor_t * const text; /**< Information for TEXT coding */
41 const XERdescriptor_t * const xer; /**< Information for XER */
42 const TTCN_JSONdescriptor_t * const json; /**< Information for JSON coding */
43 /** ASN subtype
44 *
45 * Used by classes implementing more than one ASN.1 type
46 * (OBJID and UNIVERSAL_CHARSTRING).
47 */
48 enum {
49 DONTCARE,
50 UNIVERSALSTRING, BMPSTRING, UTF8STRING,
51 TELETEXSTRING, VIDEOTEXSTRING, GRAPHICSTRING, GENERALSTRING,
52 OBJID, ROID
53 } const asnbasetype;
54 };
55
56 #ifdef TITAN_RUNTIME_2
57
58 struct Erroneous_value_t {
59 const bool raw;
60 const Base_Type * const errval; // NULL if `omit'
61 const TTCN_Typedescriptor_t* type_descr; // NULL if `omit' or raw
62 };
63
64 struct Erroneous_values_t {
65 const int field_index;
66 const char* field_qualifier;
67 const Erroneous_value_t * const before;
68 const Erroneous_value_t * const value;
69 const Erroneous_value_t * const after;
70 };
71
72 struct Erroneous_descriptor_t {
73 const int field_index;
74 const int omit_before; // -1 if none
75 const char* omit_before_qualifier; // NULL if none
76 const int omit_after; // -1 if none
77 const char* omit_after_qualifier; // NULL if none
78 const int values_size;
79 const Erroneous_values_t * const values_vec;
80 const int embedded_size;
81 const Erroneous_descriptor_t * const embedded_vec;
82 /** search in values_vec for the field with index field_idx */
83 const Erroneous_values_t* get_field_err_values(int field_idx) const;
84 /** search in embedded_vec for the field with index field_idx */
85 const Erroneous_descriptor_t* get_field_emb_descr(int field_idx) const;
86 /** if the current element of values_vec has index field_idx then returns it and increments values_idx */
87 const Erroneous_values_t* next_field_err_values(const int field_idx, int& values_idx) const;
88 /** if the current element of embedded_vec has index field_idx then returns it and increments edescr_idx */
89 const Erroneous_descriptor_t* next_field_emb_descr(const int field_idx, int& edescr_idx) const;
90 void log() const;
91 void log_() const;
92 };
93
94 #endif
95
96 class TTCN_Type_list;
97 class RAW_enc_tree;
98 class Limit_Token_List;
99 #ifdef TITAN_RUNTIME_2
100 class Text_Buf;
101 #endif
102
103 #ifdef TITAN_RUNTIME_2
104 #define VIRTUAL_IF_RUNTIME_2 virtual
105 #else
106 #define VIRTUAL_IF_RUNTIME_2
107 #endif
108
109 /**
110 * The base class for all types in TTCN-3 runtime environment.
111 *
112 * Uses the compiler-generated default constructor, copy constructor
113 * and assignment operator (they do nothing because the class is empty).
114 */
115 class Base_Type {
116 public:
117
118 /** Whether the value is present.
119 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility!
120 * causes DTE, must be used only if the field is OPTIONAL<>
121 */
122 VIRTUAL_IF_RUNTIME_2 boolean ispresent() const;
123
124 VIRTUAL_IF_RUNTIME_2 void log() const;
125
126 /** Check whether the XML encoding of the type can begin with an XML element
127 * having the specified name and namespace.
128 *
129 * This function checks for the "starter tag" of the type. This usually
130 * means the tag should match the name in the XER descriptor,
131 * with a few notable exceptions:
132 *
133 * - If the type is untagged, then
134 * - if it's a record, all fields up to the the first non-optional field
135 * are checked;
136 * - if it's a record-of, the element is checked.
137 * - A \c universal \c charstring with ANY-ELEMENT can begin with any tag.
138 *
139 * @param name the XML element name (local name)
140 * @param uri the namespace URI of the element
141 * @param xd XER descriptor
142 * @param flavor one or more bits from XER_flavor. Only XER_EXTENDED counts
143 * @return TRUE if the XML element can begin the XER encoding of the type,
144 * FALSE otherwise.
145 */
146 static boolean can_start(const char *name, const char *uri,
147 XERdescriptor_t const& xd, unsigned int flavor);
148
149 #ifdef TITAN_RUNTIME_2
150 virtual void set_param(Module_Param& param) = 0;
151 /** Whether the type is a sequence-of.
152 * @return \c FALSE */
153 virtual boolean is_seof() const { return FALSE; }
154
155 virtual void encode_text(Text_Buf& text_buf) const = 0;
156 virtual void decode_text(Text_Buf& text_buf) = 0;
157
158 virtual boolean is_bound() const = 0;
159 virtual boolean is_value() const { return is_bound(); }
160 virtual void clean_up() = 0;
161
162 /** returns if the value of this is equal to the value of the parameter. */
163 virtual boolean is_equal(const Base_Type* other_value) const = 0;
164
165 /** Performs: *this = *other_value.
166 * Does not take ownership of \p other_value. */
167 virtual void set_value(const Base_Type* other_value) = 0;
168
169 /** creates a copy of this object, returns a pointer to it. */
170 virtual Base_Type* clone() const = 0;
171
172 /** return reference to the type descriptor object for this type. */
173 virtual const TTCN_Typedescriptor_t* get_descriptor() const = 0;
174
175 /** @name override in class OPTIONAL<T>
176 * @{ */
177 virtual boolean is_optional() const { return FALSE; }
178
179 /** Is the optional value present or omit.
180 * @return \c true if present, \c false otherwise
181 * used for TTCN-3 ispresent()
182 **/
183 virtual boolean is_present() const;
184
185 /** Access the embedded type of the optional.
186 *
187 * The implementation in Base_Type always causes a dynamic testcase error.
188 */
189 virtual Base_Type* get_opt_value();
190
191 /** Access the embedded type of the optional.
192 *
193 * The implementation in Base_Type always causes a dynamic testcase error.
194 */
195 virtual const Base_Type* get_opt_value() const;
196
197 /** Set the optional field to \c omit.
198 * The implementation in Base_Type always causes a dynamic testcase error.
199 */
200 virtual void set_to_omit();
201
202 /** Set the optional field to present.
203 * The implementation in Base_Type always causes a dynamic testcase error.
204 */
205 virtual void set_to_present();
206 /** @} */
207
208 virtual ~Base_Type() { }
209
210 #endif
211
212 /** Do nothing for non-structured types. */
213 VIRTUAL_IF_RUNTIME_2 void set_implicit_omit() { }
214
215 /** @brief Encode an instance of this object
216
217 Description:
218
219 @param p_td type descriptor
220 @param p_buf buffer
221 @param p_coding coding type to select BER, RAW or TEXT coding
222
223 */
224 VIRTUAL_IF_RUNTIME_2 void encode(const TTCN_Typedescriptor_t& p_td,
225 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...) const;
226
227 /** @brief Decode an instance of this object
228
229 Description:
230
231 @param p_td type descriptor
232 @param p_buf buffer
233 @param p_coding coding type to select BER, RAW or TEXT coding
234
235 */
236 VIRTUAL_IF_RUNTIME_2 void decode(const TTCN_Typedescriptor_t& p_td,
237 TTCN_Buffer& p_buf, TTCN_EncDec::coding_t p_coding, ...);
238
239 protected:
240 /** Check type descriptor for BER encoding
241 *
242 * @param p_td a TTCN type descriptor
243 *
244 * Calls TTCN_EncDec_ErrorContext::error_internal if
245 * p_td has no BER descriptor.
246 */
247 static void BER_chk_descr(const TTCN_Typedescriptor_t& p_td);
248
249 /** Check the BER coding
250 *
251 * If \p p_coding is either BER_ENCODE_CER or BER_ENCODE_DER,
252 * this function does nothing. Otherwise, it issues a warning
253 * and sets \p p_coding to BER_ENCODE_DER.
254 *
255 * @param[in, out] p_coding BER coding
256 * @post p_coding is either BER_ENCODE_CER or BER_ENCODE_DER
257 */
258 static void BER_encode_chk_coding(unsigned& p_coding);
259
260 /** Check type descriptor for XER encoding and the XER coding variant
261 *
262 * @param[in, out] p_coding XER coding
263 * @param[in] p_td TTCN type descriptor
264 *
265 * If \p p_td has no XER descriptor, calls
266 * TTCN_EncDec_ErrorContext::error_internal()
267 *
268 * If \p p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED,
269 * this function does nothing. Otherwise, it issues a warning
270 * and sets \p p_coding to XER_BASIC.
271 *
272 * @post p_coding is one of XER_BASIC, XER_CANONICAL or XER_EXTENDED
273 */
274 static void XER_encode_chk_coding(unsigned& p_coding,
275 const TTCN_Typedescriptor_t& p_td);
276
277 /** Check bound and create a 0-length TLV in case unbound is ignored.
278 *
279 * @param[in] p_isbound
280 * @return a newly constructed and empty \c ASN_BER_TLV_t
281 * if p_isbound is false, or NULL if p_isbound is true
282 * @note if p_isbound is false, this function calls
283 * TTCN_EncDec_ErrorContext::error with TTCN_EncDec::ET_UNBOUND.
284 * If that error is not ignored, this function does not return at all.
285 */
286 static ASN_BER_TLV_t* BER_encode_chk_bound(boolean p_isbound);
287
288 private:
289 static void BER_encode_putoctets_OCTETSTRING
290 (unsigned char *target,
291 unsigned int octetnum_start, unsigned int octet_count,
292 int p_nof_octets, const unsigned char *p_octets_ptr);
293
294 protected:
295 static ASN_BER_TLV_t* BER_encode_TLV_OCTETSTRING
296 (unsigned p_coding,
297 int p_nof_octets, const unsigned char *p_octets_ptr);
298
299 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
300 const int_val_t& p_int_val);
301 static ASN_BER_TLV_t *BER_encode_TLV_INTEGER(unsigned p_coding,
302 const int& p_int_val);
303
304 static void BER_encode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
305 boolean p_isvalid, int p_value);
306
307 static void BER_decode_str2TLV(TTCN_Buffer& p_buf, ASN_BER_TLV_t& p_tlv,
308 unsigned L_form);
309
310 static boolean BER_decode_constdTLV_next(const ASN_BER_TLV_t& p_tlv,
311 size_t& V_pos,
312 unsigned L_form,
313 ASN_BER_TLV_t& p_target_tlv);
314
315 static void BER_decode_constdTLV_end(const ASN_BER_TLV_t& p_tlv,
316 size_t& V_pos,
317 unsigned L_form,
318 ASN_BER_TLV_t& p_target_tlv,
319 boolean tlv_present);
320
321 static void BER_decode_strip_tags(const ASN_BERdescriptor_t& p_ber,
322 const ASN_BER_TLV_t& p_tlv,
323 unsigned L_form,
324 ASN_BER_TLV_t& stripped_tlv);
325
326 private:
327 static void BER_decode_getoctets_OCTETSTRING
328 (const unsigned char *source, size_t s_len,
329 unsigned int& octetnum_start,
330 int& p_nof_octets, unsigned char *p_octets_ptr);
331
332 protected:
333 static void BER_decode_TLV_OCTETSTRING
334 (const ASN_BER_TLV_t& p_tlv, unsigned L_form,
335 unsigned int& octetnum_start,
336 int& p_nof_octets, unsigned char *p_octets_ptr);
337
338 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
339 unsigned L_form, int_val_t& p_int_val);
340 static boolean BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
341 unsigned L_form, int& p_int_val);
342
343 static boolean BER_decode_TLV_CHOICE(const ASN_BERdescriptor_t& p_ber,
344 const ASN_BER_TLV_t& p_tlv,
345 unsigned L_form,
346 ASN_BER_TLV_t& p_target_tlv);
347
348 static boolean BER_decode_CHOICE_selection(boolean select_result,
349 const ASN_BER_TLV_t& p_tlv);
350
351 static void BER_decode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
352 boolean p_isvalid, int p_value);
353
354 public:
355 VIRTUAL_IF_RUNTIME_2 ASN_BER_TLV_t* BER_encode_TLV(
356 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
357 #ifdef TITAN_RUNTIME_2
358 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
359 const TTCN_Typedescriptor_t& /*p_td*/, unsigned /*p_coding*/) const;
360 virtual ASN_BER_TLV_t* BER_encode_negtest_raw() const;
361 virtual int encode_raw(TTCN_Buffer& p_buf) const;
362 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
363 #endif
364
365 /** Examines whether this message corresponds the tags in the
366 * descriptor. If the BER descriptor contains no tags, then returns
367 * TRUE. Otherwise, examines the first (outermost) tag only. */
368 #ifdef TITAN_RUNTIME_2
369 virtual
370 #else
371 static
372 #endif
373 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
374 const ASN_BER_TLV_t& p_tlv);
375
376 /** Returns TRUE on success, FALSE otherwise. */
377 VIRTUAL_IF_RUNTIME_2 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
378 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
379
380 VIRTUAL_IF_RUNTIME_2 void BER_decode_opentypes(
381 TTCN_Type_list& /*p_typelist*/, unsigned /*L_form*/) {}
382
383 /** Encode with RAW coding.
384 * @return the length of the encoding
385 * @param p_td type descriptor
386 * @param myleaf filled with RAW encoding data
387 * @note Basetype::RAW_encode throws an error. */
388 VIRTUAL_IF_RUNTIME_2 int RAW_encode(const TTCN_Typedescriptor_t& p_td,
389 RAW_enc_tree& myleaf) const;
390 #ifdef TITAN_RUNTIME_2
391 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr,
392 const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
393 #endif
394
395 /** Decode with RAW coding
396 *
397 * @param p_td type descriptor
398 * @param p_buf buffer with data to be decoded
399 * @param limit number of bits the decoder is allowed to use. At the top level
400 * this is 8x the number of bytes in the buffer.
401 * @param top_bit_ord (LSB/MSB) from TTCN_RAWdescriptor_t::top_bit_order
402 * @param no_err set to TRUE if the decoder is to return errors silently,
403 * without calling TTCN_EncDec_ErrorContext::error
404 * @param sel_field selected field indicator for CROSSTAG, or -1
405 * @param first_call default TRUE. May be FALSE for a REPEATABLE record-of
406 * inside a set, if an element has been successfully decoded.
407 * @return length of decoded field, or a negative number for error
408 * @note Basetype::RAW_decode throws an error. */
409 VIRTUAL_IF_RUNTIME_2 int RAW_decode(
410 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, int limit,
411 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1,
412 boolean first_call=TRUE);
413
414
415 /** Encode with TEXT encoding.
416 * @return the length of the encoding
417 * @param p_td type descriptor
418 * @param p_buf buffer for the encoded data
419 * @note Basetype::TEXT_encode throws an error. */
420 VIRTUAL_IF_RUNTIME_2 int TEXT_encode(const TTCN_Typedescriptor_t& p_td,
421 TTCN_Buffer& p_buf) const;
422
423 #ifdef TITAN_RUNTIME_2
424 /** Encode with TEXT encoding negative test.
425 * @return the length of the encoding
426 * @param p_err_descr type descriptor
427 * @param p_td type descriptor
428 * @param p_buf buffer for the encoded data
429 * @note Basetype::TEXT_encode_negtest throws an error. */
430 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td,
431 TTCN_Buffer& p_buf) const;
432 #endif
433
434 /** Decode TEXT.
435 * @return decoded length
436 * @note Basetype::TEXT_decode throws an error. */
437 VIRTUAL_IF_RUNTIME_2 int TEXT_decode(
438 const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
439 boolean no_err=FALSE, boolean first_call=TRUE);
440
441 /** Write the XER encoding of the current object into the buffer.
442 *
443 * @param p_td type descriptor
444 * @param p_buf buffer
445 * @param flavor one of XER_flavor values
446 * @param indent indentation level
447 * @return number of bytes written into the buffer
448 */
449 VIRTUAL_IF_RUNTIME_2 int XER_encode(const XERdescriptor_t& p_td,
450 TTCN_Buffer& p_buf, unsigned int flavor, int indent) const;
451
452 #ifdef TITAN_RUNTIME_2
453 virtual int XER_encode_negtest(const Erroneous_descriptor_t* /*p_err_descr*/,
454 const XERdescriptor_t& /*p_td*/, TTCN_Buffer& /*p_buf*/,
455 unsigned int /*flavor*/, int /*indent*/) const;
456 #endif
457
458 /** Decode the current object from the supplied buffer.
459 *
460 * The XML pull parser presents a forward iterator over the XML elements.
461 * @pre Upon entering this function, the current node should be the one corresponding
462 * to this object, or possibly a text or whitespace node before it.
463 * Code should begin like this:
464 * @code
465 * int Foo::XER_decode() {
466 * int success=1, type;
467 * for ( ; success==1; success=reader.Read() ) {
468 * int type=reader.NodeType();
469 * if (XML_READER_TYPE_ELEMENT==type) {
470 * verify_name(
471 * }
472 * }
473 * }
474 * @endcode
475 *
476 * @post Upon leaving the function, the current node should be \b past
477 * the end tag, i.e. the next start tag (or any whitespace before it).
478 *
479 * It is important not to advance the parser too far, to avoid "stealing"
480 * the content of other fields (the parser can only go forward).
481 *
482 * @param p_td type descriptor
483 * @param reader Wrapper around the XML processor
484 * @param flavor one of XER_flavor values
485 * @return number of bytes "consumed"
486 */
487 VIRTUAL_IF_RUNTIME_2 int XER_decode(const XERdescriptor_t& p_td,
488 XmlReaderWrap& reader, unsigned int flavor);
489
490 /** Return an array of namespace declarations.
491 *
492 * @param[in] p_td XER descriptor of the type.
493 * @param[out] num set to the number of strings returned.
494 * @param[out] def_ns set to @p true if a default namespace was encountered
495 * @return an array of strings or NULL. All the strings in the array
496 * and the array itself must be deallocated by the caller (if num>0).
497 *
498 * The strings are in the following format:
499 * @code " xmlns:prefix='uri'" @endcode
500 * (the space at start allows direct concatenation of the strings)
501 */
502 VIRTUAL_IF_RUNTIME_2 char ** collect_ns(const XERdescriptor_t& p_td,
503 size_t& num, bool& def_ns) const;
504
505 /** Copy strings from @p new_namespaces to @p collected_ns,
506 * discarding duplicates.
507 *
508 * May reallocate @p collected_ns, in which case @p num_collected
509 * is adjusted accordingly.
510 *
511 * @param collected_ns target array
512 * @param num_collected number of already collected namespaces
513 * @param new_namespaces new array
514 * @param num_new number of new namespaces
515 *
516 * @post new_namespaces has been deallocated and set to NULL.
517 */
518 static void merge_ns(char **&collected_ns, size_t& num_collected,
519 char **new_namespaces, size_t num_new);
520
521 typedef char** (Base_Type::*collector_fn)
522 (const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
523
524 /** Start writing the XML representation
525 *
526 * @param[in] p_td XER descriptor
527 * @param[out] p_buf buffer to write into
528 * @param[in,out] flavor XER variant (basic, canonical or extended),
529 * also used to pass various flags between begin_xml and its caller
530 * @param[in] indent indentation level
531 * @param[in] empty true if an empty-element tag is needed
532 * @param[in] collector namespace collector function
533 * @param[in] type_atr type identification attribute for useNil/useUnion,
534 * in the following format: "xsi:type='foo'"
535 * @return "omit_tag": zero if the type's own tag was written (not omitted
536 * e.g. due to untagged).
537 * If the XML tag was omitted, the return value is nonzero.
538 * The special value -1 is returned if the XML in the buffer
539 * was shortened by exactly one character.
540 */
541 VIRTUAL_IF_RUNTIME_2 int begin_xml(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
542 unsigned int& flavor, int indent, bool empty,
543 collector_fn collector = &Base_Type::collect_ns, const char *type_atr = NULL) const;
544 /** Finish the XML representation.
545 *
546 * @param[in] p_td XER descriptor
547 * @param[out] p_buf buffer to write into
548 * @param[in] flavor XER variant (basic, canonical or extended),
549 * also used to pass various flags
550 * @param[in] indent indentation level
551 * @param[in] empty true if an empty-element tag is needed
552 */
553 VIRTUAL_IF_RUNTIME_2 void end_xml (const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
554 unsigned int flavor, int indent, bool empty) const;
555
556 /** Encode JSON.
557 * @return encoded length
558 * @note Basetype::JSON_encode throws an error. */
559 VIRTUAL_IF_RUNTIME_2 int JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&) const;
560
561 /** Decode JSON.
562 * @return decoded length
563 * @note Basetype::JSON_decode throws an error. */
564 VIRTUAL_IF_RUNTIME_2 int JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean);
565
566 /** not a component by default (components will return true) */
567 inline boolean is_component() { return FALSE; }
568 };
569
570 /**
571 * This class is used by BER decoding to handle
572 * ComponentRelationConstraint.
573 */
574 class TTCN_Type_list {
575 private:
576 size_t n_types;
577 const Base_Type **types;
578 /// Copy constructor disabled
579 TTCN_Type_list(const TTCN_Type_list&);
580 /// Assignment disabled
581 TTCN_Type_list& operator=(const TTCN_Type_list&);
582 public:
583 TTCN_Type_list() : n_types(0), types(NULL) {}
584 ~TTCN_Type_list();
585 void push(const Base_Type *p_type);
586 const Base_Type* pop();
587 /** If \a pos is 0 then returns the outermost, otherwise the "pos"th
588 * inner parent. */
589 const Base_Type* get_nth(size_t pos) const;
590 };
591
592 /** @name Type descriptor objects
593 @{
594 */
595 extern const TTCN_Typedescriptor_t BOOLEAN_descr_;
596 extern const TTCN_Typedescriptor_t INTEGER_descr_;
597 extern const TTCN_Typedescriptor_t FLOAT_descr_;
598 extern const TTCN_Typedescriptor_t VERDICTTYPE_descr_;
599 extern const TTCN_Typedescriptor_t OBJID_descr_;
600 extern const TTCN_Typedescriptor_t BITSTRING_descr_;
601 extern const TTCN_Typedescriptor_t HEXSTRING_descr_;
602 extern const TTCN_Typedescriptor_t OCTETSTRING_descr_;
603 extern const TTCN_Typedescriptor_t CHARSTRING_descr_;
604 extern const TTCN_Typedescriptor_t UNIVERSAL_CHARSTRING_descr_;
605 extern const TTCN_Typedescriptor_t COMPONENT_descr_;
606 extern const TTCN_Typedescriptor_t DEFAULT_descr_;
607 extern const TTCN_Typedescriptor_t ASN_NULL_descr_;
608 extern const TTCN_Typedescriptor_t ASN_ANY_descr_;
609 extern const TTCN_Typedescriptor_t EXTERNAL_descr_;
610 extern const TTCN_Typedescriptor_t EMBEDDED_PDV_descr_;
611 extern const TTCN_Typedescriptor_t CHARACTER_STRING_descr_;
612 extern const TTCN_Typedescriptor_t ObjectDescriptor_descr_;
613 extern const TTCN_Typedescriptor_t UTF8String_descr_;
614 extern const TTCN_Typedescriptor_t ASN_ROID_descr_;
615 extern const TTCN_Typedescriptor_t NumericString_descr_;
616 extern const TTCN_Typedescriptor_t PrintableString_descr_;
617 extern const TTCN_Typedescriptor_t TeletexString_descr_;
618 extern const TTCN_Typedescriptor_t& T61String_descr_;
619 extern const TTCN_Typedescriptor_t VideotexString_descr_;
620 extern const TTCN_Typedescriptor_t IA5String_descr_;
621 extern const TTCN_Typedescriptor_t ASN_GeneralizedTime_descr_;
622 extern const TTCN_Typedescriptor_t ASN_UTCTime_descr_;
623 extern const TTCN_Typedescriptor_t GraphicString_descr_;
624 extern const TTCN_Typedescriptor_t VisibleString_descr_;
625 extern const TTCN_Typedescriptor_t& ISO646String_descr_;
626 extern const TTCN_Typedescriptor_t GeneralString_descr_;
627 extern const TTCN_Typedescriptor_t UniversalString_descr_;
628 extern const TTCN_Typedescriptor_t BMPString_descr_;
629 /** @} */
630
631 #ifdef TITAN_RUNTIME_2
632
633 class Text_Buf;
634 class INTEGER;
635
636 /** The base class of all record-of types when using RT2
637 *
638 */
639 // Record_Of_Template can be found in Template.hh
640 class Record_Of_Type : public Base_Type
641 {
642 friend class Set_Of_Template;
643 friend class Record_Of_Template;
644 protected:
645 friend boolean operator==(null_type null_value,
646 const Record_Of_Type& other_value);
647 struct recordof_setof_struct {
648 int ref_count;
649 int n_elements;
650 Base_Type **value_elements;
651 } *val_ptr;
652 Erroneous_descriptor_t* err_descr;
653
654 /** Stores the indices of elements that are referenced by 'out' and 'inout' parameters.
655 * These elements must not be deleted.*/
656 Vector<int> refd_indices;
657
658 /** Cached maximum value of \a refd_indices (default: -1).*/
659 int max_refd_index;
660
661 static boolean compare_function(const Record_Of_Type *left_ptr, int left_index, const Record_Of_Type *right_ptr, int right_index);
662 Record_Of_Type() : val_ptr(NULL), err_descr(NULL), max_refd_index(-1) {}
663 Record_Of_Type(null_type other_value);
664 Record_Of_Type(const Record_Of_Type& other_value);
665 /// Assignment disabled
666 Record_Of_Type& operator=(const Record_Of_Type& other_value);
667
668 /** Returns the number of actual elements in the record of (does not count the
669 * unbound elements left at the end of the record of struct to make sure
670 * referenced elements are not deleted). */
671 int get_nof_elements() const;
672
673 /** Returns true if the indexed element is bound */
674 bool is_elem_bound(int index) const;
675
676 /** Returns the highest referenced index (uses \a max_refd_index as its cache)*/
677 int get_max_refd_index();
678
679 /** Returns true if the element at the given index is referenced by an 'out' or
680 * 'inout' parameter. */
681 bool is_index_refd(int index);
682
683 public:
684 void set_val(null_type other_value);
685
686 boolean operator==(null_type other_value) const;
687 boolean operator!=(null_type other_value) const;
688
689 Base_Type* get_at(int index_value);
690 Base_Type* get_at(const INTEGER& index_value);
691 const Base_Type* get_at(int index_value) const;
692 const Base_Type* get_at(const INTEGER& index_value) const;
693
694 virtual boolean is_equal(const Base_Type* other_value) const;
695 virtual void set_value(const Base_Type* other_value);
696
697 /* following functions return rec_of or this or other_value */
698 Record_Of_Type* rotl(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
699 Record_Of_Type* rotr(int rotate_count, Record_Of_Type* rec_of) const;
700 Record_Of_Type* rotr(const INTEGER& rotate_count, Record_Of_Type* rec_of) const;
701 Record_Of_Type* concat(const Record_Of_Type* other_value, Record_Of_Type* rec_of) const;
702 /* parameter rec_of must be a newly created descendant object, these helper functions fill it */
703 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
704 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
705 void replace_(int index, int len, const Record_Of_Template* repl, Record_Of_Type* rec_of) const;
706 void replace_(int index, int len, const Set_Of_Template* repl, Record_Of_Type* rec_of) const;
707
708 void set_size(int new_size);
709
710 /** Implementation for isbound.
711 * @return \c true if the Record_Of itself is bound, \c false otherwise.
712 * Ignores elements. No need to be overridden. */
713 virtual boolean is_bound() const;
714 /** Implementation for isvalue.
715 * @return \c true if all elements are bound, \c false otherwise */
716 virtual boolean is_value() const;
717 virtual void clean_up();
718
719 virtual boolean is_seof() const { return TRUE; }
720
721 /** Implementation for the \c sizeof operation
722 * @return the number of elements */
723 int size_of() const;
724 int n_elem() const { return size_of(); }
725
726 /** Implementation for the lengthof operation.
727 * @return the index of the last bound element +1 */
728 int lengthof() const;
729 virtual void log() const;
730 virtual void set_param(Module_Param& param);
731 virtual void set_implicit_omit();
732 virtual void encode_text(Text_Buf& text_buf) const;
733 virtual void decode_text(Text_Buf& text_buf);
734
735 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
736
737 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
738 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
739
740 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
741 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
742 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
743
744 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr, const TTCN_Typedescriptor_t& p_td, RAW_enc_tree& myleaf) const;
745 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
746 /** Decode RAW
747 *
748 * @param td type descriptor
749 * @param buf buffer
750 * @param limit max number of bits that can be used
751 * @param top_bit_ord
752 * @param no_err (not used)
753 * @param sel_field
754 * @param first_call default true, false for second and subsequent calls
755 * of a REPEATABLE record-of. Equivalent to "not repeated"
756 * @return number of bits used, or a negative number in case of error
757 */
758 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
759 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
760 /// raw extension bit
761 virtual int rawdec_ebv() const;
762
763 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
764 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
765 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
766
767 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
768 unsigned int flavor, int indent) const;
769 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
770 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned flavor, int indent) const;
771 /// Helper for XER_encode_negtest
772 int encode_element(int i, const Erroneous_values_t* err_vals,
773 const Erroneous_descriptor_t* emb_descr,
774 TTCN_Buffer& p_buf, unsigned int flavor, int indent) const;
775 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int);
776 virtual boolean isXerAttribute() const;
777 virtual boolean isXmlValueList() const;
778
779 /** Encodes accordingly to the JSON encoding rules.
780 * Returns the length of the encoded data. */
781 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
782
783 /** Decodes accordingly to the JSON encoding rules.
784 * Returns the length of the decoded data. */
785 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
786
787 /** @returns \c true if this is a set-of type,
788 * \c false if this is a record-of type */
789 virtual boolean is_set() const = 0;
790 /** return the type descriptor of the element */
791 virtual const TTCN_Typedescriptor_t* get_elem_descr() const = 0;
792 /** creates an instance of the record's element class, using the default constructor */
793 virtual Base_Type* create_elem() const = 0;
794
795 /** Constant unbound element - return this instead of NULL in constant functions */
796 virtual const Base_Type* get_unbound_elem() const = 0;
797
798 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
799 virtual boolean can_start_v(const char *name, const char *prefix,
800 XERdescriptor_t const& xd, unsigned int flavor) = 0;
801
802 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
803 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
804
805 /** Indicates that the element at the given index is referenced by an 'out' or
806 * 'inout' parameter and must not be deleted.
807 * Used just before the actual function call that references the element. */
808 void add_refd_index(int index);
809
810 /** Indicates that the element at the given index is no longer referenced by
811 * an 'out' or 'inout' parameter.
812 * Used immediately after the actual function call that referenced the element. */
813 void remove_refd_index(int index);
814 };
815
816 extern boolean operator==(null_type null_value,
817 const Record_Of_Type& other_value);
818 extern boolean operator!=(null_type null_value,
819 const Record_Of_Type& other_value);
820
821 ////////////////////////////////////////////////////////////////////////////////
822
823 /** The base class of all record types when using RT2 */
824 // Record_Template can be found in Template.hh
825 class Record_Type : public Base_Type {
826 protected:
827 struct default_struct {
828 int index;
829 const Base_Type* value;
830 };
831 Erroneous_descriptor_t* err_descr;
832 boolean bound_flag;
833 public:
834 Record_Type() : err_descr(NULL), bound_flag(false) {}
835
836 /// @{
837 /** get pointer to a field */
838 virtual Base_Type* get_at(int index_value) = 0;
839 virtual const Base_Type* get_at(int index_value) const = 0;
840 /// @}
841
842 /** get the index to a field based on its name and namespace URI, or -1 */
843 int get_index_byname(const char *name, const char *uri) const;
844
845 /** get number of fields */
846 virtual int get_count() const = 0;
847 /** number of optional fields */
848 virtual int optional_count() const { return 0; }
849
850 /** virtual functions inherited from Base_Type */
851 boolean is_equal(const Base_Type* other_value) const;
852 void set_value(const Base_Type* other_value);
853
854 /** @returns \c true if this is a set type,
855 * \c false if this is a record type */
856 virtual boolean is_set() const = 0;
857
858 /** return the name of a field */
859 virtual const char* fld_name(int field_index) const = 0;
860
861 /** return the descriptor of a field */
862 virtual const TTCN_Typedescriptor_t* fld_descr(int field_index) const = 0;
863
864 /** return int array which contains the indexes of optional fields
865 and a -1 value appended to the end, or return NULL if no optional fields,
866 generated classes override if there are optional fields */
867 virtual const int* get_optional_indexes() const { return NULL; }
868 /** default values and indexes, if no default fields then returns NULL,
869 the last default_struct.index is -1 */
870 virtual const default_struct* get_default_indexes() const { return NULL; }
871
872 /** override if it's TRUE for the specific type (if sdef->opentype_outermost) */
873 virtual boolean is_opentype_outermost() const { return FALSE; }
874
875 virtual boolean default_as_optional() const { return FALSE; }
876
877 virtual boolean is_bound() const;
878 virtual boolean is_value() const;
879 virtual void clean_up();
880 virtual void log() const;
881 virtual void set_param(Module_Param& param);
882 virtual void set_implicit_omit();
883
884 int size_of() const;
885 virtual void encode_text(Text_Buf& text_buf) const;
886 virtual void decode_text(Text_Buf& text_buf);
887
888 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
889 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
890
891 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
892 virtual ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
893 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
894 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
895 virtual void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
896
897 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
898 virtual int RAW_encode_negtest(const Erroneous_descriptor_t *, const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
899 virtual int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t, boolean no_err = FALSE, int sel_field = -1, boolean first_call = TRUE);
900 // Helper functions for RAW enc/dec, shall be overridden in descendants if the default behavior is not enough.
901 virtual boolean raw_has_ext_bit() const { return FALSE; }
902
903 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
904 virtual int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr, const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
905 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
906
907 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
908 unsigned int flavor, int indent) const;
909 virtual int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
910 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
911 unsigned int flavor, int indent) const;
912 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int);
913 /// @{
914 /// Methods overridden in the derived (generated) class
915 virtual int get_xer_num_attr() const { return 0; /* default */ }
916 virtual const XERdescriptor_t* xer_descr(int field_index) const;
917 /// @}
918 virtual char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
919 virtual boolean can_start_v(const char *name, const char *prefix,
920 XERdescriptor_t const& xd, unsigned int flavor) = 0;
921
922 /** Encodes accordingly to the JSON encoding rules.
923 * Returns the length of the encoded data. */
924 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
925
926 /** Decodes accordingly to the JSON encoding rules.
927 * Returns the length of the decoded data. */
928 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
929
930 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
931 Erroneous_descriptor_t* get_err_descr() const { return err_descr; }
932 private:
933 /// Helper for XER_encode_negtest
934 int encode_field(int i, const Erroneous_values_t* err_vals, const Erroneous_descriptor_t* emb_descr,
935 TTCN_Buffer& p_buf, unsigned int sub_flavor, int indent) const;
936 };
937
938 ////////////////////////////////////////////////////////////////////////////////
939 class Empty_Record_Type : public Base_Type {
940 protected:
941 boolean bound_flag;
942
943 Empty_Record_Type();
944 Empty_Record_Type(const Empty_Record_Type& other_value);
945
946 public:
947 boolean operator==(null_type other_value) const;
948 inline boolean operator!=(null_type other_value) const { return !(*this == other_value); }
949
950 /** virtual functions inherited from Base_Type */
951 virtual boolean is_equal(const Base_Type* other_value) const;
952 virtual void set_value(const Base_Type* other_value);
953
954 /** @returns \c true if this is a set type,
955 * \c false if this is a record type */
956 virtual boolean is_set() const = 0;
957
958 inline void set_null() { bound_flag = TRUE; }
959 virtual boolean is_bound() const { return bound_flag; }
960 virtual void clean_up() { bound_flag = FALSE; }
961 virtual void log() const;
962 virtual void set_param(Module_Param& param);
963
964 int size_of() const { return 0; }
965 virtual void encode_text(Text_Buf& text_buf) const;
966 virtual void decode_text(Text_Buf& text_buf);
967
968 virtual void encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...) const;
969 virtual void decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, TTCN_EncDec::coding_t, ...);
970
971 virtual ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
972 virtual boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, const ASN_BER_TLV_t& p_tlv, unsigned L_form);
973
974 virtual int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
975 virtual int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
976
977 virtual int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
978 virtual int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&, boolean no_err=FALSE, boolean first_call=TRUE);
979
980 virtual int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
981 unsigned int flavor, int indent) const;
982 virtual int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, unsigned int);
983
984 /** Encodes accordingly to the JSON encoding rules.
985 * Returns the length of the encoded data. */
986 virtual int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
987
988 /** Decodes accordingly to the JSON encoding rules.
989 * Returns the length of the decoded data. */
990 virtual int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
991 };
992
993 struct Enum_Type : public Base_Type {
994 virtual int as_int() const = 0;
995 virtual void from_int(int) = 0;
996 };
997
998 extern boolean operator==(null_type null_value, const Empty_Record_Type& other_value);
999 extern boolean operator!=(null_type null_value, const Empty_Record_Type& other_value);
1000
1001 #undef VIRTUAL_IF_RUNTIME_2
1002 #endif
1003
1004 template <class EXPR_TYPE>
1005 class Lazy_Param {
1006 protected:
1007 bool expr_evaluated;
1008 EXPR_TYPE expr_cache;
1009 virtual void eval_expr() {}
1010 public:
1011 Lazy_Param(): expr_evaluated(false) {}
1012 enum evaluated_state_t { EXPR_EVALED };
1013 Lazy_Param(evaluated_state_t /*p_es*/, EXPR_TYPE p_cache): expr_evaluated(true), expr_cache(p_cache) {}
1014 operator EXPR_TYPE&() {
1015 if (!expr_evaluated) { eval_expr(); expr_evaluated=true; }
1016 return expr_cache;
1017 }
1018 virtual ~Lazy_Param() {}
1019 };
1020
1021 #endif
This page took 0.100139 seconds and 4 git commands to generate.