Apollo 10.0
自动驾驶开放平台
parameter.cc
浏览该文件的文档.
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
18
20
21namespace apollo {
22namespace cyber {
23
25
27 param_.set_name("");
28 param_.set_type(ParamType::NOT_SET);
29}
30
31Parameter::Parameter(const std::string& name) {
32 param_.set_name(name);
33 param_.set_type(proto::ParamType::NOT_SET);
34}
35
37 param_.CopyFrom(parameter.param_);
38}
39
40Parameter::Parameter(const std::string& name, const bool bool_value) {
41 param_.set_name(name);
42 param_.set_bool_value(bool_value);
43 param_.set_type(ParamType::BOOL);
44 param_.set_type_name("BOOL");
45}
46
47Parameter::Parameter(const std::string& name, const int int_value) {
48 param_.set_name(name);
49 param_.set_int_value(int_value);
50 param_.set_type(ParamType::INT);
51 param_.set_type_name("INT");
52}
53
54Parameter::Parameter(const std::string& name, const int64_t int_value) {
55 param_.set_name(name);
56 param_.set_int_value(int_value);
57 param_.set_type(ParamType::INT);
58 param_.set_type_name("INT");
59}
60
61Parameter::Parameter(const std::string& name, const float double_value) {
62 param_.set_name(name);
63 param_.set_double_value(double_value);
64 param_.set_type(ParamType::DOUBLE);
65 param_.set_type_name("DOUBLE");
66}
67
68Parameter::Parameter(const std::string& name, const double double_value) {
69 param_.set_name(name);
70 param_.set_double_value(double_value);
71 param_.set_type(ParamType::DOUBLE);
72 param_.set_type_name("DOUBLE");
73}
74
75Parameter::Parameter(const std::string& name, const std::string& string_value) {
76 param_.set_name(name);
77 param_.set_string_value(string_value);
78 param_.set_type(ParamType::STRING);
79 param_.set_type_name("STRING");
80}
81
82Parameter::Parameter(const std::string& name, const char* string_value) {
83 param_.set_name(name);
84 param_.set_string_value(string_value);
85 param_.set_type(ParamType::STRING);
86 param_.set_type_name("STRING");
87}
88
89Parameter::Parameter(const std::string& name, const std::string& msg_str,
90 const std::string& full_name,
91 const std::string& proto_desc) {
92 param_.set_name(name);
93 param_.set_string_value(msg_str);
94 param_.set_type(ParamType::PROTOBUF);
95 param_.set_type_name(full_name);
96 param_.set_proto_desc(proto_desc);
97}
98
99Parameter::Parameter(const std::string& name,
100 const google::protobuf::Message& msg) {
101 param_.set_name(name);
102 std::string str;
103 msg.SerializeToString(&str);
104 std::string desc;
106 param_.set_string_value(str);
107 param_.set_type(ParamType::PROTOBUF);
108 param_.set_type_name(msg.GetDescriptor()->full_name());
109 param_.set_proto_desc(desc);
110}
111
112void Parameter::FromProtoParam(const Param& param) { param_.CopyFrom(param); }
113
114Param Parameter::ToProtoParam() const { return param_; }
115
116std::string Parameter::DebugString() const {
117 std::stringstream ss;
118 ss << "{name: \"" << param_.name() << "\", ";
119 ss << "type: \"" << TypeName() << "\", ";
120 ss << "value: ";
121 switch (Type()) {
122 case ParamType::BOOL: {
123 ss << (AsBool() ? "true" : "false");
124 break;
125 }
126 case ParamType::INT: {
127 ss << std::to_string(AsInt64());
128 break;
129 }
130 case ParamType::DOUBLE: {
131 ss << std::to_string(AsDouble());
132 break;
133 }
134 case ParamType::STRING: {
135 ss << "\"" << AsString() << "\"";
136 break;
137 }
138 case ParamType::PROTOBUF: {
139 ProtobufFactory::Instance()->RegisterMessage(Descriptor());
140 auto message =
141 ProtobufFactory::Instance()->GenerateMessageByType(TypeName());
142 if (message != nullptr) {
143 message->ParseFromString(AsString());
144 ss << "\"" << message->ShortDebugString() << "\"";
145 delete message;
146 }
147 break;
148 }
149 case ParamType::NOT_SET: {
150 ss << "not set";
151 break;
152 }
153 default:
154 // do nothing
155 break;
156 }
157 ss << "}";
158 return ss.str();
159}
160
161} // namespace cyber
162} // namespace apollo
A Parameter holds an apollo::cyber::proto::Param, It's more human-readable, you can use basic-value t...
Definition parameter.h:42
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::string DebugString() const
show debug string
Definition parameter.cc:116
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
static void GetDescriptorString(const google::protobuf::Message &message, std::string *desc_str)
class register implement
Definition arena_queue.h:37