2015-11-17 22:27:49 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-11-17 22:27:49 +11:00
|
|
|
*
|
2018-06-22 17:41:56 +10:00
|
|
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
2015-11-17 22:27:49 +11:00
|
|
|
*/
|
|
|
|
|
2018-06-27 17:58:45 +10:00
|
|
|
#pragma once
|
2015-11-17 22:27:49 +11:00
|
|
|
|
2018-06-22 17:41:56 +10:00
|
|
|
#include "cast.hpp"
|
|
|
|
#include "types/traits.hpp"
|
|
|
|
|
2015-11-18 14:43:13 +11:00
|
|
|
#include <limits>
|
2015-11-24 16:48:07 +11:00
|
|
|
#include <type_traits>
|
2018-06-22 17:41:56 +10:00
|
|
|
#include <iosfwd>
|
|
|
|
|
2015-11-18 14:43:13 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::strongdef {
|
2015-11-17 22:27:49 +11:00
|
|
|
/// A transparent wrapper around a (typically lightweight) type for the
|
|
|
|
/// purposes of overload disambiguation. It acts like a typesafe typedef.
|
2018-06-27 17:58:45 +10:00
|
|
|
template <typename TagT, typename T = typename TagT::index_type>
|
2019-03-20 15:39:24 +11:00
|
|
|
class index {
|
|
|
|
public:
|
2015-11-17 22:27:49 +11:00
|
|
|
using value_type = T;
|
2018-06-27 17:58:45 +10:00
|
|
|
using tag_type = TagT;
|
2015-11-17 22:27:49 +11:00
|
|
|
|
2019-08-29 14:05:18 +10:00
|
|
|
/// We detect underlying_type in a few situations where we emulate
|
|
|
|
/// 'enum-like' structures. This lets us opt in to this behaviour.
|
|
|
|
using underlying_type = T;
|
|
|
|
|
2018-06-21 13:18:53 +10:00
|
|
|
// TODO: ideally we'd default the constructor, but templated
|
|
|
|
// constructors can't be defaulted? So we just stub one out.
|
2018-06-27 17:58:45 +10:00
|
|
|
constexpr explicit index () { ; }
|
2018-06-22 17:41:56 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
constexpr explicit index (cruft::types::identity_t<T> const &_data): data (_data) { ; }
|
2018-06-27 17:58:45 +10:00
|
|
|
constexpr index (index const&) = default;
|
2019-05-08 10:33:09 +10:00
|
|
|
constexpr index (index &&) = default;
|
2018-06-22 17:41:56 +10:00
|
|
|
|
2018-06-27 17:58:45 +10:00
|
|
|
index& operator= (T const &) = delete;
|
|
|
|
index& operator= (index const &) = default;
|
2019-05-08 10:33:09 +10:00
|
|
|
index& operator= (index &&) = default;
|
2015-11-17 22:27:49 +11:00
|
|
|
|
2015-11-24 16:48:07 +11:00
|
|
|
// conversion operators must not be explicit or it defeats the point
|
|
|
|
// of this class (ease of use, transparency).
|
2018-06-22 17:41:56 +10:00
|
|
|
explicit operator const T& (void) const& { return data; }
|
|
|
|
explicit operator T& (void) & { return data; }
|
|
|
|
|
|
|
|
constexpr auto operator== (T const &) = delete;
|
2018-06-27 17:58:45 +10:00
|
|
|
constexpr auto operator== (index const &rhs) const
|
2018-06-22 17:41:56 +10:00
|
|
|
{
|
|
|
|
return data == rhs.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto operator!= (T const&) = delete;
|
2018-06-27 17:58:45 +10:00
|
|
|
constexpr auto operator!= (index const &rhs) const
|
2018-06-22 17:41:56 +10:00
|
|
|
{
|
|
|
|
return data != rhs.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto operator< (T const &) const = delete;
|
2018-06-27 17:58:45 +10:00
|
|
|
constexpr auto operator< (index const &rhs) const { return data < rhs.data; }
|
2018-06-22 17:41:56 +10:00
|
|
|
|
|
|
|
constexpr auto operator> (T const &) const = delete;
|
2018-06-27 17:58:45 +10:00
|
|
|
constexpr auto operator> (index const &rhs) const { return data > rhs.data; }
|
|
|
|
|
|
|
|
constexpr auto& operator++ (void)& { ++data; return *this; }
|
2015-11-17 22:27:49 +11:00
|
|
|
|
2019-03-20 15:39:24 +11:00
|
|
|
constexpr T * operator-> () &{ return &data; }
|
|
|
|
constexpr T const* operator-> () const &{ return &data; }
|
|
|
|
|
|
|
|
constexpr T & operator* () &{ return data; }
|
|
|
|
constexpr T const& operator* () const &{ return data; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
value_type data;
|
2015-11-17 22:27:49 +11:00
|
|
|
};
|
2018-06-22 17:41:56 +10:00
|
|
|
|
|
|
|
|
2018-06-27 17:58:45 +10:00
|
|
|
template <typename TagT, typename T>
|
2018-06-22 17:41:56 +10:00
|
|
|
std::ostream&
|
2018-06-27 17:58:45 +10:00
|
|
|
operator<< (std::ostream &os, index<TagT,T> const &val)
|
2018-06-22 17:41:56 +10:00
|
|
|
{
|
2019-03-20 15:39:24 +11:00
|
|
|
return os << +*val;
|
2018-06-22 17:41:56 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ContainerT>
|
|
|
|
static auto indices (ContainerT const &obj)
|
|
|
|
{
|
|
|
|
using index_t = typename ContainerT::index_t;
|
|
|
|
|
|
|
|
struct view {
|
|
|
|
view (ContainerT const &_obj):
|
|
|
|
m_obj (_obj)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
struct iterator {
|
|
|
|
iterator (index_t _idx):
|
|
|
|
idx (_idx)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
iterator& operator++ ()
|
|
|
|
{
|
2019-03-20 15:39:24 +11:00
|
|
|
++idx;
|
2018-06-22 17:41:56 +10:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
index_t const& operator* () const { return idx; }
|
|
|
|
index_t const* operator-> () const { return &idx; }
|
|
|
|
|
|
|
|
bool operator!= (iterator const &rhs) { return idx != rhs.idx; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
index_t idx;
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator begin (void) const { return iterator (index_t (0)); }
|
|
|
|
iterator end (void) const
|
|
|
|
{
|
|
|
|
return index_t (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::cast::narrow<
|
2018-06-22 17:41:56 +10:00
|
|
|
typename index_t::value_type
|
|
|
|
> (
|
|
|
|
m_obj.size ()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ContainerT const &m_obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
return view {obj};
|
2015-11-17 22:27:49 +11:00
|
|
|
}
|
2019-05-17 10:55:49 +10:00
|
|
|
|
|
|
|
|
|
|
|
#include "types/description.hpp"
|
|
|
|
namespace cruft::types {
|
|
|
|
template <typename TagT, typename ValueT>
|
|
|
|
struct arity_trait<
|
|
|
|
::cruft::strongdef::index<TagT, ValueT>
|
2019-05-30 10:43:28 +10:00
|
|
|
> : public arity_trait<ValueT>
|
2019-05-17 10:55:49 +10:00
|
|
|
{ ; };
|
|
|
|
|
|
|
|
|
|
|
|
template <typename TagT, typename ValueT>
|
|
|
|
struct category_traits<
|
|
|
|
::cruft::strongdef::index<TagT,ValueT>
|
2019-08-29 14:05:18 +10:00
|
|
|
> : public std::integral_constant<category,category::ENUM>
|
|
|
|
{ ; };
|
2019-05-30 10:43:28 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename TagT, typename ValueT>
|
|
|
|
struct signedness_traits<
|
|
|
|
::cruft::strongdef::index<TagT,ValueT>
|
|
|
|
> : public signedness_traits<ValueT>
|
|
|
|
{ ; };
|
|
|
|
};
|