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

FN_generic_array_ref.h « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e46fa4eafbe548885873465a328c49f9841372b (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
#ifndef __FN_GENERIC_ARRAY_REF_H__
#define __FN_GENERIC_ARRAY_REF_H__

#include "FN_cpp_type.h"

#include "BLI_array_ref.h"

namespace FN {

using BLI::ArrayRef;
using BLI::MutableArrayRef;

class GenericArrayRef {
 private:
  const CPPType *m_type;
  const void *m_buffer;
  uint m_size;

 public:
  GenericArrayRef(const CPPType &type) : GenericArrayRef(type, nullptr, 0)
  {
  }

  GenericArrayRef(const CPPType &type, const void *buffer, uint size)
      : m_type(&type), m_buffer(buffer), m_size(size)
  {
    BLI_assert(buffer != nullptr || size == 0);
    BLI_assert(type.pointer_has_valid_alignment(buffer));
  }

  template<typename T>
  GenericArrayRef(ArrayRef<T> array)
      : GenericArrayRef(CPP_TYPE<T>(), (const void *)array.begin(), array.size())
  {
  }

  const CPPType &type() const
  {
    return *m_type;
  }

  uint size() const
  {
    return m_size;
  }

  const void *buffer() const
  {
    return m_buffer;
  }

  const void *operator[](uint index) const
  {
    BLI_assert(index < m_size);
    return POINTER_OFFSET(m_buffer, m_type->size() * index);
  }

  template<typename T> ArrayRef<T> as_typed_ref() const
  {
    BLI_assert(CPP_TYPE<T>() == *m_type);
    return ArrayRef<T>((const T *)m_buffer, m_size);
  }
};

class GenericMutableArrayRef {
 private:
  const CPPType *m_type;
  void *m_buffer;
  uint m_size;

 public:
  GenericMutableArrayRef(const CPPType &type) : GenericMutableArrayRef(type, nullptr, 0)
  {
  }

  GenericMutableArrayRef(const CPPType &type, void *buffer, uint size)
      : m_type(&type), m_buffer(buffer), m_size(size)
  {
    BLI_assert(buffer != nullptr || size == 0);
    BLI_assert(type.pointer_has_valid_alignment(buffer));
  }

  template<typename T>
  GenericMutableArrayRef(MutableArrayRef<T> array)
      : GenericMutableArrayRef(CPP_TYPE<T>(), (void *)array.begin(), array.size())
  {
  }

  operator GenericArrayRef() const
  {
    return GenericArrayRef(*m_type, m_buffer, m_size);
  }

  void destruct_all()
  {
    m_type->destruct_n(m_buffer, m_size);
  }

  void destruct_indices(IndexMask indices)
  {
    m_type->destruct_indices(m_buffer, indices);
  }

  GenericMutableArrayRef slice(uint start, uint size)
  {
    BLI_assert(start + size <= m_size);
    return GenericMutableArrayRef(*m_type, POINTER_OFFSET(m_buffer, start * m_type->size()), size);
  }

  const CPPType &type() const
  {
    return *m_type;
  }

  void *buffer()
  {
    return m_buffer;
  }

  uint size() const
  {
    return m_size;
  }

  void default_initialize(IndexMask indices)
  {
    m_type->construct_default_indices(m_buffer, indices);
  }

  void fill__uninitialized(const void *value)
  {
    m_type->fill_uninitialized(value, m_buffer, m_size);
  }

  void fill__initialized(const void *value)
  {
    m_type->fill_initialized(value, m_buffer, m_size);
  }

  void copy_in__uninitialized(uint index, const void *src)
  {
    BLI_assert(index < m_size);
    void *dst = POINTER_OFFSET(m_buffer, m_type->size() * index);
    m_type->copy_to_uninitialized(src, dst);
  }

  void copy_in__initialized(uint index, const void *src)
  {
    BLI_assert(index < m_size);
    void *dst = POINTER_OFFSET(m_buffer, m_type->size() * index);
    m_type->copy_to_initialized(src, dst);
  }

  static void RelocateUninitialized(GenericMutableArrayRef from, GenericMutableArrayRef to);

  void *operator[](uint index)
  {
    BLI_assert(index < m_size);
    return POINTER_OFFSET(m_buffer, m_type->size() * index);
  }

  template<typename T> MutableArrayRef<T> as_typed_ref()
  {
    BLI_assert(CPP_TYPE<T>() == *m_type);
    return MutableArrayRef<T>((T *)m_buffer, m_size);
  }
};

}  // namespace FN

#endif /* __FN_GENERIC_ARRAY_REF_H__ */