OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #if !defined(ALLOCATED_ARRAY_H) |
| 19 #define ALLOCATED_ARRAY_H |
| 20 |
| 21 template<class T> |
| 22 class AllocatedArray |
| 23 { |
| 24 size_t size; |
| 25 std::unique_ptr<T[]> p; |
| 26 |
| 27 /** |
| 28 * Copy constructor deleted. Allocation is unique to single object. |
| 29 */ |
| 30 AllocatedArray<T>(const AllocatedArray<T>&); // = delete |
| 31 /** |
| 32 * Copy assignment deleted. Allocation is unique to single object. |
| 33 */ |
| 34 AllocatedArray<T>& operator=(const AllocatedArray<T>&); // = delete |
| 35 |
| 36 public: |
| 37 /** |
| 38 * Default constructor. No allocation. |
| 39 */ |
| 40 AllocatedArray() |
| 41 : size(0), |
| 42 p() |
| 43 {} |
| 44 |
| 45 /** |
| 46 * Length constructor. |
| 47 * Initializes each element with default constructor of T(). |
| 48 * Does no allocation if `n` is zero. |
| 49 */ |
| 50 AllocatedArray(size_t n) |
| 51 : size(n), |
| 52 p(n > 0 ? new T[n] : nullptr) // TODO: make_unique once available |
| 53 {} |
| 54 |
| 55 AllocatedArray<T>(AllocatedArray<T>&& x) // = default; |
| 56 { |
| 57 size = x.size; |
| 58 std::swap(p, x.p); |
| 59 } |
| 60 |
| 61 AllocatedArray<T>& operator=(AllocatedArray<T>&& x) // = default; |
| 62 { |
| 63 size = x.size; |
| 64 std::swap(p, x.p); |
| 65 return *this; |
| 66 } |
| 67 |
| 68 T& operator[](size_t i) const |
| 69 { |
| 70 return p.get()[i]; |
| 71 } |
| 72 |
| 73 T* Get() const |
| 74 { |
| 75 return p.get(); |
| 76 } |
| 77 |
| 78 void Reset() |
| 79 { |
| 80 size = 0; |
| 81 p.reset(); |
| 82 } |
| 83 |
| 84 size_t Size() const |
| 85 { |
| 86 return size; |
| 87 } |
| 88 }; |
| 89 |
| 90 #endif |
OLD | NEW |