OpenSDN source code
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  * Modifications Copyright (c) Juniper Networks, Inc.
20  * Copyright Contributors to the OpenSDN project
21  */
22 
23 #ifndef T_FIELD_H
24 #define T_FIELD_H
25 
26 #include <string>
27 #include <sstream>
28 
29 #include "t_doc.h"
30 
31 // Forward declare for xsd_attrs
32 class t_struct;
33 
39 class t_field : public t_doc {
40  public:
41  t_field(t_type* type, std::string name) :
42  type_(type),
43  name_(name),
44  key_(0),
45 #ifdef SANDESH
47 #endif
48  value_(NULL),
49  xsd_optional_(false),
50  xsd_nillable_(false),
51  xsd_attrs_(NULL)
52 #ifdef SANDESH
53  , auto_generated_(false)
54 #endif
55  {}
56 
57  t_field(t_type* type, std::string name, int32_t key) :
58  type_(type),
59  name_(name),
60  key_(key),
62  value_(NULL),
63  xsd_optional_(false),
64  xsd_nillable_(false),
65  xsd_attrs_(NULL)
66 #ifdef SANDESH
67  , auto_generated_(false)
68 #endif
69  {}
70 
71 #ifdef SANDESH
72  t_field(t_type* type, std::string name, int32_t key, bool auto_generated) :
73  type_(type),
74  name_(name),
75  key_(key),
77  value_(NULL),
78  xsd_optional_(false),
79  xsd_nillable_(false),
80  xsd_attrs_(NULL),
81  auto_generated_(auto_generated){}
82 
83  static std::string auto_string_name(int32_t key) {
84  std::string str;
85  char buf[20];
86  snprintf(buf, 20, "str%d", key);
87  str = buf;
88  return str;
89  }
90 #endif
91 
92  virtual ~t_field() {}
93 
94  t_type* get_type() const {
95  return type_;
96  }
97 
98  const std::string& get_name() const {
99  return name_;
100  }
101 
102  int32_t get_key() const {
103  return key_;
104  }
105 
106  enum e_req {
110  };
111 
112  void set_req(e_req req) {
113  req_ = req;
114  }
115 
116  e_req get_req() const {
117  return req_;
118  }
119 
120  void set_value(t_const_value* value) {
121  value_ = value;
122  }
123 
125  return value_;
126  }
127 
128  void set_xsd_optional(bool xsd_optional) {
129  xsd_optional_ = xsd_optional;
130  }
131 
132  bool get_xsd_optional() const {
133  return xsd_optional_;
134  }
135 
136  void set_xsd_nillable(bool xsd_nillable) {
137  xsd_nillable_ = xsd_nillable;
138  }
139 
140  bool get_xsd_nillable() const {
141  return xsd_nillable_;
142  }
143 
144  void set_xsd_attrs(t_struct* xsd_attrs) {
145  xsd_attrs_ = xsd_attrs;
146  }
147 
149  return xsd_attrs_;
150  }
151 
152 #ifdef SANDESH
153  bool get_auto_generated() const {
154  return auto_generated_;
155  }
156 
157  void set_auto_generated(bool auto_generated) {
158  auto_generated_ = auto_generated;
159  }
160 #endif
161 
162  // This is not the same function as t_type::get_fingerprint_material,
163  // but it does the same thing.
164  std::string get_fingerprint_material() const {
165  std::ostringstream keystm;
166  keystm << key_;
167  return keystm.str() + ":" +
168  ((req_ == T_OPTIONAL) ? "opt-" : "") +
170  }
171 
177  struct key_compare {
178  bool operator()(t_field const * const & a, t_field const * const & b) const {
179  return a->get_key() < b->get_key();
180  }
181  };
182 
183 #ifdef SANDESH
184  virtual bool has_key_annotation() const {
185  std::map<std::string, std::string>::const_iterator it;
186  it = annotations_.find("key");
187  if (it != annotations_.end()) {
188  return true;
189  }
190  return false;
191  }
192  virtual bool has_hidden_annotation() const {
193  std::map<std::string, std::string>::const_iterator it;
194  it = annotations_.find("hidden");
195  if (it != annotations_.end()) {
196  return true;
197  }
198  return false;
199  }
200 #endif
201 
202  std::map<std::string, std::string> annotations_;
203 
204  private:
206  std::string name_;
207  int32_t key_;
210 
214 #ifdef SANDESH
215  bool auto_generated_;
216 #endif
217 
218 };
219 
224 struct t_field_id {
225  int64_t value;
227 };
228 
229 #endif
Definition: t_doc.h:30
t_field(t_type *type, std::string name)
Definition: t_field.h:41
int32_t get_key() const
Definition: t_field.h:102
t_const_value * get_value()
Definition: t_field.h:124
const std::string & get_name() const
Definition: t_field.h:98
e_req req_
Definition: t_field.h:208
std::string name_
Definition: t_field.h:206
t_struct * xsd_attrs_
Definition: t_field.h:213
t_type * get_type() const
Definition: t_field.h:94
@ T_OPTIONAL
Definition: t_field.h:108
@ T_OPT_IN_REQ_OUT
Definition: t_field.h:109
@ T_REQUIRED
Definition: t_field.h:107
e_req get_req() const
Definition: t_field.h:116
t_field(t_type *type, std::string name, int32_t key)
Definition: t_field.h:57
t_type * type_
Definition: t_field.h:205
bool get_xsd_nillable() const
Definition: t_field.h:140
void set_value(t_const_value *value)
Definition: t_field.h:120
int32_t key_
Definition: t_field.h:207
virtual ~t_field()
Definition: t_field.h:92
bool get_xsd_optional() const
Definition: t_field.h:132
t_const_value * value_
Definition: t_field.h:209
t_struct * get_xsd_attrs()
Definition: t_field.h:148
void set_req(e_req req)
Definition: t_field.h:112
std::string get_fingerprint_material() const
Definition: t_field.h:164
std::map< std::string, std::string > annotations_
Definition: t_field.h:202
bool xsd_nillable_
Definition: t_field.h:212
void set_xsd_optional(bool xsd_optional)
Definition: t_field.h:128
void set_xsd_nillable(bool xsd_nillable)
Definition: t_field.h:136
void set_xsd_attrs(t_struct *xsd_attrs)
Definition: t_field.h:144
bool xsd_optional_
Definition: t_field.h:211
Definition: t_type.h:51
virtual std::string get_fingerprint_material() const =0
uint8_t type
Definition: load_balance.h:2
bool operator()(t_field const *const &a, t_field const *const &b) const
Definition: t_field.h:178
bool auto_assigned
Definition: t_field.h:226
int64_t value
Definition: t_field.h:225