CARLA
 
载入中...
搜索中...
未找到
ActorAttribute.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8
9#include "carla/Exception.h"
10#include "carla/Logging.h"
11#include "carla/StringUtil.h"
12
13namespace carla {
14namespace client {
15
16#define LIBCARLA_THROW_INVALID_VALUE(message) throw_exception(InvalidAttributeValue(GetId() + ": " + message));
17#define LIBCARLA_THROW_BAD_VALUE_CAST(type) \
18 if (GetType() != rpc::ActorAttributeType:: type) { \
19 throw_exception(BadAttributeCast(GetId() + ": bad attribute cast: cannot convert to " #type)); \
20 }
21
22 void ActorAttribute::Set(std::string value) {
24 LIBCARLA_THROW_INVALID_VALUE("read-only attribute");
25 }
28 }
29 _attribute.value = std::move(value);
30 Validate();
31 }
32
33
34 template <>
37 auto value = StringUtil::ToLowerCopy(GetValue());
38 if (value == "true") {
39 return true;
40 } else if (value == "false") {
41 return false;
42 }
43 LIBCARLA_THROW_INVALID_VALUE("invalid bool: " + GetValue());
44 }
45
46 template<>
49 return std::atoi(GetValue().c_str());
50 }
51
52 template<>
55 double x = std::atof(GetValue().c_str());
56 if ((x > std::numeric_limits<float>::max()) ||
57 (x < std::numeric_limits<float>::lowest())) {
58 LIBCARLA_THROW_INVALID_VALUE("float overflow");
59 }
60 return static_cast<float>(x);
61 }
62
63 template <>
64 std::string ActorAttributeValueAccess::As<std::string>() const {
66 return GetValue();
67 }
68
69 template <>
70 sensor::data::Color ActorAttributeValueAccess::As<sensor::data::Color>() const {
72
73 std::vector<std::string> channels;
74 StringUtil::Split(channels, GetValue(), ",");
75 if (channels.size() != 3u) {
76 log_error("invalid color", GetValue());
77 LIBCARLA_THROW_INVALID_VALUE("colors must have 3 channels (R,G,B)");
78 }
79
80 auto to_int = [this](const std::string &str) {
81 int i = std::atoi(str.c_str());
82 if (i > std::numeric_limits<uint8_t>::max()) {
83 LIBCARLA_THROW_INVALID_VALUE("integer overflow in color channel");
84 }
85 return static_cast<uint8_t>(i);
86 };
87
88 return {to_int(channels[0u]), to_int(channels[1u]), to_int(channels[2u])};
89 }
90
102
103#undef LIBCARLA_THROW_BAD_VALUE_CAST
104#undef LIBCARLA_THROW_INVALID_VALUE
105
106} // namespace client
107} // namespace carla
#define LIBCARLA_THROW_BAD_VALUE_CAST(type)
#define LIBCARLA_THROW_INVALID_VALUE(message)
static void Split(Container &destination, const Range1T &str, const Range2T &separators)
Definition StringUtil.h:66
static auto ToLowerCopy(const SequenceT &str)
Definition StringUtil.h:41
static void ToLower(WritableRangeT &str)
Definition StringUtil.h:36
virtual const std::string & GetValue() const =0
virtual rpc::ActorAttributeType GetType() const =0
T As() const
Cast the value to the given type.
void Set(std::string value)
Set the value of this attribute.
virtual rpc::ActorAttributeType GetType() const override
auto ActorAttributeValueAccess::As< rpc::ActorAttributeType::String >() const
auto ActorAttributeValueAccess::As< rpc::ActorAttributeType::Bool >() const
auto ActorAttributeValueAccess::As< rpc::ActorAttributeType::RGBColor >() const
auto ActorAttributeValueAccess::As< rpc::ActorAttributeType::Float >() const
auto ActorAttributeValueAccess::As< rpc::ActorAttributeType::Int >() const
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
static void log_error(Args &&... args)
Definition Logging.h:110