// vi:set ft=cpp: -*- Mode: C++ -*- /* SPDX-License-Identifier: GPL-2.0-only or License-Ref-kk-custom */ /* * Copyright (C) 2012-2013 Technische Universität Dresden. * Copyright (C) 2016-2017 Kernkonzept GmbH. */ #pragma once #include #include namespace cxx { template< typename T > class Static_container { private: struct X : T { void *operator new (size_t, void *p) throw() { return p; } void operator delete (void *) {} X() = default; template X(Args && ...a) : T(cxx::forward(a)...) {} }; public: void operator = (Static_container const &) = delete; Static_container(Static_container const &) = delete; Static_container() = default; T *get() { return reinterpret_cast(_s); } T *operator -> () { return get(); } T &operator * () { return *get(); } operator T* () { return get(); } void construct() { new (reinterpret_cast(_s)) X; } template< typename ...Args > void construct(Args && ...args) { new (reinterpret_cast(_s)) X(cxx::forward(args)...); } private: char _s[sizeof(X)] __attribute__((aligned(__alignof(X)))); }; }