@@ -25,12 +25,13 @@ class IMathEngine;
25
25
class CMemoryHandleInternal ;
26
26
27
27
// Wraps the pointer to memory allocated by a math engine
28
+ // IMPORTANT: Do not use pointers to CMemoryHandle for children classes with fields, because of the non virtual dtor.
28
29
class NEOMATHENGINE_API CMemoryHandle {
29
30
public:
30
31
constexpr CMemoryHandle () = default;
31
32
// Be copied and moved by default
32
33
33
- bool operator !=( const CMemoryHandle& other ) const { return !operator ==( other ); }
34
+ bool operator !=( const CMemoryHandle& other ) const { return !( * this == other ); }
34
35
bool operator ==( const CMemoryHandle& other ) const
35
36
{ return MathEngine == other.MathEngine && Object == other.Object && Offset == other.Offset ; }
36
37
@@ -54,6 +55,7 @@ class NEOMATHENGINE_API CMemoryHandle {
54
55
// ---------------------------------------------------------------------------------------------------------------------
55
56
56
57
// Wraps the typed pointer to memory allocated by a math engine
58
+ // IMPORTANT: Do not use pointers to CMemoryHandle for children classes with fields, because of the non virtual dtor.
57
59
template <class T >
58
60
class CTypedMemoryHandle : public CMemoryHandle {
59
61
public:
@@ -130,37 +132,43 @@ class CTypedMemoryHandle : public CMemoryHandle {
130
132
// ---------------------------------------------------------------------------------------------------------------------
131
133
132
134
// CMemoryHandleVar is a variable or a fixed-size array for a math engine
135
+ // IMPORTANT: Do not use pointers to CMemoryHandleVarBase for children with fields, because of the non virtual dtor.
133
136
template <class T >
134
137
class CMemoryHandleVarBase {
135
138
public:
136
- void SetValueAt ( int index, T value );
137
- T GetValueAt ( int index ) const ;
138
- void SetValue ( T value );
139
- T GetValue () const ;
139
+ // Moveable only
140
+ CMemoryHandleVarBase ( CMemoryHandleVarBase&& other ) : Data( other.Data ), DataSize( other.DataSize )
141
+ { other.Data = CTypedMemoryHandle<T>{}; } // nullify to avoid double free
142
+ CMemoryHandleVarBase& operator =( CMemoryHandleVarBase&& other )
143
+ { if ( this != &other ) { std::swap ( *this , other ); } return *this ; }
144
+
145
+ void SetValueAt ( int index, T value ) { Data.SetValueAt ( index, value ); }
146
+ T GetValueAt ( int index ) const { return Data.GetValueAt ( index ); }
147
+ void SetValue ( T value ) { Data.SetValue ( value ); }
148
+ T GetValue () const { return Data.GetValue (); }
140
149
141
150
const CTypedMemoryHandle<T>& GetHandle () const { return Data; }
142
151
143
152
// Operators for easier use
144
- operator const CTypedMemoryHandle<T>&( ) const { return GetHandle (); }
153
+ operator const CTypedMemoryHandle<T>&() const { return GetHandle (); }
145
154
operator CTypedMemoryHandle<const T>() const { return GetHandle (); }
146
155
CTypedMemoryHandle<T> operator []( int index ) const { return GetHandle () + index; }
147
156
148
157
bool operator ==( const CTypedMemoryHandle<const T>& other ) const { return GetHandle () == other; }
149
- bool operator !=( const CTypedMemoryHandle<const T>& other ) const { return GetHandle () != other; }
158
+ bool operator !=( const CTypedMemoryHandle<const T>& other ) const { return !( * this == other ) ; }
150
159
151
160
CTypedMemoryHandle<T> operator +( ptrdiff_t shift ) const { return GetHandle () + shift; }
152
161
CTypedMemoryHandle<T> operator -( ptrdiff_t shift ) const { return GetHandle () - shift; }
153
162
int operator -( const CTypedMemoryHandle<T>& handle ) const { return GetHandle () - handle; }
154
163
155
164
int Size () const { return static_cast <int >( DataSize ); }
156
- IMathEngine* GetMathEngine () const { return &MathEngine ; }
165
+ IMathEngine* GetMathEngine () const { return Data. GetMathEngine () ; }
157
166
158
167
protected:
159
- IMathEngine& MathEngine; // the math engine owner
160
- mutable CTypedMemoryHandle<T> Data; // the typed memory handler
168
+ CTypedMemoryHandle<T> Data; // the typed memory handler
161
169
const size_t DataSize; // the typed memory size
162
170
163
- CMemoryHandleVarBase ( IMathEngine& mathEngine, size_t size ) : MathEngine( mathEngine ), DataSize( size ) {}
171
+ explicit CMemoryHandleVarBase ( size_t size ) : DataSize( size ) {}
164
172
~CMemoryHandleVarBase () = default ;
165
173
166
174
private:
@@ -171,7 +179,7 @@ class CMemoryHandleVarBase {
171
179
172
180
// ---------------------------------------------------------------------------------------------------------------------
173
181
174
- // A variable or an array
182
+ // A variable or an array on the heap
175
183
template <class T >
176
184
class CMemoryHandleVar : public CMemoryHandleVarBase <T> {
177
185
public:
0 commit comments