OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_const_value.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_CONST_VALUE_H
21 #define T_CONST_VALUE_H
22 
23 #include "t_enum.h"
24 #include <stdint.h>
25 #include <map>
26 #include <vector>
27 #include <string>
28 #include <boost/uuid/uuid.hpp>
29 #include <boost/uuid/string_generator.hpp>
30 
37  public:
38 
46  };
47 
49 
50  t_const_value(int64_t val) {
51  set_integer(val);
52  }
53 
54  t_const_value(std::string val) {
55  set_string(val);
56  }
57 
58  void set_string(std::string val) {
60  stringVal_ = val;
61  }
62 
63  std::string get_string() const {
64  return stringVal_;
65  }
66 
67  void set_integer(int64_t val) {
69  intVal_ = val;
70  }
71 
72  int64_t get_integer() const {
73  if (valType_ == CV_IDENTIFIER) {
74  if (enum_ == NULL) {
75  throw "have identifier \"" + get_identifier() + "\", but unset enum on line!";
76  }
77  std::string identifier = get_identifier();
78  std::string::size_type dot = identifier.rfind('.');
79  if (dot != std::string::npos) {
80  identifier = identifier.substr(dot+1);
81  }
82  t_enum_value* val = enum_->get_constant_by_name(identifier);
83  if (val == NULL) {
84  throw
85  "Unable to find enum value \"" + identifier +
86  "\" in enum \"" + enum_->get_name() + "\"";
87  }
88  return val->get_value();
89  } else {
90  return intVal_;
91  }
92  }
93 
94  void set_double(double val) {
96  doubleVal_ = val;
97  }
98 
99  double get_double() const {
100  return doubleVal_;
101  }
102 
103  std::string get_uuid() const {
104  try {
105  boost::uuids::string_generator()(stringVal_);
106  }
107  catch (...) {
108  throw "Invalid UUID value \"" + stringVal_ + "\"" ;
109  }
110  return stringVal_;
111  }
112 
113  void set_map() {
114  valType_ = CV_MAP;
115  }
116 
118  mapVal_[key] = val;
119  }
120 
121  const std::map<t_const_value*, t_const_value*>& get_map() const {
122  return mapVal_;
123  }
124 
125  void set_list() {
126  valType_ = CV_LIST;
127  }
128 
129  void add_list(t_const_value* val) {
130  listVal_.push_back(val);
131  }
132 
133  const std::vector<t_const_value*>& get_list() const {
134  return listVal_;
135  }
136 
137  void set_identifier(std::string val) {
139  identifierVal_ = val;
140  }
141 
142  std::string get_identifier() const {
143  return identifierVal_;
144  }
145 
146  std::string get_identifier_name() const {
147  std::string ret = get_identifier();
148  size_t s = ret.find('.');
149  if (s == std::string::npos) {
150  throw "error: identifier " + ret + " is unqualified!";
151  }
152  ret = ret.substr(s+1);
153  s = ret.find('.');
154  if (s != std::string::npos) {
155  ret = ret.substr(s+1);
156  }
157  return ret;
158  }
159 
160  std::string get_identifier_with_parent() const {
161  std::string ret = get_identifier();
162  size_t s = ret.find('.');
163  if (s == std::string::npos) {
164  throw "error: identifier " + ret + " is unqualified!";
165  }
166  size_t s2 = ret.find('.', s+1);
167  if (s2 != std::string::npos) {
168  ret = ret.substr(s+1);
169  }
170  return ret;
171  }
172 
173  void set_enum(t_enum* tenum) {
174  enum_ = tenum;
175  }
176 
178  return valType_;
179  }
180 
181  private:
182  std::map<t_const_value*, t_const_value*> mapVal_;
183  std::vector<t_const_value*> listVal_;
184  std::string stringVal_;
185  int64_t intVal_;
186  double doubleVal_;
187  std::string identifierVal_;
189 
191 
192 };
193 
194 #endif
195 
t_enum * enum_
void set_string(std::string val)
Definition: t_const_value.h:58
void set_enum(t_enum *tenum)
std::string get_identifier_name() const
Definition: t_enum.h:30
std::string identifierVal_
std::string get_identifier_with_parent() const
void set_integer(int64_t val)
Definition: t_const_value.h:67
const std::map< t_const_value *, t_const_value * > & get_map() const
void set_identifier(std::string val)
int64_t get_integer() const
Definition: t_const_value.h:72
t_const_value_type get_type() const
std::vector< t_const_value * > listVal_
t_const_value(std::string val)
Definition: t_const_value.h:54
t_const_value_type valType_
t_enum_value * get_constant_by_name(const std::string name)
Definition: t_enum.h:47
std::string get_uuid() const
std::string stringVal_
void set_double(double val)
Definition: t_const_value.h:94
std::string get_identifier() const
virtual const std::string & get_name() const
Definition: t_type.h:56
int get_value()
Definition: t_enum_value.h:54
std::string get_string() const
Definition: t_const_value.h:63
double get_double() const
Definition: t_const_value.h:99
std::map< t_const_value *, t_const_value * > mapVal_
t_const_value(int64_t val)
Definition: t_const_value.h:50
void add_list(t_const_value *val)
void add_map(t_const_value *key, t_const_value *val)
const std::vector< t_const_value * > & get_list() const