MeshLib
 
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1#pragma once
2
3#include "TypeCast.h"
4
5#include <MRMesh/MRVector.h>
6
9// it's your responsibility to update it by calling invalidate() after the vector's change, explicit or implicit
10template <typename T>
12{
13 using value_type = T;
14
16 size_t size;
17
18 operator const std::vector<T>&() const
19 {
20 return *vec_;
21 }
22
23 operator std::vector<T>&()
24 {
25 return *vec_;
26 }
27
29 {
30 data = vec_->data();
31 size = vec_->size();
32 }
33
34protected:
35 explicit vector_wrapper_base( std::vector<T>* vec )
36 : vec_( vec )
37 {
38 invalidate();
39 }
40
41 std::vector<T>* vec_;
42};
43
44template <typename T>
46{
48
49 explicit vector_wrapper( std::vector<T>&& vec )
50 : base( new std::vector<T>( std::move( vec ) ) )
51 { }
52
53 template <typename I>
55 : vector_wrapper( std::move( vec.vec_ ) )
56 { }
57
59 {
60 delete base::vec_;
61 }
62};
63
64template <typename T>
66{
68
69 explicit vector_ref_wrapper( const std::vector<T>& vec )
70 : base( const_cast<std::vector<T>*>( &vec ) )
71 { }
72
73 template <typename I>
74 explicit vector_ref_wrapper( const MR::Vector<T, I>& vec )
76 { }
77};
78
79#define MR_VECTOR_LIKE_IMPL( ClassName, Type ) \
80static_assert( sizeof( MR_CONCAT( MR, ClassName ) ) == sizeof( vector_ref_wrapper<Type> ) ); \
81void MR_CONCAT( MR_CONCAT( mr, ClassName ), Invalidate )( MR_CONCAT( MR, ClassName )* vec ) \
82{ \
83 reinterpret_cast<vector_ref_wrapper<Type>*>( vec )->invalidate(); \
84} \
85void MR_CONCAT( MR_CONCAT( mr, ClassName ), Free )( MR_CONCAT( MR, ClassName )* vec ) \
86{ \
87 delete reinterpret_cast<vector_wrapper<Type>*>( vec ); \
88}
89
90#define MR_VECTOR_IMPL( Type ) MR_VECTOR_LIKE_IMPL( MR_CONCAT( Vector, Type ), Type )
91
92#define VECTOR_WRAPPER( Type ) vector_wrapper<typename Type::value_type>
93#define VECTOR_REF_WRAPPER( Type ) vector_ref_wrapper<typename Type::value_type>
94
95#define REGISTER_VECTOR_LIKE( ClassName, Type ) \
96ADD_AUTO_CAST( ClassName, vector_ref_wrapper<Type> ); \
97ADD_AUTO_CAST( vector_ref_wrapper<Type>, ClassName ); \
98ADD_AUTO_CAST( vector_wrapper<Type>, ClassName );
99
100#define REGISTER_VECTOR( Type ) \
101ADD_AUTO_CAST( MR_CONCAT( MR, Type ), VECTOR_REF_WRAPPER( Type ) ); \
102ADD_AUTO_CAST( VECTOR_REF_WRAPPER( Type ), MR_CONCAT( MR, Type ) ); \
103ADD_AUTO_CAST( VECTOR_WRAPPER( Type ), MR_CONCAT( MR, Type ) );
104
105#define VECTOR( ... ) vector_ref_wrapper( __VA_ARGS__ )
106
107#define NEW_VECTOR( ... ) new vector_wrapper( __VA_ARGS__ )
108
109#define RETURN_VECTOR( ... ) return auto_cast( VECTOR( __VA_ARGS__ ) )
110
111#define RETURN_NEW_VECTOR( ... ) return auto_cast( NEW_VECTOR( __VA_ARGS__ ) )
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:19
Definition Vector.h:66
vector_ref_wrapper(const MR::Vector< T, I > &vec)
Definition Vector.h:74
vector_ref_wrapper(const std::vector< T > &vec)
Definition Vector.h:69
Definition Vector.h:12
value_type * data
Definition Vector.h:15
T value_type
Definition Vector.h:13
vector_wrapper_base(std::vector< T > *vec)
Definition Vector.h:35
std::vector< T > * vec_
Definition Vector.h:41
void invalidate()
Definition Vector.h:28
size_t size
Definition Vector.h:16
Definition Vector.h:46
vector_wrapper(std::vector< T > &&vec)
Definition Vector.h:49
vector_wrapper(MR::Vector< T, I > &&vec)
Definition Vector.h:54
~vector_wrapper()
Definition Vector.h:58