Apollo  6.0
Open source self driving car software
parameter.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_PARAMETER_PARAMETER_H_
18 #define CYBER_PARAMETER_PARAMETER_H_
19 
20 #include <string>
21 
22 #include "cyber/proto/parameter.pb.h"
23 
24 #include "cyber/common/log.h"
25 
29 namespace apollo {
30 namespace cyber {
31 
32 using apollo::cyber::proto::Param;
33 using apollo::cyber::proto::ParamType;
34 
42 class Parameter {
43  public:
47  Parameter();
48 
52  explicit Parameter(const Parameter& parameter);
53 
59  explicit Parameter(const std::string& name);
60 
67  Parameter(const std::string& name, const bool bool_value);
68 
75  Parameter(const std::string& name, const int int_value);
76 
83  Parameter(const std::string& name, const int64_t int_value);
84 
91  Parameter(const std::string& name, const float float_value);
92 
99  Parameter(const std::string& name, const double double_value);
100 
107  Parameter(const std::string& name, const std::string& string_value);
108 
115  Parameter(const std::string& name, const char* string_value);
116 
125  Parameter(const std::string& name, const std::string& msg_str,
126  const std::string& full_name, const std::string& proto_desc);
127 
135  Parameter(const std::string& name, const google::protobuf::Message& msg);
136 
144  void FromProtoParam(const Param& param);
145 
151  Param ToProtoParam() const;
152 
157  inline ParamType Type() const;
158 
165  inline std::string TypeName() const;
166 
172  inline std::string Descriptor() const;
173 
179  inline const std::string Name() const;
180 
187  inline bool AsBool() const;
188 
194  inline int64_t AsInt64() const;
195 
201  inline double AsDouble() const;
202 
208  inline const std::string AsString() const;
209 
215  std::string DebugString() const;
216 
225  template <typename ValueType>
226  typename std::enable_if<
228  ValueType>::type
229  value() const;
230 
239  template <typename ValueType>
242  ValueType>::type
243  value() const;
244 
252  template <typename ValueType>
254  ValueType>::type
255  value() const;
256 
264  template <typename ValueType>
266  const std::string&>::type
267  value() const;
268 
276  template <typename ValueType>
278  value() const;
279 
280  private:
281  Param param_;
282 };
283 
284 template <typename ValueType>
285 typename std::enable_if<
287  ValueType>::type
289  ValueType message;
290  if (!message.ParseFromString(param_.string_value())) {
291  AERROR << "The type of parameter \"" << param_.name() << "\" is "
292  << TypeName() << ", not " << ValueType::descriptor()->full_name();
293  }
294  return message;
295 }
296 
297 template <typename ValueType>
300  ValueType>::type
301 Parameter::value() const {
302  if (param_.type() != proto::ParamType::INT) {
303  AERROR << "The type of parameter \"" << param_.name() << "\" is "
304  << TypeName() << ", not INT";
305  }
306  return static_cast<ValueType>(param_.int_value());
307 }
308 
309 template <typename ValueType>
311  ValueType>::type
312 Parameter::value() const {
313  if (param_.type() != proto::ParamType::DOUBLE) {
314  AERROR << "The type of parameter \"" << param_.name() << "\" is "
315  << TypeName() << ", not DOUBLE";
316  }
317  return static_cast<ValueType>(param_.double_value());
318 }
319 
320 template <typename ValueType>
322  const std::string&>::type
323 Parameter::value() const {
324  if (param_.type() != proto::ParamType::STRING &&
325  param_.type() != proto::ParamType::PROTOBUF) {
326  AERROR << "The type of parameter \"" << param_.name() << "\" is "
327  << TypeName() << ", not STRING";
328  }
329  return param_.string_value();
330 }
331 
332 template <typename ValueType>
334 Parameter::value() const {
335  if (param_.type() != proto::ParamType::BOOL) {
336  AERROR << "The type of parameter \"" << param_.name() << "\" is "
337  << TypeName() << ", not BOOL";
338  }
339  return param_.bool_value();
340 }
341 
342 inline ParamType Parameter::Type() const { return param_.type(); }
343 
344 inline std::string Parameter::TypeName() const { return param_.type_name(); }
345 
346 inline std::string Parameter::Descriptor() const { return param_.proto_desc(); }
347 
348 inline const std::string Parameter::Name() const { return param_.name(); }
349 
350 inline bool Parameter::AsBool() const { return value<bool>(); }
351 
352 inline int64_t Parameter::AsInt64() const { return value<int64_t>(); }
353 
354 inline double Parameter::AsDouble() const { return value<double>(); }
355 
356 const std::string Parameter::AsString() const { return value<std::string>(); }
357 
358 } // namespace cyber
359 } // namespace apollo
360 
361 #endif // CYBER_PARAMETER_PARAMETER_H_
void FromProtoParam(const Param &param)
Parse a cyber::proto::Param object to cyber::parameter::Parameter object.
Param ToProtoParam() const
Parse a cyber::parameter::Parameter object to cyber::proto::Param object.
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
A Parameter holds an apollo::cyber::proto::Param, It&#39;s more human-readable, you can use basic-value t...
Definition: parameter.h:42
const std::string AsString() const
Get Paramter as a string value.
Definition: parameter.h:356
Parameter()
Empty constructor.
std::enable_if< std::is_base_of< google::protobuf::Message, ValueType >::value, ValueType >::type value() const
Translate paramter value as a protobuf::Message.
Definition: parameter.h:288
std::string TypeName() const
Get Paramter&#39;s type name, i.e. INT,DOUBLE,STRING or protobuf message&#39;s fullname.
Definition: parameter.h:344
std::string DebugString() const
show debug string
double AsDouble() const
et Paramter as a double value
Definition: parameter.h:354
apollo::cyber::base::std value
#define AERROR
Definition: log.h:44
std::string Descriptor() const
Get Paramter&#39;s descriptor, only work on protobuf types.
Definition: parameter.h:346
bool AsBool() const
Get Paramter as a bool value.
Definition: parameter.h:350
int64_t AsInt64() const
Get Paramter as an int64_t value.
Definition: parameter.h:352
ParamType Type() const
Get the cyber:parameter::ParamType of this object.
Definition: parameter.h:342
const std::string Name() const
Get the Parameter name.
Definition: parameter.h:348