Created
October 10, 2014 13:53
-
-
Save gnzlbg/435e63a781f387fcb8aa to your computer and use it in GitHub Desktop.
Constexpr storage class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Stack storage implementation | |
/// - preconditions: NoRows != dynamic && NoCols != dynamic | |
template<class T, template <class> class StorageContainer, | |
TInt NoRows, TInt NoCols, TInt MaxRows, TInt MaxCols> | |
struct storage<T, StorageContainer, NoRows, NoCols, MaxRows, MaxCols, stack_storage> { | |
static_assert(NoRows != dynamic, "stack storage with dynamic #of rows"); | |
static_assert(NoCols != dynamic, "stack storage with dynamic #of columns"); | |
static constexpr auto max_no_rows() RETURNS(MaxRows); | |
static constexpr auto max_no_cols() RETURNS(MaxCols); | |
static constexpr auto max_size() RETURNS(max_no_rows() * max_no_cols()); | |
using data_container = T[max_size()];//std::array<T, max_size()>; | |
using iterator = T*; | |
using const_iterator = T const*; | |
using reference = T&; | |
using const_reference = T const&; | |
using value_type = T; | |
constexpr storage() = default; | |
constexpr storage(storage const&) = default; | |
constexpr storage(storage&&) = default; | |
constexpr storage& operator=(storage const&) = default; | |
constexpr storage& operator=(storage&&) = default; | |
constexpr storage(data_container const& other) : data_(other) {} | |
constexpr storage(data_container&& other) : data_(std::move(other)) {} | |
constexpr storage(const TInt no_elements) { | |
ASSERT(no_elements <= max_size(), "#of elements: `" << no_elements | |
<< "` should be <= " << max_size() << "(max_no_rows: " | |
<< max_no_rows() << ", max_no_cols: " << max_no_cols() << ")."); | |
} | |
constexpr storage(const TInt no_rows_, const TInt no_cols_) { | |
ASSERT(no_rows_ <= max_no_rows(), "#of rows: `" << no_rows_ | |
<< "` should be <= " << max_no_rows() << "."); | |
ASSERT(no_cols_ <= max_no_cols(), "#of cols: `" << no_cols_ | |
<< "` should be <= " << max_no_cols() << "."); | |
} | |
constexpr storage(std::initializer_list<T> args) { | |
ASSERT(args.size() == max_size(), "mismatch #of elements"); | |
for (TInt i = 0; i != max_size(); ++i) { | |
data_[i] = std::begin(args)[i]; | |
} | |
} | |
data_container data_; | |
constexpr auto data() noexcept -> data_container & { return data_; } | |
constexpr auto data() const noexcept -> data_container const& { return data_; } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment