Apollo 10.0
自动驾驶开放平台
parameter.h
浏览该文件的文档.
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
29namespace apollo {
30namespace cyber {
31
34
42class 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<
227 std::is_base_of<google::protobuf::Message, ValueType>::value,
228 ValueType>::type
229 value() const;
230
239 template <typename ValueType>
240 typename std::enable_if<std::is_integral<ValueType>::value &&
241 !std::is_same<ValueType, bool>::value,
242 ValueType>::type
243 value() const;
244
252 template <typename ValueType>
253 typename std::enable_if<std::is_floating_point<ValueType>::value,
254 ValueType>::type
255 value() const;
256
264 template <typename ValueType>
265 typename std::enable_if<std::is_convertible<ValueType, std::string>::value,
266 const std::string&>::type
267 value() const;
268
276 template <typename ValueType>
277 typename std::enable_if<std::is_same<ValueType, bool>::value, bool>::type
278 value() const;
279
280 private:
281 Param param_;
282};
283
284template <typename ValueType>
285typename std::enable_if<
286 std::is_base_of<google::protobuf::Message, ValueType>::value,
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
297template <typename ValueType>
298typename std::enable_if<std::is_integral<ValueType>::value &&
299 !std::is_same<ValueType, bool>::value,
300 ValueType>::type
301Parameter::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
309template <typename ValueType>
310typename std::enable_if<std::is_floating_point<ValueType>::value,
311 ValueType>::type
312Parameter::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
320template <typename ValueType>
321typename std::enable_if<std::is_convertible<ValueType, std::string>::value,
322 const std::string&>::type
323Parameter::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
332template <typename ValueType>
333typename std::enable_if<std::is_same<ValueType, bool>::value, bool>::type
334Parameter::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
342inline ParamType Parameter::Type() const { return param_.type(); }
343
344inline std::string Parameter::TypeName() const { return param_.type_name(); }
345
346inline std::string Parameter::Descriptor() const { return param_.proto_desc(); }
347
348inline const std::string Parameter::Name() const { return param_.name(); }
349
350inline bool Parameter::AsBool() const { return value<bool>(); }
351
352inline int64_t Parameter::AsInt64() const { return value<int64_t>(); }
353
354inline double Parameter::AsDouble() const { return value<double>(); }
355
356const std::string Parameter::AsString() const { return value<std::string>(); }
357
358} // namespace cyber
359} // namespace apollo
360
361#endif // CYBER_PARAMETER_PARAMETER_H_
A Parameter holds an apollo::cyber::proto::Param, It's more human-readable, you can use basic-value t...
Definition parameter.h:42
std::enable_if< std::is_floating_point< ValueType >::value, ValueType >::type value() const
std::enable_if< std::is_base_of< google::protobuf::Message, ValueType >::value, ValueType >::type value() const
Definition parameter.h:288
std::string TypeName() const
Get Paramter's type name, i.e.
Definition parameter.h:344
void FromProtoParam(const Param &param)
Parse a cyber::proto::Param object to cyber::parameter::Parameter object
Definition parameter.cc:112
Parameter()
Empty constructor
Definition parameter.cc:26
double AsDouble() const
et Paramter as a double value
Definition parameter.h:354
std::enable_if< std::is_integral< ValueType >::value &&!std::is_same< ValueType, bool >::value, ValueType >::type value() const
std::string DebugString() const
show debug string
Definition parameter.cc:116
std::enable_if< std::is_convertible< ValueType, std::string >::value, conststd::string & >::type value() const
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
std::string Descriptor() const
Get Paramter's descriptor, only work on protobuf types
Definition parameter.h:346
bool AsBool() const
Get Paramter as a bool value
Definition parameter.h:350
const std::string AsString() const
Get Paramter as a string value
Definition parameter.h:356
Param ToProtoParam() const
Parse a cyber::parameter::Parameter object to cyber::proto::Param object
Definition parameter.cc:114
const std::string Name() const
Get the Parameter name
Definition parameter.h:348
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
optional ParamType type