diff options
author | Treeston <treeston.mmoc@gmail.com> | 2018-12-25 01:47:24 +0100 |
---|---|---|
committer | Treeston <treeston.mmoc@gmail.com> | 2018-12-25 01:47:24 +0100 |
commit | 6d6077e36fe9c5cb8ea8e4f981d637e72ee87037 (patch) | |
tree | 4b1bc526645b3fe02e03b5d08a23cd676eab1c3b /src/common/Utilities/Containers.h | |
parent | afdbc5ffe38e9b845f6ef123849139e846a540ae (diff) |
Core/Utils: Fix Unicode handling
Diffstat (limited to 'src/common/Utilities/Containers.h')
-rw-r--r-- | src/common/Utilities/Containers.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index c43012e6d73..212dc3994f2 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -21,6 +21,8 @@ #include "Define.h" #include "Random.h" #include <algorithm> +#include <exception> +#include <iterator> #include <utility> #include <vector> @@ -38,6 +40,34 @@ namespace Trinity return std::addressof(not_ptr); } + template <class T> + class BufferWriteGuard + { + public: + using iterator_category = std::output_iterator_tag; + using value_type = void; + using pointer = T*; + using reference = T&; + using difference_type = std::ptrdiff_t; + + BufferWriteGuard(T* buf, size_t n) : _buf(buf), _n(n) {} + + T& operator*() const { check(); return *_buf; } + BufferWriteGuard& operator++() { check(); ++_buf; --_n; return *this; } + T* operator++(int) { check(); T* b = _buf; ++_buf; --_n; return b; } + + size_t size() const { return _n; } + + private: + T* _buf; + size_t _n; + void check() + { + if (!_n) + throw std::out_of_range("index"); + } + }; + namespace Containers { // replace with std::size in C++17 |