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 
47  const_pointer Find(const std::string &key) const;
48 
49  /// @throw std::out_of_range if no such element exists.
50  const_reference at(const std::string &key) const;
51 
52  /// @warning Linear complexity.
54  using diff_t = std::iterator_traits<const_iterator>::difference_type;
55  return std::next(_blueprints.begin(), static_cast<diff_t>(pos))->second;
56  }
57 
58  /// @warning Linear complexity.
59  /// @throw std::out_of_range if !(pos < size()).
60  const_reference at(size_type pos) const;
61 
62  const_iterator begin() const /*noexcept*/ {
64  }
65 
66  const_iterator end() const /*noexcept*/ {
68  }
69 
70  bool empty() const /*noexcept*/ {
71  return _blueprints.empty();
72  }
73 
74  size_type size() const /*noexcept*/ {
75  return _blueprints.size();
76  }
77 
78  private:
79 
81  : _blueprints(std::move(blueprints)) {}
82 
84  };
85 
86 } // namespace client
87 } // 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:99
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