Taskolib  1.4.4
VariableName.h
Go to the documentation of this file.
1 
24 // SPDX-License-Identifier: LGPL-2.1-or-later
25 
26 #ifndef TASKOLIB_VARIABLENAME_H_
27 #define TASKOLIB_VARIABLENAME_H_
28 
29 #include <functional>
30 #include <string>
31 #include <string_view>
32 
33 #include <gul17/cat.h>
34 
35 namespace task {
36 
46 {
47 public:
48  using SizeType = std::string::size_type;
49  using size_type = std::string::size_type;
50 
59  VariableName(const char* name);
60 
66  explicit VariableName(const std::string& name);
67  explicit VariableName(std::string&& name);
68 
70  SizeType length() const noexcept { return name_.size(); }
71 
73  friend bool operator==(const VariableName& a, const VariableName& b) noexcept
74  {
75  return a.string() == b.string();
76  }
77 
79  friend bool operator!=(const VariableName& a, const VariableName& b) noexcept
80  {
81  return a.string() != b.string();
82  }
83 
85  friend bool operator<(const VariableName& a, const VariableName& b) noexcept
86  {
87  return a.string() < b.string();
88  }
89 
91  friend bool operator>(const VariableName& a, const VariableName& b) noexcept
92  {
93  return a.string() > b.string();
94  }
95 
100  friend bool operator<=(const VariableName& a, const VariableName& b) noexcept
101  {
102  return a.string() <= b.string();
103  }
104 
109  friend bool operator>=(const VariableName& a, const VariableName& b) noexcept
110  {
111  return a.string() >= b.string();
112  }
113 
119  VariableName& operator+=(std::string_view suffix);
120 
122  friend std::string& operator+=(std::string& lhs, const VariableName& rhs)
123  {
124  lhs += rhs.string();
125  return lhs;
126  }
127 
129  friend std::string operator+(const VariableName& lhs, std::string_view rhs)
130  {
131  return gul17::cat(lhs.string(), rhs);
132  }
133 
135  friend std::string operator+(std::string_view lhs, const VariableName& rhs)
136  {
137  return gul17::cat(lhs, rhs.string());
138  }
139 
141  explicit operator const std::string&() const { return name_; }
142 
144  SizeType size() const noexcept { return name_.size(); }
145 
147  const std::string& string() const noexcept { return name_; }
148 
149 private:
150  std::string name_;
151 };
152 
153 } // namespace task
154 
155 
156 namespace std {
157 
159 template<>
160 struct hash<task::VariableName>
161 {
162  std::size_t operator()(const task::VariableName& name) const noexcept
163  {
164  return std::hash<std::string>{}(name.string());
165  }
166 };
167 
168 } // namespace std
169 
170 #endif
A variable name is a string with limited functionality and some limitations on the allowed characters...
Definition: VariableName.h:46
friend bool operator==(const VariableName &a, const VariableName &b) noexcept
Determine if two variable names are identical.
Definition: VariableName.h:73
friend std::string & operator+=(std::string &lhs, const VariableName &rhs)
Append a VariableName to a std::string.
Definition: VariableName.h:122
friend bool operator<(const VariableName &a, const VariableName &b) noexcept
Determine if the left variable name is lexicographically less than the right one.
Definition: VariableName.h:85
VariableName(const char *name)
Construct a variable name from a C string.
Definition: VariableName.cc:62
friend std::string operator+(const VariableName &lhs, std::string_view rhs)
Concatenate a VariableName and a string_view.
Definition: VariableName.h:129
friend bool operator!=(const VariableName &a, const VariableName &b) noexcept
Determine if two variable names differ.
Definition: VariableName.h:79
const std::string & string() const noexcept
Return a const reference to the internal string member.
Definition: VariableName.h:147
SizeType length() const noexcept
Return the length of the variable name string.
Definition: VariableName.h:70
friend bool operator>(const VariableName &a, const VariableName &b) noexcept
Determine if the left variable name is lexicographically greater than the right one.
Definition: VariableName.h:91
std::string name_
Definition: VariableName.h:150
std::string::size_type SizeType
Definition: VariableName.h:48
friend bool operator<=(const VariableName &a, const VariableName &b) noexcept
Determine if the left variable name is lexicographically less than or equal to the right one.
Definition: VariableName.h:100
VariableName & operator+=(std::string_view suffix)
Append a suffix to a VariableName.
Definition: VariableName.cc:83
SizeType size() const noexcept
Return the length of the variable name string.
Definition: VariableName.h:144
friend std::string operator+(std::string_view lhs, const VariableName &rhs)
Concatenate a string_view and a VariableName.
Definition: VariableName.h:135
friend bool operator>=(const VariableName &a, const VariableName &b) noexcept
Determine if the left variable name is lexicographically greater than or equal to the right one.
Definition: VariableName.h:109
std::string::size_type size_type
Definition: VariableName.h:49
Definition: VariableName.h:156
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
std::size_t operator()(const task::VariableName &name) const noexcept
Definition: VariableName.h:162