OpenDDS  Snapshot(2023/04/28-20:55)
topic_keys.h
Go to the documentation of this file.
1 #ifndef TOPIC_KEYS_HEADER
2 #define TOPIC_KEYS_HEADER
3 
4 #include <string>
5 #include <iterator>
6 #include <exception>
7 #include <sstream>
8 #include <vector>
9 
10 class AST_Decl;
11 class AST_Type;
12 class AST_Structure;
13 class AST_Field;
14 class AST_Union;
15 
16 /**
17  * Find Keys in Topic Types
18  *
19  * Use like this:
20  * AST_Structure* struct_node;
21  * // ...
22  * TopicKeys keys(struct_node);
23  * TopicKeys::Iterator end = keys.end();
24  * for (TopicKeys::Iterator i = keys.begin(); i != end; ++i) {
25  * AST_Decl* key = *i;
26  * // ...
27  * }
28  *
29  * The key AST_Decl will be a different type of node depending on the key:
30  * - For struct field keys this will be the AST_Field
31  * - For Array keys this will be the base AST_Type, repeated for each element
32  * - For Union keys this will be the AST_Union
33  */
34 class TopicKeys {
35 public:
36  enum RootType {
43  };
44 
45  /**
46  * Get the RootType of the AST_Type
47  */
48  static RootType root_type(AST_Type* type);
49 
50  /**
51  * Error in the AST or the application of the @key annotation.
52  */
53  class Error : public std::exception {
54  public:
55  Error();
56  Error(AST_Decl* node, const std::string& message);
57  virtual ~Error() throw ();
58 
59  Error& operator=(const Error& error);
60  virtual const char* what() const throw();
61  AST_Decl* node();
62 
63  private:
64  AST_Decl* node_;
65  std::string message_;
66  };
67 
68  /**
69  * Iterator for traversing the TAO_IDL AST, looking for nodes within a topic
70  * type node that are annotated with @key.
71  */
72  class Iterator {
73  public:
74  /**
75  * Standard Iterator Type Declarations
76  */
77  ///{
78  typedef AST_Decl* value_type;
79  typedef AST_Decl** pointer;
80  typedef AST_Decl*& reference;
81  typedef std::input_iterator_tag iterator_category;
82  ///}
83 
84  /**
85  * Create new iterator equal to TopicKeys::end()
86  */
87  Iterator();
88 
89  /**
90  * Create new iterator pointing to the first topic key or equal to
91  * TopicKeys::end() if there are no keys.
92  */
93  Iterator(TopicKeys &parent);
94 
95  /**
96  * Create a completely separate copy of another iterator
97  */
98  Iterator(const Iterator& other);
99 
100  ~Iterator();
101 
102  Iterator& operator=(const Iterator& other);
103  Iterator& operator++(); // Prefix
104  Iterator operator++(int); // Postfix
105  AST_Decl* operator*() const;
106  bool operator==(const Iterator& other) const;
107  bool operator!=(const Iterator& other) const;
108 
109  /**
110  * Get the path of the key in reference to the root_
111  */
112  std::string path();
113  std::string canonical_path();
114 
115  /**
116  * Get the level of recursion
117  */
118  size_t level() const;
119 
120  /**
121  * Get the root type of the final child
122  */
123  RootType root_type() const;
124 
125  /**
126  * Get the final child's parent's root type
127  *
128  * Returns InvalidType if it is the root or a invalid iterator
129  */
130  RootType parents_root_type() const;
131 
132  /**
133  * Get the AST_Type of the current value
134  *
135  * Returns 0 if the iterator is invalid
136  */
137  AST_Type* get_ast_type() const;
138 
139  static Iterator end_value();
140 
141  private:
142  Iterator(AST_Type* root, Iterator* parent);
143  Iterator(AST_Field* root, Iterator* parent);
144 
146  unsigned pos_;
148  /// Current value of the entire iterator stack
149  AST_Decl* current_value_;
150  AST_Decl* root_;
152  size_t level_;
154  /// The Dimensions of the Array
155  std::vector<size_t> dimensions_;
156  /// Element Count in the Array
158 
159  /// Used in struct field key iteration
161 
162  /**
163  * Internal Recursive Impl. of path()
164  */
165  void path_i(std::stringstream& ss, bool canonical = false);
166 
167  void cleanup();
168  };
169 
170  TopicKeys();
171  TopicKeys(const TopicKeys& other);
172  /**
173  * If recursive is false, do a shallow iteration.
174  */
175  TopicKeys(AST_Structure* root, bool recursive = true);
176  TopicKeys(AST_Union* root);
177  ~TopicKeys();
178 
179  TopicKeys& operator=(const TopicKeys& other);
180  AST_Decl* root() const;
181  RootType root_type() const;
182 
183  Iterator begin();
184  Iterator end();
185 
186  /**
187  * Count the keys in the topic type
188  */
189  size_t count();
190 
191  bool recursive() const;
192 
193 private:
194  AST_Decl* root_;
196 
197  /// Cached Key Count
198  ///{
199  bool counted_;
200  size_t count_;
201  ///}
202 
203  /// Have iterators recurse into structures
205 };
206 
207 #endif
AST_Decl * node_
Definition: topic_keys.h:64
Iterator end()
Definition: topic_keys.cpp:500
size_t element_count_
Element Count in the Array.
Definition: topic_keys.h:157
virtual const char * what() const
Definition: topic_keys.cpp:61
AST_Decl * current_value_
Current value of the entire iterator stack.
Definition: topic_keys.h:149
bool operator==(const DisjointSequence::OrderedRanges< T > &a, const DisjointSequence::OrderedRanges< T > &b)
RootType root_type_
Definition: topic_keys.h:195
std::input_iterator_tag iterator_category
Definition: topic_keys.h:81
bool implied_keys_
Used in struct field key iteration.
Definition: topic_keys.h:160
AST_Decl * root_
Definition: topic_keys.h:194
size_t count()
Definition: topic_keys.cpp:515
size_t count_
Definition: topic_keys.h:200
AST_Decl *& reference
Definition: topic_keys.h:80
Iterator * parent_
Definition: topic_keys.h:145
std::string message_
Definition: topic_keys.h:65
std::vector< size_t > dimensions_
The Dimensions of the Array.
Definition: topic_keys.h:155
Error & operator=(const Error &error)
Definition: topic_keys.cpp:54
virtual ~Error()
Definition: topic_keys.cpp:50
AST_Decl ** pointer
Definition: topic_keys.h:79
bool recursive() const
Definition: topic_keys.cpp:528
bool operator!=(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:125
AST_Decl * node()
Definition: topic_keys.cpp:66
AST_Decl * root() const
Definition: topic_keys.cpp:505
Iterator begin()
Definition: topic_keys.cpp:495
OpenDDS_Dcps_Export TimeDuration operator*(double x, const TimeDuration &y)
bool recursive_
}
Definition: topic_keys.h:204
Iterator * child_
Definition: topic_keys.h:147
AST_Decl * root_
Definition: topic_keys.h:150
AST_Decl * value_type
{
Definition: topic_keys.h:78
bool counted_
Definition: topic_keys.h:199
RootType root_type() const
Definition: topic_keys.cpp:510