Skip to content

Commit

Permalink
More modern fixed_array
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Feb 25, 2020
1 parent 9554781 commit 0929c4d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions util/fixed_array.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ template <class T> class FixedArray {
*/
~FixedArray() { clear(); }

#if __cplusplus >= 201103L
FixedArray(FixedArray &&from)
: block_(std::move(from.block_)),
newed_end_(from.newed_end_)
# ifndef NDEBUG
, allocated_end_(from.allocated_end_)
# endif // NDEBUG
{
from.newed_end_ = NULL;
# ifndef NDEBUG
from.allocated_end_ = NULL;
# endif // NDEBUG
}
#endif // C++11

/** Gets a pointer to the first object currently stored in this data structure. */
T *begin() { return static_cast<T*>(block_.get()); }

Expand Down Expand Up @@ -122,6 +137,20 @@ template <class T> class FixedArray {
* The memory backing the constructed object is managed by this data structure.
* I miss C++11 variadic templates.
*/
#if __cplusplus >= 201103L
template <typename... Construct> T *emplace_back(Construct&&... construct) {
T *ret = end();
new (end()) T(construct...);
Constructed();
return ret;
}
template <typename... Construct> T *push_back(Construct&&... construct) {
T *ret = end();
new (end()) T(construct...);
Constructed();
return ret;
}
#else
void push_back() {
new (end()) T();
Constructed();
Expand All @@ -138,6 +167,7 @@ template <class T> class FixedArray {
new (end()) T(c, d);
Constructed();
}
#endif

void pop_back() {
back().~T();
Expand Down

0 comments on commit 0929c4d

Please sign in to comment.