OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_field.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef T_FIELD_H
21 #define T_FIELD_H
22 
23 #include <string>
24 #include <sstream>
25 
26 #include "t_doc.h"
27 
28 // Forward declare for xsd_attrs
29 class t_struct;
30 
36 class t_field : public t_doc {
37  public:
38  t_field(t_type* type, std::string name) :
39  type_(type),
40  name_(name),
41  key_(0),
42 #ifdef SANDESH
44 #endif
45  value_(NULL),
46  xsd_optional_(false),
47  xsd_nillable_(false),
48  xsd_attrs_(NULL)
49 #ifdef SANDESH
50  , auto_generated_(false)
51 #endif
52  {}
53 
54  t_field(t_type* type, std::string name, int32_t key) :
55  type_(type),
56  name_(name),
57  key_(key),
59  value_(NULL),
60  xsd_optional_(false),
61  xsd_nillable_(false),
62  xsd_attrs_(NULL)
63 #ifdef SANDESH
64  , auto_generated_(false)
65 #endif
66  {}
67 
68 #ifdef SANDESH
69  t_field(t_type* type, std::string name, int32_t key, bool auto_generated) :
70  type_(type),
71  name_(name),
72  key_(key),
74  value_(NULL),
75  xsd_optional_(false),
76  xsd_nillable_(false),
77  xsd_attrs_(NULL),
78  auto_generated_(auto_generated){}
79 
80  static std::string auto_string_name(int32_t key) {
81  std::string str;
82  char buf[20];
83  snprintf(buf, 20, "str%d", key);
84  str = buf;
85  return str;
86  }
87 #endif
88 
89  virtual ~t_field() {}
90 
91  t_type* get_type() const {
92  return type_;
93  }
94 
95  const std::string& get_name() const {
96  return name_;
97  }
98 
99  int32_t get_key() const {
100  return key_;
101  }
102 
103  enum e_req {
107  };
108 
109  void set_req(e_req req) {
110  req_ = req;
111  }
112 
113  e_req get_req() const {
114  return req_;
115  }
116 
117  void set_value(t_const_value* value) {
118  value_ = value;
119  }
120 
122  return value_;
123  }
124 
125  void set_xsd_optional(bool xsd_optional) {
126  xsd_optional_ = xsd_optional;
127  }
128 
129  bool get_xsd_optional() const {
130  return xsd_optional_;
131  }
132 
133  void set_xsd_nillable(bool xsd_nillable) {
134  xsd_nillable_ = xsd_nillable;
135  }
136 
137  bool get_xsd_nillable() const {
138  return xsd_nillable_;
139  }
140 
141  void set_xsd_attrs(t_struct* xsd_attrs) {
142  xsd_attrs_ = xsd_attrs;
143  }
144 
146  return xsd_attrs_;
147  }
148 
149 #ifdef SANDESH
150  bool get_auto_generated() const {
151  return auto_generated_;
152  }
153 
154  void set_auto_generated(bool auto_generated) {
155  auto_generated_ = auto_generated;
156  }
157 #endif
158 
159  // This is not the same function as t_type::get_fingerprint_material,
160  // but it does the same thing.
161  std::string get_fingerprint_material() const {
162  std::ostringstream keystm;
163  keystm << key_;
164  return keystm.str() + ":" +
165  ((req_ == T_OPTIONAL) ? "opt-" : "") +
167  }
168 
174  struct key_compare {
175  bool operator()(t_field const * const & a, t_field const * const & b) {
176  return a->get_key() < b->get_key();
177  }
178  };
179 
180 #ifdef SANDESH
181  virtual bool has_key_annotation() const {
182  std::map<std::string, std::string>::const_iterator it;
183  it = annotations_.find("key");
184  if (it != annotations_.end()) {
185  return true;
186  }
187  return false;
188  }
189  virtual bool has_hidden_annotation() const {
190  std::map<std::string, std::string>::const_iterator it;
191  it = annotations_.find("hidden");
192  if (it != annotations_.end()) {
193  return true;
194  }
195  return false;
196  }
197 #endif
198 
199  std::map<std::string, std::string> annotations_;
200 
201  private:
203  std::string name_;
204  int32_t key_;
207 
211 #ifdef SANDESH
212  bool auto_generated_;
213 #endif
214 
215 };
216 
221 struct t_field_id {
222  int64_t value;
224 };
225 
226 #endif
void set_req(e_req req)
Definition: t_field.h:109
const std::string & get_name() const
Definition: t_field.h:95
std::string name_
Definition: t_field.h:203
t_struct * get_xsd_attrs()
Definition: t_field.h:145
Definition: t_type.h:48
virtual std::string get_fingerprint_material() const =0
bool operator()(t_field const *const &a, t_field const *const &b)
Definition: t_field.h:175
t_type * get_type() const
Definition: t_field.h:91
void set_xsd_nillable(bool xsd_nillable)
Definition: t_field.h:133
bool get_xsd_optional() const
Definition: t_field.h:129
virtual ~t_field()
Definition: t_field.h:89
int64_t value
Definition: t_field.h:222
bool auto_assigned
Definition: t_field.h:223
uint8_t type
Definition: load_balance.h:109
Definition: t_doc.h:27
bool xsd_optional_
Definition: t_field.h:208
t_field(t_type *type, std::string name, int32_t key)
Definition: t_field.h:54
bool xsd_nillable_
Definition: t_field.h:209
void set_xsd_optional(bool xsd_optional)
Definition: t_field.h:125
e_req get_req() const
Definition: t_field.h:113
int32_t get_key() const
Definition: t_field.h:99
bool get_xsd_nillable() const
Definition: t_field.h:137
t_type * type_
Definition: t_field.h:202
t_struct * xsd_attrs_
Definition: t_field.h:210
void set_xsd_attrs(t_struct *xsd_attrs)
Definition: t_field.h:141
int32_t key_
Definition: t_field.h:204
e_req req_
Definition: t_field.h:205
void set_value(t_const_value *value)
Definition: t_field.h:117
std::string get_fingerprint_material() const
Definition: t_field.h:161
t_const_value * get_value()
Definition: t_field.h:121
t_const_value * value_
Definition: t_field.h:206
std::map< std::string, std::string > annotations_
Definition: t_field.h:199
t_field(t_type *type, std::string name)
Definition: t_field.h:38