Last sync 2016.04.01
[deliverable/titan.core.git] / compiler2 / ttcn3 / Ttcn2Json.hh
1 /******************************************************************************
2 * Copyright (c) 2000-2016 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 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 *
12 ******************************************************************************/
13
14 #ifndef TTCN2JSON_HH
15 #define TTCN2JSON_HH
16
17 #include "../map.hh"
18
19 // forward declarations
20 namespace Common {
21 class Modules;
22 class Value;
23 }
24
25 class JSON_Tokenizer;
26
27 namespace Ttcn {
28
29 /** TTCN-3 to JSON schema converter
30 * Generates a JSON schema from the type and coding function definitions in
31 * TTCN-3 modules */
32 class Ttcn2Json {
33
34 private:
35
36 /** Input modules */
37 Common::Modules* modules;
38
39 Ttcn2Json(const Ttcn2Json&); // no copying
40 Ttcn2Json& operator=(const Ttcn2Json&); // no assignment
41
42 /** Inserts the JSON schema skeleton into the parameter JSON tokenizer and
43 * passes the tokenizer on to the TTCN-3 modules to insert the schemas for
44 * their types and coding functions */
45 void create_schema(JSON_Tokenizer& json);
46
47 /** Inserts a JSON schema to the end of another schema
48 * @param to contains the destination schema
49 * @param from contains the inserted (source) schema; its contents will be
50 * altered (ruined), do not use after this call */
51 void insert_schema(JSON_Tokenizer& to, JSON_Tokenizer& from);
52
53 public:
54
55 /** Initializes this object with the input modules, calls create_schema() to
56 * generate the JSON schema and writes the schema into the given file
57 * @param p_modules input TTCN-3 modules
58 * @param p_schema_name JSON schema file name */
59 Ttcn2Json(Common::Modules* p_modules, const char* p_schema_name);
60
61 };
62
63 /** This class helps generate all combinations of JSON encodings for omitted
64 * fields
65 *
66 * JSON code for omitted optional fields of can be generated in 2 ways:
67 * - the field is not present in the JSON object or
68 * - the field is present and its value is 'null'.
69 * Because of this all record/set values containing omitted fields have 2^N
70 * possible JSON encodings, where N is the number of omitted fields. */
71 class JsonOmitCombination {
72 public:
73 /** This type contains the JSON encoding type of an omitted optional field */
74 enum omit_state_t {
75 NOT_OMITTED, // the field is not omitted
76 OMITTED_ABSENT, // the omitted field is not present in the JSON object
77 OMITTED_NULL // the omitted field is set to 'null' in the JSON object
78 };
79
80 /** Constructor - generates the first combination*/
81 JsonOmitCombination(Common::Value* p_top_level);
82
83 /** Destructor */
84 ~JsonOmitCombination();
85
86 /** Generates the next combination (recursive)
87 *
88 * The algorithm is basically adding 1 to a binary number (whose bits are the
89 * omitted fields of all the records stored in this object, zero bits are
90 * OMITTED_ABSENT, one bits are OMITTED_NULL, all NOT_OMITTEDs are ignored
91 * and the first bit is the least significant bit).
92 *
93 * The constructor generates the first combination, where all omitted fields
94 * are absent (=all zeros).
95 *
96 * Usage: keep calling this function (with its default parameter) until the
97 * last combination (where all omitted fields are 'null', = all ones) is reached.
98 *
99 * @param current_value index of the value whose omitted fields are currently
100 * evaluated (always starts from the first value)
101 *
102 * @return true, if the next combination was successfully generated, or
103 * false, when called with the last combination */
104 bool next(int current_value = 0);
105
106 /** Returns the state of the specified record/set field in the current
107 * combination.
108 *
109 * @param p_rec_value specifies the record value
110 * @param p_comp field index inside the record value */
111 omit_state_t get_state(Common::Value* p_rec_value, size_t p_comp) const;
112
113 private:
114 /** Walks through the given value and all of its components (recursively),
115 * creates a map entry for each record and set value found. The entry contains
116 * which field indexes are omitted (set to OMITTED_ABSENT) and which aren't
117 * (set to NOT_OMITTED). */
118 void parse_value(Common::Value* p_value);
119
120 /** Helper function for the next() function.
121 * Goes through the first values in the map and sets their omitted fields'
122 * states to OMITTED_ABSENT.
123 *
124 * @param value_count specifies how many values to reset */
125 void reset_previous(int value_count);
126
127 /** Map containing the current combination.
128 * Key: pointer to a record or set value in the AST (not owned)
129 * Value: integer array, each integer contains the JSON omitted state of a
130 * field in * the record/set specified by the key, the array's length is equal
131 * to the number of fields in the record/set (owned) */
132 map<Common::Value*, int> combinations;
133 };
134 }
135
136 #endif /* TTCN2JSON_HH */
137
This page took 0.042328 seconds and 5 git commands to generate.