6 #include <initializer_list> 11 #include <hilti/base/util.h> 15 using normalizer_func = std::string (*)(std::string);
16 inline std::string identity_normalizer(std::string s) {
return s; }
27 template<
class Derived, normalizer_func N =
identity_normalizer>
31 IDBase(
const char* s) : _id(N(s)) {}
32 explicit IDBase(std::string s) : _id(N(std::move(s))) {}
39 template<
typename... T,
typename enable = std::enable_if_t<(... && std::is_convertible_v<T, std::string>)>>
40 explicit IDBase(
const T&... s) : _id((util::join<std::string>({N(s)...},
"::"))) {}
43 IDBase(std::initializer_list<std::string> x)
44 : _id(util::join(util::transform(
std::vector(x), [](auto i) {
return N(i); }),
"::")) {}
47 const auto&
str()
const {
return _id; }
50 Derived
namespace_()
const {
return Derived(util::rsplit1(_id,
"::").first, AlreadyNormalized()); }
53 Derived
local()
const {
return Derived(util::rsplit1(_id,
"::").second, AlreadyNormalized()); }
56 bool empty()
const {
return _id.empty(); }
59 auto length()
const {
return util::split(_id,
"::").size(); }
68 Derived
sub(
int i)
const {
69 auto x = util::split(_id,
"::");
74 return Derived(i >= 0 && static_cast<size_t>(i) < x.size() ? x[i] :
"", AlreadyNormalized());
84 Derived
sub(
int from,
int to)
const {
85 return Derived(util::join(util::slice(util::split(_id,
"::"), from, to),
"::"), AlreadyNormalized());
94 Derived
firstN(
int n)
const {
return Derived(
sub(0, -1 - n), AlreadyNormalized()); }
102 Derived
lastN(
int n)
const {
return Derived(
sub(-1 - n, -1), AlreadyNormalized()); }
111 if ( _id == root._id )
114 if ( ! util::startsWith(_id, root._id +
"::") )
115 return Derived(root + _id, AlreadyNormalized());
117 return Derived(_id.substr(root._id.size() + 2), AlreadyNormalized());
122 Derived n(_id, AlreadyNormalized());
129 Derived n(_id, AlreadyNormalized());
136 if ( ! other.empty() ) {
138 _id = N(std::move(other));
140 _id +=
"::" + N(std::move(other));
143 return static_cast<Derived&
>(*this);
148 if ( ! other._id.empty() ) {
152 _id +=
"::" + other._id;
155 return static_cast<Derived&
>(*this);
158 bool operator==(
const Derived& other)
const {
return _id == other._id; };
159 bool operator!=(
const Derived& other)
const {
return _id != other._id; };
160 bool operator==(
const std::string& other)
const {
return _id == N(other); }
161 bool operator!=(
const std::string& other)
const {
return _id != N(other); }
162 bool operator<(
const Derived& other)
const {
return _id < other._id; };
164 explicit operator bool()
const {
return !
empty(); }
165 operator std::string()
const {
return _id; }
Derived & operator+=(const Derived &other)
Definition: id-base.h:147
IDBase(std::initializer_list< std::string > x)
Definition: id-base.h:43
Definition: optional.h:79
Derived relativeTo(const Derived &root) const
Definition: id-base.h:110
IDBase(const T &... s)
Definition: id-base.h:40
auto length() const
Definition: id-base.h:59
Derived firstN(int n) const
Definition: id-base.h:94
Derived lastN(int n) const
Definition: id-base.h:102
Derived operator+(const Derived &other) const
Definition: id-base.h:128
Derived sub(int from, int to) const
Definition: id-base.h:84
Derived namespace_() const
Definition: id-base.h:50
Derived local() const
Definition: id-base.h:53
Derived operator+(const std::string &other) const
Definition: id-base.h:121
Derived sub(int i) const
Definition: id-base.h:68
const auto & str() const
Definition: id-base.h:47
bool empty() const
Definition: id-base.h:56
Derived & operator+=(std::string other)
Definition: id-base.h:135