CARLA
BlueprintLibrary.h
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 
7 #pragma once
8 
9 #include "carla/Debug.h"
10 #include "carla/Iterator.h"
11 #include "carla/Memory.h"
12 #include "carla/NonCopyable.h"
14 
15 #include <type_traits>
16 #include <unordered_map>
17 #include <vector>
18 
19 namespace carla {
20 namespace client {
21 
22  /// @todo Works as a list but its actually a map. We should assess the use
23  /// cases and reconsider this implementation.
25  : public EnableSharedFromThis<BlueprintLibrary>,
26  private MovableNonCopyable {
27  using map_type = std::unordered_map<std::string, ActorBlueprint>;
28  public:
29 
30  // Here we force a bit the typedefs to make this class look like a list.
31  using key_type = map_type::key_type;
32  using value_type = map_type::mapped_type;
33  using size_type = map_type::size_type;
34  using const_iterator = decltype(carla::iterator::make_map_values_const_iterator<map_type::const_iterator>(map_type::const_iterator{}));
35  using const_reference = const value_type &;
36  using const_pointer = const value_type *;
37 
38  explicit BlueprintLibrary(const std::vector<rpc::ActorDefinition> &blueprints);
39 
40  BlueprintLibrary(BlueprintLibrary &&) = default;
42 
43  /// Filters a list of ActorBlueprint with id or tags matching
44  /// @a wildcard_pattern.
45  SharedPtr<BlueprintLibrary> Filter(const std::string &wildcard_pattern) const;
46  SharedPtr<BlueprintLibrary> FilterByAttribute(const std::string &name, const std::string& value) const;
47 
48  const_pointer Find(const std::string &key) const;
49 
50  /// @throw std::out_of_range if no such element exists.
51  const_reference at(const std::string &key) const;
52 
53  /// @warning Linear complexity.
55  using diff_t = std::iterator_traits<const_iterator>::difference_type;
56  return std::next(_blueprints.begin(), static_cast<diff_t>(pos))->second;
57  }
58 
59  /// @warning Linear complexity.
60  /// @throw std::out_of_range if !(pos < size()).
61  const_reference at(size_type pos) const;
62 
63  const_iterator begin() const /*noexcept*/ {
65  }
66 
67  const_iterator end() const /*noexcept*/ {
69  }
70 
71  bool empty() const /*noexcept*/ {
72  return _blueprints.empty();
73  }
74 
75  size_type size() const /*noexcept*/ {
76  return _blueprints.size();
77  }
78 
79  private:
80 
82  : _blueprints(std::move(blueprints)) {}
83 
85  };
86 
87 } // namespace client
88 } // namespace carla
std::unordered_map< std::string, ActorBlueprint > map_type
SharedPtr< BlueprintLibrary > Filter(const std::string &wildcard_pattern) const
Filters a list of ActorBlueprint with id or tags matching wildcard_pattern.
map_type::mapped_type value_type
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
const_iterator begin() const
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
const value_type & const_reference
const_pointer Find(const std::string &key) const
const_reference at(const std::string &key) const
Inherit (privately) to suppress copy construction and assignment.
decltype(carla::iterator::make_map_values_const_iterator< map_type::const_iterator >(map_type::const_iterator{})) const_iterator
BlueprintLibrary & operator=(BlueprintLibrary &&)=default
static auto make_map_values_const_iterator(It it)
Creates an iterator over const references to the values of a map.
Definition: Iterator.h:43
BlueprintLibrary(map_type blueprints)
BlueprintLibrary(const std::vector< rpc::ActorDefinition > &blueprints)
const_iterator end() const
const_reference operator[](size_type pos) const
SharedPtr< BlueprintLibrary > FilterByAttribute(const std::string &name, const std::string &value) const