6 #include <initializer_list> 10 #include <hilti/base/util.h> 14 using normalizer_func = std::string (*)(std::string);
15 inline std::string identity_normalizer(std::string s) {
return s; }
26 template<
class Derived, normalizer_func N =
identity_normalizer>
30 IDBase(
const char* s) : _id(N(s)) {}
31 explicit IDBase(std::string s) : _id(N(std::move(s))) {}
38 template<
typename... T,
typename enable = std::enable_if_t<(... && std::is_convertible_v<T, std::string>)>>
39 explicit IDBase(
const T&... s) : _id((util::join<std::string>({N(s)...},
"::"))) {}
42 IDBase(
const std::initializer_list<std::string>& x)
43 : _id(util::join(util::transform(x, [](auto i) {
return N(i); }),
"::")) {}
46 const auto&
str()
const {
return _id; }
55 bool empty()
const {
return _id.empty(); }
64 Derived
sub(
int i)
const {
70 return Derived(i >= 0 && static_cast<size_t>(i) < x.size() ? x[i] :
"", AlreadyNormalized());
80 Derived
sub(
int from,
int to)
const {
90 Derived
firstN(
int n)
const {
return Derived(
sub(0, -1 - n), AlreadyNormalized()); }
98 Derived
lastN(
int n)
const {
return Derived(
sub(-1 - n, -1), AlreadyNormalized()); }
107 if ( _id == root._id )
111 return Derived(root + _id, AlreadyNormalized());
113 return Derived(_id.substr(root._id.size() + 2), AlreadyNormalized());
118 Derived n(_id, AlreadyNormalized());
125 Derived n(_id, AlreadyNormalized());
132 if ( ! other.empty() ) {
134 _id = N(std::move(other));
136 _id +=
"::" + N(std::move(other));
139 return static_cast<Derived&
>(*this);
144 if ( ! other._id.empty() ) {
145 if ( other._id.empty() )
148 _id +=
"::" + other._id;
151 return static_cast<Derived&
>(*this);
154 bool operator==(
const Derived& other)
const {
return _id == other._id; };
155 bool operator!=(
const Derived& other)
const {
return _id != other._id; };
156 bool operator==(
const std::string& other)
const {
return _id == N(other); }
157 bool operator!=(
const std::string& other)
const {
return _id != N(other); }
158 bool operator<(
const Derived& other)
const {
return _id < other._id; };
160 explicit operator bool()
const {
return !
empty(); }
161 operator std::string()
const {
return _id; }
Derived & operator+=(const Derived &other)
Definition: id-base.h:143
std::pair< std::string, std::string > rsplit1(std::string s, const std::string &delim=" ")
Definition: util.cc:62
IDBase(const std::initializer_list< std::string > &x)
Definition: id-base.h:42
Derived relativeTo(const Derived &root) const
Definition: id-base.h:106
IDBase(const T &... s)
Definition: id-base.h:39
std::vector< std::string > split(std::string s, const std::string &delim=" ")
Definition: util.cc:35
Derived firstN(int n) const
Definition: id-base.h:90
Derived lastN(int n) const
Definition: id-base.h:98
Derived operator+(const Derived &other) const
Definition: id-base.h:124
Derived sub(int from, int to) const
Definition: id-base.h:80
bool startsWith(const std::string &s, const std::string &prefix)
Definition: util.h:326
std::string join(const T &l, const std::string &delim="")
Definition: util.h:223
Derived namespace_() const
Definition: id-base.h:49
Derived local() const
Definition: id-base.h:52
Derived operator+(const std::string &other) const
Definition: id-base.h:117
Derived sub(int i) const
Definition: id-base.h:64
const auto & str() const
Definition: id-base.h:46
bool empty() const
Definition: id-base.h:55
Derived & operator+=(std::string other)
Definition: id-base.h:131
std::vector< T > slice(const std::vector< T > &v, int begin, int end=-1)
Definition: util.h:196