diff --git a/include/vectorwrapper/forward_wrapper.hpp b/include/vectorwrapper/forward_wrapper.hpp new file mode 100644 index 0000000..e30acab --- /dev/null +++ b/include/vectorwrapper/forward_wrapper.hpp @@ -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 + 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(&m_storage); } + //const V& vector() const { return *reinterpret_cast(&m_storage); } + + Storage m_storage; + }; +} //namespace vwr + +#if defined VWR_OUTER_NAMESPACE +} //namespace VWR_OUTER_NAMESPACE +#endif diff --git a/test/unit/test_custom_storage.cpp b/test/unit/test_custom_storage.cpp index 5cee311..ef30100 100644 --- a/test/unit/test_custom_storage.cpp +++ b/test/unit/test_custom_storage.cpp @@ -15,6 +15,7 @@ */ #include "vectorwrapper/vectorwrapper.hpp" +#include "vectorwrapper/forward_wrapper.hpp" #include namespace { diff --git a/testme.cpp b/testme.cpp new file mode 100644 index 0000000..673feea --- /dev/null +++ b/testme.cpp @@ -0,0 +1,41 @@ +#include + +template +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 +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 +std::ostream& operator<< (std::ostream& os, const Vec& v) { + os << '<' << v.x() << ',' << v.y() << '>'; + return os; +} + +int main() { + Vec v1; + Vec 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; +}