Compare commits
2 commits
master
...
custom_sto
Author | SHA1 | Date | |
---|---|---|---|
7b3df1b14a | |||
ebfcc6f6ac |
8 changed files with 163 additions and 10 deletions
45
include/vectorwrapper/forward_wrapper.hpp
Normal file
45
include/vectorwrapper/forward_wrapper.hpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright 2015-2018 Michele "King_DuckZ" Santullo
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined VWR_OUTER_NAMESPACE
|
||||
namespace VWR_OUTER_NAMESPACE {
|
||||
#endif
|
||||
|
||||
namespace vwr {
|
||||
template <typename V, typename Storage>
|
||||
class forward_wrapper {
|
||||
public:
|
||||
forward_wrapper() { new(&m_storage) V; }
|
||||
forward_wrapper (const forward_wrapper&) = delete;
|
||||
forward_wrapper (forward_wrapper&&) = delete;
|
||||
~forward_wrapper() { vector().~V(); }
|
||||
|
||||
forward_wrapper& operator= (const forward_wrapper&) = delete;
|
||||
forward_wrapper& operator= (forward_wrapper&&) = delete;
|
||||
|
||||
private:
|
||||
V& vector() { return *reinterpret_cast<V*>(&m_storage); }
|
||||
//const V& vector() const { return *reinterpret_cast<const V*>(&m_storage); }
|
||||
|
||||
Storage m_storage;
|
||||
};
|
||||
} //namespace vwr
|
||||
|
||||
#if defined VWR_OUTER_NAMESPACE
|
||||
} //namespace VWR_OUTER_NAMESPACE
|
||||
#endif
|
|
@ -60,6 +60,8 @@ namespace vwr {
|
|||
friend Vec<V>& assign_same_type<V> ( Vec<V>& parLeft, const Vec<V>& parRight );
|
||||
public:
|
||||
typedef V vector_type;
|
||||
typedef typename get_storage_type<V>::type storage_type;
|
||||
|
||||
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
|
||||
|
||||
enum {
|
||||
|
@ -77,8 +79,8 @@ namespace vwr {
|
|||
scalar_type& operator[] ( size_type parIndex );
|
||||
const scalar_type& operator[] ( size_type parIndex ) const;
|
||||
|
||||
vector_type& data ( void ) { return m_wrapped; }
|
||||
const vector_type& data ( void ) const { return m_wrapped; }
|
||||
storage_type& data ( void ) { return m_wrapped; }
|
||||
const storage_type& data ( void ) const { return m_wrapped; }
|
||||
|
||||
template <typename V2>
|
||||
const typename std::enable_if<is_vec<V2>::value and directly_convertible<V, V2>::value, V2>::type& cast ( void ) const;
|
||||
|
@ -107,7 +109,7 @@ namespace vwr {
|
|||
template <typename Op, size_type... I>
|
||||
void assign_values_op_scalar (Op parOp, const bt::number_seq<size_type, I...>& parSeq, const scalar_type& parOther);
|
||||
|
||||
vector_type m_wrapped;
|
||||
storage_type m_wrapped;
|
||||
};
|
||||
|
||||
template <
|
||||
|
|
|
@ -78,7 +78,7 @@ namespace vwr {
|
|||
|
||||
template <typename V>
|
||||
auto VecBase<V>::operator[] (size_type parIndex) const -> const scalar_type& {
|
||||
return VecGetter<V>::get_at(const_cast<vector_type&>(m_wrapped), parIndex);
|
||||
return VecGetter<V>::get_at(const_cast<storage_type&>(m_wrapped), parIndex);
|
||||
}
|
||||
|
||||
template <typename V>
|
||||
|
|
|
@ -34,30 +34,41 @@ namespace vwr {
|
|||
namespace implem {
|
||||
define_has_typedef(lower_vector_type, LowerVec);
|
||||
define_has_typedef(higher_vector_type, HigherVec);
|
||||
define_has_typedef(storage_type, StorageType);
|
||||
define_has_enum(offset_x, OffsetX);
|
||||
define_has_method(get_at, GetAt);
|
||||
define_has_enum(cast_ignore_trailing_properties, CastIgnoreTrailingProperties);
|
||||
|
||||
template <typename V, bool UserDefined=HasStorageTypeTypedef<VectorWrapperInfo<V>>::value> struct get_storage_type;
|
||||
template <typename V> struct get_storage_type<V, true> {
|
||||
typedef typename VectorWrapperInfo<V>::storage_type type;
|
||||
static_assert(alignof(type) >= alignof(V), "Requested storage type's alignment is not compatible with vector_type");
|
||||
};
|
||||
template <typename V> struct get_storage_type<V, false> {
|
||||
typedef V type;
|
||||
};
|
||||
|
||||
template <typename T, bool=HasOffsetXEnum<VectorWrapperInfo<T>>::value and std::is_standard_layout<T>::value>
|
||||
struct VecGetter;
|
||||
template <typename T>
|
||||
struct VecGetter<T, true> {
|
||||
static typename VectorWrapperInfo<T>::scalar_type& get_at ( T& parVec, size_type parIndex );
|
||||
typedef typename get_storage_type<T>::type storage_type;
|
||||
static typename VectorWrapperInfo<T>::scalar_type& get_at ( storage_type& parVec, size_type parIndex );
|
||||
};
|
||||
template <typename T>
|
||||
struct VecGetter<T, false> {
|
||||
typedef typename get_storage_type<T>::type storage_type;
|
||||
private:
|
||||
static_assert(HasGetAtMethod<VectorWrapperInfo<T>>::value, "You must provide a get_at() static method for this vector_type");
|
||||
typedef typename VectorWrapperInfo<T>::scalar_type scalar_type;
|
||||
typedef T vector_type;
|
||||
using get_at_func = decltype(&VectorWrapperInfo<T>::get_at)(size_type, vector_type&);
|
||||
using get_at_func = decltype(&VectorWrapperInfo<T>::get_at)(size_type, storage_type&);
|
||||
using get_at_rettype = typename std::result_of<get_at_func>::type;
|
||||
|
||||
static_assert(not std::is_rvalue_reference<get_at_rettype>::value, "rvalue ref return types not implemented");
|
||||
static_assert(std::is_lvalue_reference<get_at_rettype>::value, "Read-only vectors not implemented");
|
||||
|
||||
public:
|
||||
static get_at_rettype get_at ( T& parVec, size_type parIndex );
|
||||
static get_at_rettype get_at ( storage_type& parVec, size_type parIndex );
|
||||
};
|
||||
|
||||
template <typename T, size_type I> struct get_offset_enum_from_index;
|
||||
|
|
|
@ -21,7 +21,7 @@ namespace VWR_OUTER_NAMESPACE {
|
|||
namespace vwr {
|
||||
namespace implem {
|
||||
template <typename T>
|
||||
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, true>::get_at (T& parVec, size_type parIndex) {
|
||||
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, true>::get_at (storage_type& parVec, size_type parIndex) {
|
||||
assert(parIndex < VectorWrapperInfo<T>::dimensions);
|
||||
typedef T vector_type;
|
||||
typedef typename VectorWrapperInfo<T>::scalar_type scalar_type;
|
||||
|
@ -31,7 +31,7 @@ namespace vwr {
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
auto VecGetter<T, false>::get_at (T& parVec, size_type parIndex) -> get_at_rettype {
|
||||
auto VecGetter<T, false>::get_at (storage_type& parVec, size_type parIndex) -> get_at_rettype {
|
||||
assert(parIndex < VectorWrapperInfo<T>::dimensions);
|
||||
return VectorWrapperInfo<T>::get_at(parIndex, parVec);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ add_executable(${PROJECT_NAME}
|
|||
test_sequence_range.cpp
|
||||
test_offset_getters.cpp
|
||||
test_custom_type.cpp
|
||||
test_custom_storage.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
|
53
test/unit/test_custom_storage.cpp
Normal file
53
test/unit/test_custom_storage.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "vectorwrapper/vectorwrapper.hpp"
|
||||
#include "vectorwrapper/forward_wrapper.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace {
|
||||
struct VecType {
|
||||
int a, b, c;
|
||||
};
|
||||
} //unnamed namespace
|
||||
|
||||
namespace vwr {
|
||||
template <>
|
||||
struct VectorWrapperInfo<VecType> {
|
||||
enum { dimensions = 3};
|
||||
typedef int scalar_type;
|
||||
typedef int storage_type[3];
|
||||
enum {
|
||||
offset_x = offsetof(VecType, a),
|
||||
offset_y = offsetof(VecType, b),
|
||||
offset_z = offsetof(VecType, c)
|
||||
};
|
||||
};
|
||||
} //namespace vwr
|
||||
|
||||
namespace {
|
||||
typedef vwr::Vec<VecType> ivec3;
|
||||
} //unnamed namespace
|
||||
|
||||
TEST(vwr, custom_storage_type) {
|
||||
ivec3 v(55);
|
||||
|
||||
EXPECT_EQ(v.x(), 55);
|
||||
EXPECT_EQ(v.y(), 55);
|
||||
EXPECT_EQ(v.z(), 55);
|
||||
|
||||
auto& data = v.data();
|
||||
}
|
41
testme.cpp
Normal file
41
testme.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
int get_at (const T&, int);
|
||||
|
||||
struct XY {
|
||||
int x, y;
|
||||
};
|
||||
|
||||
template <> int get_at(const int(&in)[2], int i) { return in[i]; }
|
||||
template <> int get_at(const XY& in, int i) { return (i ? in.y : in.x); }
|
||||
|
||||
template <typename T>
|
||||
struct Vec {
|
||||
int x() const { return get_at(m_storage_type, 0); }
|
||||
int y() const { return get_at(m_storage_type, 1); }
|
||||
|
||||
T m_storage_type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<< (std::ostream& os, const Vec<T>& v) {
|
||||
os << '<' << v.x() << ',' << v.y() << '>';
|
||||
return os;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Vec<int[2]> v1;
|
||||
Vec<XY> v2;
|
||||
|
||||
v1.m_storage_type[0] = 1234;
|
||||
v1.m_storage_type[1] = 7777;
|
||||
|
||||
v2.m_storage_type.x = 44;
|
||||
v2.m_storage_type.y = 9999;
|
||||
|
||||
std::cout << v1 << '\n';
|
||||
std::cout << v2 << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue