Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_array_cxx.h « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c7704e20fb1a88c01bdc1efefb4967e379cfaac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#ifndef __BLI_ARRAY_CXX_H__
#define __BLI_ARRAY_CXX_H__

/** \file
 * \ingroup bli
 *
 * This is a container that contains a fixed size array. Note however, the size of the array is not
 * a template argument. Instead it can be specified at the construction time.
 */

#include "BLI_utildefines.h"
#include "BLI_allocator.h"
#include "BLI_array_ref.h"
#include "BLI_memory_utils_cxx.h"

namespace BLI {

template<typename T, typename Allocator = GuardedAllocator> class Array {
 private:
  T *m_data;
  uint m_size;
  Allocator m_allocator;

 public:
  Array()
  {
    m_data = nullptr;
    m_size = 0;
  }

  Array(ArrayRef<T> values)
  {
    m_size = values.size();
    m_data = this->allocate(m_size);
    uninitialized_copy_n(values.begin(), m_size, m_data);
  }

  Array(const std::initializer_list<T> &values) : Array(ArrayRef<T>(values))
  {
  }

  explicit Array(uint size)
  {
    m_data = this->allocate(size);
    m_size = size;

    for (uint i = 0; i < m_size; i++) {
      new (m_data + i) T();
    }
  }

  Array(uint size, const T &value)
  {
    m_data = this->allocate(size);
    m_size = size;
    uninitialized_fill_n(m_data, m_size, value);
  }

  Array(const Array &other)
  {
    m_size = other.size();
    m_allocator = other.m_allocator;

    if (m_size == 0) {
      m_data = nullptr;
      return;
    }
    else {
      m_data = this->allocate(m_size);
      copy_n(other.begin(), m_size, m_data);
    }
  }

  Array(Array &&other) noexcept
  {
    m_data = other.m_data;
    m_size = other.m_size;
    m_allocator = other.m_allocator;

    other.m_data = nullptr;
    other.m_size = 0;
  }

  ~Array()
  {
    destruct_n(m_data, m_size);
    if (m_data != nullptr) {
      m_allocator.deallocate((void *)m_data);
    }
  }

  Array &operator=(const Array &other)
  {
    if (this == &other) {
      return *this;
    }

    this->~Array();
    new (this) Array(other);
    return *this;
  }

  Array &operator=(Array &&other)
  {
    if (this == &other) {
      return *this;
    }

    this->~Array();
    new (this) Array(std::move(other));
    return *this;
  }

  operator ArrayRef<T>() const
  {
    return ArrayRef<T>(m_data, m_size);
  }

  operator MutableArrayRef<T>()
  {
    return MutableArrayRef<T>(m_data, m_size);
  }

  ArrayRef<T> as_ref() const
  {
    return *this;
  }

  T &operator[](uint index)
  {
    BLI_assert(index < m_size);
    return m_data[index];
  }

  uint size() const
  {
    return m_size;
  }

  void fill(const T &value)
  {
    MutableArrayRef<T>(*this).fill(value);
  }

  void fill_indices(ArrayRef<uint> indices, const T &value)
  {
    MutableArrayRef<T>(*this).fill_indices(indices, value);
  }

  const T *begin() const
  {
    return m_data;
  }

  const T *end() const
  {
    return m_data + m_size;
  }

  T *begin()
  {
    return m_data;
  }

  T *end()
  {
    return m_data + m_size;
  }

 private:
  T *allocate(uint size)
  {
    return (T *)m_allocator.allocate_aligned(
        size * sizeof(T), std::alignment_of<T>::value, __func__);
  }
};

template<typename T> using TemporaryArray = Array<T, TemporaryAllocator>;

}  // namespace BLI

#endif /* __BLI_ARRAY_CXX_H__ */