6
6
#include " safebase.h"
7
7
8
8
/* *
9
- * Safe wrapper for std::array
10
- * Same as std::vector
11
- * but with more contrains as a std::array is a fixed size container
12
- * SafeArray is ALWAYS initialized with default values, differently from std::array
9
+ * Safe wrapper for `std::array`.
10
+ * Works the same as SafeVector, but with more constraints as a std::array is a fixed size container.
11
+ * SafeArray is ALWAYS initialized with default values, differently from std::array.
12
+ * @see SafeVector
13
+ * @see SafeBase
13
14
*/
14
-
15
- template <typename T, unsigned N>
16
- class SafeArray : public SafeBase {
15
+ template <typename T, unsigned N> class SafeArray : public SafeBase {
17
16
private:
18
- std::array<T, N> array_; // /< The original array
19
- mutable std::unique_ptr<std::map<uint64_t , T>> tmp_; // /< The temporary array
17
+ std::array<T, N> array_; // /< Original array.
18
+ mutable std::unique_ptr<std::map<uint64_t , T>> tmp_; // /< Temporary array.
20
19
21
- // / Check the tmp_ variables!
20
+ // / Check if the temporary array is initialized (and initialize it if not).
22
21
inline void check () const {
23
- if (tmp_ == nullptr ) {
24
- tmp_ = std::make_unique<std::map<uint64_t , T>>();
25
- }
22
+ if (this ->tmp_ == nullptr ) this ->tmp_ = std::make_unique<std::map<uint64_t , T>>();
26
23
}
27
24
28
25
// / Check a index and copy if necessary.
26
+ /* *
27
+ * Check if a specific index exists in the temporary array, copying it from the original if not.
28
+ * @param index The index to check.
29
+ * @throw std::out_of_range if index is bigger than the maximum index of the array.
30
+ */
29
31
inline void checkIndexAndCopy (const uint64_t & index) {
30
32
this ->check ();
31
- if (index >= N) {
32
- throw std::out_of_range (" Index out of range" );
33
- }
34
- if (tmp_->contains (index )) {
35
- return ;
36
- }
37
- tmp_->emplace (index , array_[index ]);
33
+ if (index >= N) throw std::out_of_range (" Index out of range" );
34
+ if (this ->tmp_ ->contains (index )) return ;
35
+ this ->tmp_ ->emplace (index , this ->array_ [index ]);
38
36
}
39
37
40
38
public:
41
-
42
- // / Default Constructor
39
+ /* *
40
+ * Default constructor.
41
+ * @param a (optional) An array of T with fixed size of N to use during construction.
42
+ * Defaults to an empty array.
43
+ */
43
44
SafeArray (const std::array<T, N>& a = {}) : SafeBase(nullptr ) {
44
45
check ();
45
46
uint64_t index = 0 ;
46
47
for (const auto & value : a) {
47
- tmp_->emplace (index , value);
48
- ++ index ;
48
+ this -> tmp_ ->emplace (index , value);
49
+ index ++ ;
49
50
}
50
51
};
51
52
52
- // / Constructor for contracts
53
+ /* *
54
+ * Constructor with owner, for contracts.
55
+ * @param owner The owner of the variable.
56
+ * @param a (optional) An array of T with fixed size of N to use during construction.
57
+ * Defaults to an empty array.
58
+ */
53
59
SafeArray (DynamicContract* owner, const std::array<T, N>& a = {}) : SafeBase(owner) {
54
60
check ();
55
61
uint64_t index = 0 ;
56
62
for (const auto & value : a) {
57
- tmp_->emplace (index , value);
58
- ++ index ;
63
+ this -> tmp_ ->emplace (index , value);
64
+ index ++ ;
59
65
}
60
66
};
61
67
62
- // / Access specified element with bounds checking
68
+ /* *
69
+ * Access a specified element of the temporary array with bounds checking.
70
+ * @param pos The position of the index to access.
71
+ * @return The element at the given index.
72
+ */
63
73
inline T& at (std::size_t pos) {
64
74
checkIndexAndCopy (pos);
65
75
markAsUsed ();
66
- return tmp_->at (pos);
76
+ return this -> tmp_ ->at (pos);
67
77
}
68
78
69
- // / Access specified element with bounds checking (const version)
79
+ /* *
80
+ * Const overload of at().
81
+ * @param pos The position of the index to access.
82
+ * @return The element at the given index.
83
+ */
70
84
const T& at (std::size_t pos) const {
71
85
checkIndexAndCopy (pos);
72
- return tmp_->at (pos);
86
+ return this -> tmp_ ->at (pos);
73
87
}
74
88
75
- // / Access specified element
89
+ /* *
90
+ * Access a specified element of the temporary array without bounds checking.
91
+ * @param pos The position of the index to access.
92
+ * @return The element at the given index.
93
+ */
76
94
inline T& operator [](std::size_t pos) {
77
95
checkIndexAndCopy (pos);
78
96
markAsUsed ();
79
- return (*tmp_)[pos];
97
+ return (*this -> tmp_ )[pos];
80
98
}
81
99
82
- // / Access specified element (const version)
100
+ /* *
101
+ * Const overload of operator[].
102
+ * @param pos The position of the index to access.
103
+ * @return The element at the given index.
104
+ */
83
105
inline const T& operator [](std::size_t pos) const {
84
106
checkIndexAndCopy (pos);
85
- return (*tmp_)[pos];
107
+ return (*this -> tmp_ )[pos];
86
108
}
87
109
88
- // / Return the ORIGINAL array const begin()
89
- inline std::array<T, N>::const_iterator cbegin () const {
90
- return array_.cbegin ();
91
- }
110
+ // / Get an iterator to the beginning of the original array.
111
+ inline std::array<T, N>::const_iterator cbegin () const { return this ->array_ .cbegin (); }
92
112
93
- // / Return the ORIGINAL array const end()
94
- inline std::array<T, N>::const_iterator cend () const {
95
- return array_.cend ();
96
- }
113
+ // / Get an iterator to the end of the original array.
114
+ inline std::array<T, N>::const_iterator cend () const { return this ->array_ .cend (); }
97
115
98
- // / Return the ORIGINAL array const crbegin()
99
- inline std::array<T, N>::const_reverse_iterator crbegin () const {
100
- return array_.crbegin ();
101
- }
116
+ // / Get a reverse iterator to the beginning of the original array.
117
+ inline std::array<T, N>::const_reverse_iterator crbegin () const { return this ->array_ .crbegin (); }
102
118
103
- // / Return the ORIGINAL array const crend()
104
- inline std::array<T, N>::const_reverse_iterator crend () const {
105
- return array_.crend ();
106
- }
119
+ // / Get a reverse iterator to the end of the original array.
120
+ inline std::array<T, N>::const_reverse_iterator crend () const { return this ->array_ .crend (); }
107
121
108
- // / Empty
109
- inline bool empty () const {
110
- return (N == 0 );
111
- }
122
+ /* *
123
+ * Check if the original array is empty (has no elements).
124
+ * @return `true` if array is empty, `false` otherwise.
125
+ */
126
+ inline bool empty () const { return (N == 0 ); }
112
127
113
- // / Size
114
- inline std::size_t size () const {
115
- return N;
116
- }
128
+ /* *
129
+ * Get the current size of the original array.
130
+ * @return The size of the array.
131
+ */
132
+ inline std::size_t size () const { return N; }
117
133
118
- // / Max size
119
- inline std::size_t max_size () const {
120
- return N;
121
- }
134
+ /* *
135
+ * Get the maximum possible size of the original array.
136
+ * @return The maximum size.
137
+ */
138
+ inline std::size_t max_size () const { return N; }
122
139
123
140
/* *
124
- * Fill the array with a value
125
- * @param value The value to fill the array with
126
- */
141
+ * Fill the temporary array with a given value.
142
+ * @param value The value to fill the array with.
143
+ */
127
144
inline void fill (const T& value) {
128
- for (uint64_t i = 0 ; i < N; ++i) {
129
- tmp_->insert_or_assign (i, value);
130
- }
145
+ for (uint64_t i = 0 ; i < N; i++) this ->tmp_ ->insert_or_assign (i, value);
131
146
}
132
147
133
- // / Commit function .
148
+ // / Commit the value. Updates the original array from the temporary one and clears it .
134
149
void commit () override {
135
- for (const auto & [index , value] : *tmp_) {
136
- array_[index ] = value;
137
- }
150
+ for (const auto & [index , value] : *this ->tmp_ ) this ->array_ [index ] = value;
138
151
}
139
152
140
- // / Rollback function.
141
- void revert () const override {
142
- tmp_ = nullptr ;
143
- }
153
+ // / Revert the value. Nullifies the temporary array.
154
+ void revert () const override { this ->tmp_ = nullptr ; }
144
155
};
145
156
146
-
147
- #endif // SAFEARRAY_H
157
+ #endif // SAFEARRAY_H
0 commit comments