Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef ELEKTRA_KDB_HIGHLEVEL_STRUCTS_HPP
10 : #define ELEKTRA_KDB_HIGHLEVEL_STRUCTS_HPP
11 :
12 : #include <gen/template.hpp>
13 :
14 54 : class StructProcessor
15 : {
16 : private:
17 : std::unordered_map<std::string, std::pair<std::string, std::string>> structTypes;
18 : const kdb::Key & parentKey;
19 : const kdb::KeySet & allKeys;
20 :
21 : static inline bool shouldGenerateTypeDef (const kdb::Key & key);
22 :
23 : public:
24 54 : StructProcessor (const kdb::Key & parentKey_, const kdb::KeySet & allKeys_) : parentKey (parentKey_), allKeys (allKeys_)
25 : {
26 : }
27 :
28 : static inline bool shouldAllocate (const kdb::Key & key);
29 : static inline std::string getType (const kdb::Key & key, const std::string & tagName, bool & genType);
30 :
31 : kainjow::mustache::object process (const kdb::Key & key, const kdb::KeySet & subkeys, const std::string & tagName,
32 : const std::string & specParentName, kainjow::mustache::list & unions);
33 : static bool isFieldIgnored (const kdb::Key & key);
34 : };
35 :
36 12 : class StructFieldsProcessor
37 : {
38 : private:
39 : const kdb::Key & parentKey;
40 : const kdb::KeySet & allKeys;
41 :
42 : kainjow::mustache::list fields;
43 : size_t maxFieldNameLen = 0;
44 : std::string fieldsString;
45 :
46 : const kdb::Key & structKey;
47 : const kdb::KeySet & structKeys;
48 : bool allocating;
49 : const std::string & specParentName;
50 :
51 : kainjow::mustache::list unions;
52 :
53 : bool processed = false;
54 : bool containsStructRef = false;
55 :
56 : static inline std::string getName (const kdb::Key & key, const std::string & fieldKeyName);
57 : static inline std::string arraySizeName (const kdb::Key & key, const std::string & arrayFieldName);
58 : static inline std::string discriminatorField (const kdb::Key & key, const std::string & refFieldName);
59 : static inline bool shouldGenerateUnion (const kdb::Key & key);
60 :
61 : kainjow::mustache::object processArrayStructRef (const kdb::Key & key, const std::string & keyName,
62 : const std::string & fieldKeyName);
63 :
64 : kainjow::mustache::object processStructRef (const kdb::Key & key, const std::string & keyName, const std::string & fieldKeyName);
65 :
66 : kainjow::mustache::object processStructRefUnion (const kdb::Key & checkKey, const kdb::Key & genKey, const std::string & keyName,
67 : bool isArray, const std::string & end, const std::string & fieldKeyName);
68 :
69 : static std::string discriminatorKey (const kdb::Key & key);
70 : static std::string discriminatorUnionType (const kdb::Key & key);
71 : static std::string discriminatorEnumType (const kdb::Key & key);
72 :
73 :
74 : void processAll ();
75 :
76 : public:
77 : StructFieldsProcessor (const kdb::Key & parentKey_, const kdb::KeySet & allKeys_, const kdb::Key & structKey_,
78 : const kdb::KeySet & structKeys_, bool allocating_, const std::string & specParentName_)
79 6 : : parentKey (parentKey_), allKeys (allKeys_), structKey (structKey_), structKeys (structKeys_), allocating (allocating_),
80 24 : specParentName (specParentName_)
81 : {
82 : }
83 :
84 : kainjow::mustache::list getFields ()
85 : {
86 : if (!processed)
87 : {
88 6 : processAll ();
89 : }
90 :
91 6 : return fields;
92 : }
93 :
94 : kainjow::mustache::list getUnions ()
95 : {
96 6 : if (!processed)
97 : {
98 0 : processAll ();
99 : }
100 :
101 6 : return unions;
102 : }
103 :
104 : size_t getMaxFieldNameLen ()
105 : {
106 6 : if (!processed)
107 : {
108 0 : processAll ();
109 : }
110 6 : return maxFieldNameLen;
111 : }
112 :
113 6 : std::string getFieldsString ()
114 : {
115 6 : if (!processed)
116 : {
117 0 : processAll ();
118 : }
119 12 : return fieldsString;
120 : }
121 :
122 : bool getContainsStructRef ()
123 : {
124 6 : if (!processed)
125 : {
126 0 : processAll ();
127 : }
128 6 : return containsStructRef;
129 : }
130 :
131 : static bool processStructRef (const kdb::Key & key, const kdb::Key & parentKey, const kdb::KeySet & allKeys, std::string & typeName,
132 : std::string & nativeType, bool & alloc, std::string & restrict);
133 : static bool processArrayStructRef (const kdb::Key & arrayParent, const kdb::Key & parentKey, const kdb::KeySet & allKeys,
134 : std::string & typeName, std::string & nativeType, bool & alloc, std::string & restrict);
135 : };
136 :
137 :
138 : #endif // ELEKTRA_KDB_HIGHLEVEL_STRUCTS_HPP
|