CARLA
ActorAttribute.cpp
Go to the documentation of this file.
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 
13 namespace carla {
14 namespace 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  }
27  StringUtil::ToLower(value);
28  }
29  _attribute.value = std::move(value);
30  Validate();
31  }
32 
33 
34  template <>
35  bool ActorAttributeValueAccess::As<bool>() const {
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<>
47  int ActorAttributeValueAccess::As<int>() const {
49  return std::atoi(GetValue().c_str());
50  }
51 
52  template<>
53  float ActorAttributeValueAccess::As<float>() const {
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 
92  switch (GetType()) {
93  case rpc::ActorAttributeType::Bool: As<rpc::ActorAttributeType::Bool>(); break;
94  case rpc::ActorAttributeType::Int: As<rpc::ActorAttributeType::Int>(); break;
95  case rpc::ActorAttributeType::Float: As<rpc::ActorAttributeType::Float>(); break;
96  case rpc::ActorAttributeType::String: As<rpc::ActorAttributeType::String>(); break;
97  case rpc::ActorAttributeType::RGBColor: As<rpc::ActorAttributeType::RGBColor>(); break;
98  default:
99  LIBCARLA_THROW_INVALID_VALUE("invalid value type");
100  }
101  }
102 
103 #undef LIBCARLA_THROW_BAD_VALUE_CAST
104 #undef LIBCARLA_THROW_INVALID_VALUE
105 
106 } // namespace client
107 } // namespace carla
void Set(std::string value)
Set the value of this attribute.
A 32-bit BGRA color.
static auto ToLowerCopy(const SequenceT &str)
Definition: StringUtil.h:41
#define LIBCARLA_THROW_BAD_VALUE_CAST(type)
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
virtual rpc::ActorAttributeType GetType() const override
virtual const std::string & GetValue() const override
#define LIBCARLA_THROW_INVALID_VALUE(message)
static void Split(Container &destination, const Range1T &str, const Range2T &separators)
Definition: StringUtil.h:66
static void ToLower(WritableRangeT &str)
Definition: StringUtil.h:36