id.h
Go to the documentation of this file.
#ifndef __utl_container_id_h__
#define __utl_container_id_h__
#include <utl/core/impl.h>
#include <utl/container/array.h>
#include <algorithm>
namespace utl {
template <typename _Tp, size_t _Nm>
struct id_traits {
typedef _Tp type[_Nm];
static constexpr _Tp& Ref (const type& t, size_t n) noexcept {
return const_cast<_Tp&> (t[n]);
}
static constexpr _Tp* Ptr(const type& t) noexcept {
return const_cast<_Tp*> (t);
}
};
template <typename _Tp>
struct id_traits<_Tp, 0> {
struct type { };
static constexpr _Tp& Ref(const type& t, size_t n) noexcept {
return *static_cast<_Tp*>(nullptr);
}
static constexpr _Tp* Ptr(const type& t) noexcept {
return nullptr;
}
};
template <typename _Tp, size_t _Nm>
struct id_t {
using value_type = _Tp;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = value_type*;
using const_iterator = const value_type*;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
using reverse_iterator = std::reverse_iterator <iterator>;
using const_reverse_iterator
= std::reverse_iterator <const_iterator>;
// type and data
using traits_t = id_traits<_Tp, _Nm>;
typename traits_t::type _data;
// No explicit construct/copy/destroy for aggregate type.
// DR 776 (std::array)
void fill (const value_type& v) { std::fill_n (begin(), size(), v); }
void swap (id_t& other) noexcept {
std::swap_ranges (begin(), end(), other.begin());
}
iterator begin() noexcept { return iterator (data()); }
const_iterator begin() const noexcept { return const_iterator (data()); }
iterator end() noexcept { return iterator (data() + _Nm); }
const_iterator end() const noexcept { return const_iterator (data() + _Nm); }
const_iterator cbegin() const noexcept { return const_iterator (data()); }
const_iterator cend() const noexcept { return const_iterator (data() + _Nm); }
reverse_iterator rbegin() noexcept { return reverse_iterator (end()); }
reverse_iterator rend() noexcept { return reverse_iterator (begin()); }
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator (end()); }
const_reverse_iterator rend() const noexcept { return const_reverse_iterator (begin()); }
const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator (end()); }
const_reverse_iterator crend() const noexcept { return const_reverse_iterator (begin()); }
constexpr size_type size() const noexcept { return _Nm; }
constexpr size_type max_size() const noexcept { return _Nm; }
constexpr bool empty() const noexcept { return size() == 0; }
reference operator[] (size_type n) noexcept {
return traits_t::Ref (_data, n);
}
constexpr const_reference operator[] (size_type n) const noexcept {
return traits_t::Ref (_data, n);
}
reference at (size_type n) noexcept {
if (n < _Nm)
return traits_t::Ref (_data, n);
else
abort ();
}
constexpr const_reference at (size_type n) const noexcept {
static_assert ((n < _Nm), "id_t::at: out of range");
return traits_t::Ref (_data, n);
}
constexpr bool bit (uint8_t bit) const noexcept {
return traits_t::Ref (_data, bit/(8*sizeof(_Tp))) &
(static_cast< value_type >(0x01) << ((bit % (8*sizeof(_Tp)))-1));
}
void bit (uint8_t bit, bool v) noexcept {
value_type one = 1;
uint8_t den = 8*sizeof(_Tp)/sizeof(uint8_t);
if (v) traits_t::Ref (_data, bit/den) |= one << ((bit % den)-1);
else traits_t::Ref (_data, bit/den) &= ~one << ((bit % den)-1);
}
// first item
reference front () noexcept {
return *begin ();
}
constexpr const_reference front () const noexcept {
return traits_t::Ref (_data, 0);
}
// Last item
reference back () noexcept {
return _Nm ? *(end() - 1) : *end();
}
constexpr const_reference back () const noexcept {
return _Nm ? traits_t::Ref (_data, _Nm - 1)
: traits_t::Ref (_data, 0);
}
// Pointer to data
pointer data () noexcept { return traits_t::Ptr (_data); }
const_pointer data () const noexcept { return traits_t::Ptr (_data); }
};
template <typename _Tp, size_t _Nm>
inline bool operator== (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
return std::equal (lhs.begin(), lhs.end(), rhs.begin());
}
template <typename _Tp, size_t _Nm>
inline bool operator!= (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
return !(lhs == rhs);
}
template <typename _Tp, size_t _Nm>
inline bool operator< (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
// MSB plays bigger role in comparison
return std::lexicographical_compare(lhs.rbegin(), lhs.rend(), rhs.rbegin(), rhs.rend());
}
template <typename _Tp, size_t _Nm>
inline bool operator> (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
return rhs < lhs;
}
template <typename _Tp, size_t _Nm>
inline bool operator<= (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
return !(lhs > rhs);
}
template <typename _Tp, size_t _Nm>
inline bool operator>= (const id_t<_Tp, _Nm>& lhs, const id_t<_Tp, _Nm>& rhs) {
return !(lhs < rhs);
}
} // namespace utl
#endif /* __utl_container_id_h__ */
utl::operator==
bool operator==(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:181
utl::id_t::back
reference back() noexcept
Definition: id.h:184
utl::id_t< uint8_t, 8 >::const_reference
const value_type & const_reference
Definition: id.h:83
utl::id_t::rbegin
const_reverse_iterator rbegin() const noexcept
Definition: id.h:116
utl::id_traits::Ref
static constexpr _Tp & Ref(const type &t, size_t n) noexcept
Definition: id.h:41
utl::id_t::end
iterator end() noexcept
Definition: id.h:109
utl::id_traits< _Tp, 0 >::Ptr
static constexpr _Tp * Ptr(const type &t) noexcept
Definition: id.h:58
utl::id_t::begin
const_iterator begin() const noexcept
Definition: id.h:108
utl::id_t< uint8_t, 8 >::size_type
size_t size_type
Definition: id.h:86
utl::id_t::front
reference front() noexcept
Definition: id.h:176
utl::id_t::back
constexpr const_reference back() const noexcept
Definition: id.h:187
utl::id_t::_data
traits_t::type _data
Definition: id.h:94
utl::id_t::fill
void fill(const value_type &v)
Definition: id.h:99
utl::id_t< uint8_t, 8 >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: id.h:90
utl::id_t::cbegin
const_iterator cbegin() const noexcept
Definition: id.h:111
utl::id_t< uint8_t, 8 >::const_pointer
const value_type * const_pointer
Definition: id.h:81
utl::id_t::bit
void bit(uint8_t bit, bool v) noexcept
Definition: id.h:169
utl::id_t::cend
const_iterator cend() const noexcept
Definition: id.h:112
array.h
utl::id_traits
id container traits helper
Definition: id.h:38
utl::id_t::crend
const_reverse_iterator crend() const noexcept
Definition: id.h:119
utl::id_t< uint8_t, 8 >::difference_type
std::ptrdiff_t difference_type
Definition: id.h:87
utl::ptrdiff_t
std::ptrdiff_t ptrdiff_t
Definition: types.h:40
utl::id_t::rend
const_reverse_iterator rend() const noexcept
Definition: id.h:117
utl::operator>=
bool operator>=(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:207
utl::id_t< uint8_t, 8 >::pointer
value_type * pointer
Definition: id.h:80
utl::size_t
std::size_t size_t
Definition: types.h:37
utl
STL&#39;s core language concepts.
Definition: _1wire.h:30
utl::id_t::front
constexpr const_reference front() const noexcept
Definition: id.h:179
utl::id_t::data
pointer data() noexcept
Definition: id.h:193
utl::id_t::rend
reverse_iterator rend() noexcept
Definition: id.h:115
utl::id_t< uint8_t, 8 >::reference
value_type & reference
Definition: id.h:82
utl::id_t::max_size
constexpr size_type max_size() const noexcept
Definition: id.h:124
utl::operator>
bool operator>(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:196
utl::id_t::end
const_iterator end() const noexcept
Definition: id.h:110
utl::id_t< uint8_t, 8 >::const_iterator
const value_type * const_iterator
Definition: id.h:85
utl::id_t::swap
void swap(id_t &other) noexcept
Definition: id.h:101
utl::id_t
A standard container for storing IDs as a fixed size sequence of bytes. This type is based on etl::ar...
Definition: id.h:78
utl::id_t< uint8_t, 8 >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: id.h:88
utl::id_t::rbegin
reverse_iterator rbegin() noexcept
Definition: id.h:114
utl::id_t::crbegin
const_reverse_iterator crbegin() const noexcept
Definition: id.h:118
utl::id_t::empty
constexpr bool empty() const noexcept
Definition: id.h:125
utl::id_t::at
reference at(size_type n) noexcept
Definition: id.h:144
utl::operator!=
bool operator!=(const array< _Tp, _Nm > &lhs, const array< _Tp, _Nm > &rhs)
Definition: array.h:186
utl::id_t< uint8_t, 8 >::value_type
uint8_t value_type
Definition: id.h:79
utl::id_t::at
constexpr const_reference at(size_type n) const noexcept
Compile time boundary check dereference operator.
Definition: id.h:151
utl::id_t::data
const_pointer data() const noexcept
Definition: id.h:194
utl::id_t::operator[]
reference operator[](size_type n) noexcept
Operator [].
Definition: id.h:132
utl::id_t::begin
iterator begin() noexcept
Definition: id.h:107
utl::id_traits::Ptr
static constexpr _Tp * Ptr(const type &t) noexcept
Definition: id.h:45
utl::id_traits::type
_Tp type[_Nm]
Definition: id.h:39
utl::id_t< uint8_t, 8 >::iterator
value_type * iterator
Definition: id.h:84
impl.h
Implementation detail main forward header.
utl::id_traits< _Tp, 0 >::Ref
static constexpr _Tp & Ref(const type &t, size_t n) noexcept
Definition: id.h:54
utl::id_t::size
constexpr size_type size() const noexcept
Definition: id.h:123
utl::id_t::bit
constexpr bool bit(uint8_t bit) const noexcept
Definition: id.h:159