| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | #include "compat/gsl14.hpp" | ||
| 3 | #include <memory> | ||
| 4 | |||
| 5 | // A fixed-size array. | ||
| 6 | // This is an example of a wrapper for a dynamically allocated array, | ||
| 7 | // similar to std::span. | ||
| 8 | class Array { | ||
| 9 | public: | ||
| 10 | 1 | Array() noexcept = default; // default constructor | |
| 11 | explicit Array(gsl::index size); // constructor | ||
| 12 | Array(const Array& other); // copy constructor | ||
| 13 | Array(Array&& other) noexcept; // move constructor | ||
| 14 | Array& operator=(const Array& other); // copy assignment | ||
| 15 | Array& operator=(Array&& other) noexcept; // move assignment | ||
| 16 | 42 | virtual ~Array() noexcept = default; // destructor | |
| 17 | friend void swap(Array&, Array&) noexcept; // non-member swap | ||
| 18 | |||
| 19 | [[nodiscard]] gsl::index size() const noexcept; | ||
| 20 | const double& operator[](gsl::index) const; | ||
| 21 | double& operator[](gsl::index); | ||
| 22 | |||
| 23 | protected: | ||
| 24 | void assign(Array other) noexcept; | ||
| 25 | void check_bounds(gsl::index size) const; | ||
| 26 | gsl::index size_{0}; | ||
| 27 | std::unique_ptr<double[]> data_{}; // NOLINT(*-avoid-c-arrays) | ||
| 28 | |||
| 29 | private: | ||
| 30 | // See [Non-virtual interface pattern](https://en.wikipedia.org/wiki/Non-virtual_interface_pattern) | ||
| 31 | // See [When should someone use private virtuals?](https://isocpp.org/wiki/faq/strange-inheritance#private-virtuals) | ||
| 32 | // Derived classes can override this method, but cannot call it. | ||
| 33 | virtual void reserve(gsl::index size); | ||
| 34 | }; | ||
| 35 |