Drake
type_specific_identifier.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <string>
5 #include <utility>
6 
9 #include "drake/common/hash.h"
10 
11 namespace drake {
12 namespace maliput {
13 namespace api {
14 
15 /// %TypeSpecificIdentifier<T> represents an identifier specifically identifying
16 /// an entity of type `T`.
17 ///
18 /// A new %TypeSpecificIdentifier is constructed from a non-empty string;
19 /// TypeSpecificIdentifiers constructed from equal strings are considered to
20 /// be equal. There is currently no other semantic value attributed to the
21 /// contents of the string.
22 ///
23 /// Construction from empty strings is not allowed; there is no notion of
24 /// an "unassigned" value for a %TypeSpecificIdentifier. To represent a
25 /// possibly-unassigned %TypeSpecificIdentifier, use
26 /// drake::optional<TypeSpecificIdentifier<T>>.
27 ///
28 /// %TypeSpecificIdentifier is EqualityComparable (and provides == and !=
29 /// operators), but it is not LessThanComparable; there is no particular
30 /// ordering ascribed to %TypeSpecificIdentifier instances. However,
31 /// %TypeSpecificIdentifier does provide a strict weak ordering via a
32 /// specialization of std::less for use in ordered containers such as std::set
33 /// and std::map. This ordering may change in future implementations of
34 /// %TypeSpecificIdentifier.
35 ///
36 /// %TypeSpecificIdentifier also provides a specialization of std::hash to make
37 /// it easy to use with std::unordered_set and std::unordered_map.
38 template <typename T>
40  public:
42 
43  /// The type whose instances are identified by this TypeSpecificIdentifier.
44  typedef T identified_type;
45 
46  /// Constructs a %TypeSpecificIdentifier from the given `string`.
47  ///
48  /// @throws std::runtime_error if `string` is empty.
49  explicit TypeSpecificIdentifier(std::string string)
50  : string_(std::move(string)) {
51  DRAKE_THROW_UNLESS(!string_.empty());
52  }
53 
54  /// Returns the string representation of the %TypeSpecificIdentifier.
55  const std::string& string() const { return string_; }
56 
57  /// Tests for equality with another %TypeSpecificIdentifier.
58  bool operator==(const TypeSpecificIdentifier<T>& rhs) const {
59  return string_ == rhs.string_;
60  }
61 
62  /// Tests for inequality with another %TypeSpecificIdentifier, specifically
63  /// returning the opposite of operator==().
64  bool operator!=(const TypeSpecificIdentifier<T>& rhs) const {
65  return !(*this == rhs);
66  }
67 
68  /// Implements the @ref hash_append concept.
69  template <class HashAlgorithm>
70  friend void hash_append(
71  HashAlgorithm& hasher, const TypeSpecificIdentifier& item) noexcept {
72  using drake::hash_append;
73  hash_append(hasher, item.string_);
74  }
75 
76  private:
77  std::string string_;
78 };
79 
80 } // namespace api
81 } // namespace maliput
82 } // namespace drake
83 
84 
85 namespace std {
86 
87 /// Specialization of std::hash for TypeSpecificIdentifier<T>
88 template <typename T>
89 struct hash<drake::maliput::api::TypeSpecificIdentifier<T>>
90  : public drake::DefaultHash {};
91 
92 /// Specialization of std::less for TypeSpecificIdentifier<T> providing a
93 /// strict weak ordering over TypeSpecificIdentifier<T> suitable for use with
94 /// ordered containers.
95 template <typename T>
96 struct less<drake::maliput::api::TypeSpecificIdentifier<T>> {
97  typedef std::size_t result_type;
100 
101  result_type operator()(const first_argument_type& lhs,
102  const second_argument_type& rhs) const {
103  return lhs.string() < rhs.string();
104  }
105 };
106 
107 } // namespace std
Provides a convenient wrapper to throw an exception when a condition is unmet.
friend void hash_append(HashAlgorithm &hasher, const TypeSpecificIdentifier &item) noexcept
Implements the hash_append generic hashing concept.
Definition: type_specific_identifier.h:70
Definition: automotive_demo.cc:90
STL namespace.
const std::string & string() const
Returns the string representation of the TypeSpecificIdentifier.
Definition: type_specific_identifier.h:55
#define DRAKE_THROW_UNLESS(condition)
Evaluates condition and iff the value is false will throw an exception with a message showing at leas...
Definition: drake_throw.h:23
drake::maliput::api::TypeSpecificIdentifier< T > second_argument_type
Definition: type_specific_identifier.h:99
std::size_t result_type
Definition: type_specific_identifier.h:97
bool operator==(const TypeSpecificIdentifier< T > &rhs) const
Tests for equality with another TypeSpecificIdentifier.
Definition: type_specific_identifier.h:58
result_type operator()(const first_argument_type &lhs, const second_argument_type &rhs) const
Definition: type_specific_identifier.h:101
TypeSpecificIdentifier<T> represents an identifier specifically identifying an entity of type T...
Definition: type_specific_identifier.h:39
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname)
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:57
drake::maliput::api::TypeSpecificIdentifier< T > first_argument_type
Definition: type_specific_identifier.h:98
std::enable_if_t< std::is_integral< T >::value > hash_append(HashAlgorithm &hasher, const T &item) noexcept
Provides hash_append generic hashing for integral constants.
Definition: hash.h:78
bool operator!=(const TypeSpecificIdentifier< T > &rhs) const
Tests for inequality with another TypeSpecificIdentifier, specifically returning the opposite of oper...
Definition: type_specific_identifier.h:64
A hashing functor, somewhat like std::hash.
Definition: hash.h:202
TypeSpecificIdentifier(std::string string)
Constructs a TypeSpecificIdentifier from the given string.
Definition: type_specific_identifier.h:49
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
T identified_type
The type whose instances are identified by this TypeSpecificIdentifier.
Definition: type_specific_identifier.h:41