Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sysio/vm/exceptions.hpp>
4#include <sysio/vm/allocator.hpp>
5
6#include <algorithm>
7#include <utility>
8#include <string>
9#include <vector>
10
11namespace sysio { namespace vm {
12 namespace detail {
13 template <typename T, typename Allocator>
14 class vector {
15 public:
16 constexpr vector(Allocator& allocator, size_t size=0) :
17 _size(size),
18 _allocator(&allocator),
19 _data(allocator.template alloc<T>( _size )) {
20 }
21
22 constexpr vector(const vector& mv) = delete;
23 constexpr vector(vector&& mv) = default;
24 constexpr vector& operator=(vector&& mv) = default;
25
26 constexpr inline void resize( size_t size ) {
27 if (size > _size) {
28 _data = _allocator->template alloc<T>( size );
29 } else {
30 _allocator->template reclaim<T>( _data + size, _size - size );
31 }
32 _size = size;
33 }
34 template <typename U, typename = std::enable_if_t<std::is_same_v<T, std::decay_t<U>>, int>>
35 constexpr inline void push_back( U&& val ) {
36 // if the vector is unbounded don't assert
37 if ( _index >= _size )
38 resize( _size * 2 );
39 _data[_index++] = std::forward<U>(val);
40 }
41 constexpr inline void emplace_back( T&& val ) {
42 // if the vector is unbounded don't assert
43 if ( _index >= _size )
44 resize( _size * 2 );
45 _data[_index++] = std::move(val);
46 }
47
48 constexpr inline void back() {
49 return _data[_index];
50 }
51
52 constexpr inline void pop_back() {
53 SYS_VM_ASSERT( _index >= 0, wasm_vector_oob_exception, "vector pop out of bounds" );
54 _index--;
55 }
56
57 constexpr inline T& at( size_t i ) {
58 SYS_VM_ASSERT( i < _size, wasm_vector_oob_exception, "vector read out of bounds" );
59 return _data[i];
60 }
61
62 constexpr inline T& at( size_t i )const {
63 SYS_VM_ASSERT( i < _size, wasm_vector_oob_exception, "vector read out of bounds" );
64 return _data[i];
65 }
66
67 constexpr inline T& at_no_check( size_t i ) {
68 return _data[i];
69 }
70
71 constexpr inline T& at_no_check( size_t i ) const {
72 return _data[i];
73 }
74
75 constexpr inline T& operator[] (size_t i) const { return at(i); }
76 constexpr inline T& operator[] (size_t i) { return at(i); }
77 constexpr inline T* raw() const { return _data; }
78 constexpr inline size_t size() const { return _size; }
79 constexpr inline void set( T* data, size_t size, size_t index=-1 ) { _size = size; _data = data; _index = index == -1 ? size - 1 : index; }
80 constexpr inline void copy( T* data, size_t size ) {
81 resize(size);
82 std::copy_n(data, size, _data);
83 _index = size-1;
84 }
85
86 private:
87 size_t _size = 0;
88 Allocator* _allocator = nullptr;
89 T* _data = nullptr;
90 size_t _index = 0;
91 };
92
93 struct unmanaged_base_member {
95 unmanaged_base_member(size_t sz) : alloc(sz) {}
97 };
98 } // ns detail
99
100 template <typename T, typename Allocator>
101 class managed_vector : public detail::vector<T, Allocator> {
102 public:
104 constexpr inline void set_owner( Allocator& alloc ) { detail::vector<T, Allocator>::_allocator = &alloc; }
105 };
106
107 template <typename T>
108 using unmanaged_vector = std::vector<T>;
109
110 template <typename T>
111 std::string vector_to_string( T&& vec ) {
112 std::string str;
113 str.reserve(vec.size());
114 for (int i=0; i < vec.size(); i++)
115 str[i] = vec[i];
116 return str;
117 }
118}} // namespace sysio::vm
Concept for allocating, resizing and freeing memory block.
constexpr vector & operator=(vector &&mv)=default
constexpr vector(Allocator &allocator, size_t size=0)
Definition vector.hpp:16
constexpr T & at_no_check(size_t i)
Definition vector.hpp:67
constexpr void back()
Definition vector.hpp:48
constexpr void set(T *data, size_t size, size_t index=-1)
Definition vector.hpp:79
constexpr void pop_back()
Definition vector.hpp:52
constexpr vector(const vector &mv)=delete
constexpr T & operator[](size_t i) const
Definition vector.hpp:75
constexpr T & at(size_t i) const
Definition vector.hpp:62
constexpr vector(vector &&mv)=default
constexpr void emplace_back(T &&val)
Definition vector.hpp:41
constexpr void push_back(U &&val)
Definition vector.hpp:35
constexpr T & at(size_t i)
Definition vector.hpp:57
constexpr T * raw() const
Definition vector.hpp:77
constexpr T & at_no_check(size_t i) const
Definition vector.hpp:71
constexpr void copy(T *data, size_t size)
Definition vector.hpp:80
constexpr void resize(size_t size)
Definition vector.hpp:26
constexpr size_t size() const
Definition vector.hpp:78
constexpr void set_owner(Allocator &alloc)
Definition vector.hpp:104
bip::allocator< T, pinnable_mapped_file::segment_manager > allocator
Definition chainbase.hpp:56
std::string vector_to_string(T &&vec)
Definition vector.hpp:111
std::vector< T > unmanaged_vector
Definition vector.hpp:108
#define T(meth, val, expected)
#define SYS_VM_ASSERT(expr, exc_type, msg)
Definition exceptions.hpp:8
Definition dtoa.c:306
yubihsm_pkcs11_object_template template