The usual commit from 1 year later with changes that I don't recognise
This commit is contained in:
parent
ebfcc6f6ac
commit
7b3df1b14a
3 changed files with 87 additions and 0 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
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
|
||||
#include "vectorwrapper/vectorwrapper.hpp"
|
||||
#include "vectorwrapper/forward_wrapper.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace {
|
||||
|
|
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