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