schema_editor/comdel/parser/poly.h

401 lines
9.4 KiB
C
Raw Permalink Normal View History

2022-03-29 19:31:45 +00:00
/*
Copyright (c) 2016 Jonathan B. Coe
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef POLY_H
#define POLY_H
#include <cassert>
#include <exception>
#include <memory>
#include <type_traits>
#include <typeinfo>
2022-05-27 06:18:17 +00:00
namespace detail {
2022-03-29 19:31:45 +00:00
////////////////////////////////////////////////////////////////////////////
// Implementation detail classes
////////////////////////////////////////////////////////////////////////////
2022-05-27 06:18:17 +00:00
template<class T>
struct default_copy {
T *operator()(const T &t) const {
2022-03-29 19:31:45 +00:00
return new T(t);
}
};
2022-05-27 06:18:17 +00:00
template<class T>
struct default_delete {
void operator()(const T *t) const {
2022-03-29 19:31:45 +00:00
delete t;
}
};
2022-05-27 06:18:17 +00:00
template<class T>
struct control_block {
2022-03-29 19:31:45 +00:00
virtual ~control_block() = default;
virtual std::unique_ptr<control_block> clone() const = 0;
2022-05-27 06:18:17 +00:00
virtual T *ptr() = 0;
2022-03-29 19:31:45 +00:00
};
2022-05-27 06:18:17 +00:00
template<class T, class U = T>
class direct_control_block : public control_block<T> {
2022-03-29 19:31:45 +00:00
static_assert(!std::is_reference<U>::value, "");
U u_;
public:
2022-05-27 06:18:17 +00:00
template<class... Ts>
explicit direct_control_block(Ts &&... ts) : u_(U(std::forward<Ts>(ts)...)) {
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
std::unique_ptr<control_block<T>> clone() const override {
2022-03-29 19:31:45 +00:00
return std::make_unique<direct_control_block>(*this);
}
2022-05-27 06:18:17 +00:00
T *ptr() override {
2022-03-29 19:31:45 +00:00
return std::addressof(u_);
}
};
2022-05-27 06:18:17 +00:00
template<class T, class U, class C = default_copy<U>,
class D = default_delete<U>>
class pointer_control_block : public control_block<T>, public C {
2022-03-29 19:31:45 +00:00
std::unique_ptr<U, D> p_;
public:
2022-05-27 06:18:17 +00:00
explicit pointer_control_block(U *u, C c = C{}, D d = D{})
: C(std::move(c)), p_(u, std::move(d)) {
2022-03-29 19:31:45 +00:00
}
explicit pointer_control_block(std::unique_ptr<U, D> p, C c = C{})
2022-05-27 06:18:17 +00:00
: C(std::move(c)), p_(std::move(p)) {
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
std::unique_ptr<control_block<T>> clone() const override {
2022-03-29 19:31:45 +00:00
assert(p_);
return std::make_unique<pointer_control_block>(
2022-05-27 06:18:17 +00:00
C::operator()(*p_), static_cast<const C &>(*this), p_.get_deleter());
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
T *ptr() override {
2022-03-29 19:31:45 +00:00
return p_.get();
}
};
2022-05-27 06:18:17 +00:00
template<class T, class U>
class delegating_control_block : public control_block<T> {
2022-03-29 19:31:45 +00:00
std::unique_ptr<control_block<U>> delegate_;
public:
explicit delegating_control_block(std::unique_ptr<control_block<U>> b)
2022-05-27 06:18:17 +00:00
: delegate_(std::move(b)) {
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
std::unique_ptr<control_block<T>> clone() const override {
2022-03-29 19:31:45 +00:00
return std::make_unique<delegating_control_block>(delegate_->clone());
}
2022-05-27 06:18:17 +00:00
T *ptr() override {
return static_cast<T *>(delegate_->ptr());
2022-03-29 19:31:45 +00:00
}
};
} // end namespace detail
2022-05-27 06:18:17 +00:00
class bad_poly_construction : std::exception {
2022-03-29 19:31:45 +00:00
public:
bad_poly_construction() noexcept = default;
2022-05-27 06:18:17 +00:00
const char *what() const noexcept override {
2022-03-29 19:31:45 +00:00
return "Dynamic and static type mismatch in poly "
2022-05-27 06:18:17 +00:00
"construction";
2022-03-29 19:31:45 +00:00
}
};
2022-05-27 06:18:17 +00:00
template<class T>
2022-03-29 19:31:45 +00:00
class poly;
2022-05-27 06:18:17 +00:00
template<class T>
struct is_poly : std::false_type {
2022-03-29 19:31:45 +00:00
};
2022-05-27 06:18:17 +00:00
template<class T>
struct is_poly<poly<T>> : std::true_type {
2022-03-29 19:31:45 +00:00
};
////////////////////////////////////////////////////////////////////////////////
// `poly` class definition
////////////////////////////////////////////////////////////////////////////////
2022-05-27 06:18:17 +00:00
template<class T>
class poly {
2022-03-29 19:31:45 +00:00
static_assert(!std::is_union<T>::value, "");
static_assert(std::is_class<T>::value, "");
2022-05-27 06:18:17 +00:00
template<class U>
friend
class poly;
2022-03-29 19:31:45 +00:00
2022-05-27 06:18:17 +00:00
template<class T_, class U, class... Ts>
friend poly<T_> make_poly(Ts &&... ts);
template<class T_, class... Ts>
friend poly<T_> make_poly(Ts &&... ts);
template<class T_, class U>
2022-03-29 19:31:45 +00:00
friend poly<T_> poly_cast(poly<U> p);
2022-05-27 06:18:17 +00:00
T *ptr_ = nullptr;
2022-03-29 19:31:45 +00:00
std::unique_ptr<detail::control_block<T>> cb_;
public:
//
// Destructor
//
~poly() = default;
//
// Constructors
//
2022-05-27 06:18:17 +00:00
poly() {
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
template<class U, class C = detail::default_copy<U>,
class D = detail::default_delete<U>,
class V = std::enable_if_t<std::is_convertible<U *, T *>::value>>
explicit poly(U *u, C copier = C{}, D deleter = D{}) {
if (!u) {
2022-03-29 19:31:45 +00:00
return;
}
if (std::is_same<D, detail::default_delete<U>>::value &&
std::is_same<C, detail::default_copy<U>>::value &&
typeid(*u) != typeid(U))
throw bad_poly_construction();
std::unique_ptr<U, D> p(u, std::move(deleter));
cb_ = std::make_unique<detail::pointer_control_block<T, U, C, D>>(
2022-05-27 06:18:17 +00:00
std::move(p), std::move(copier));
2022-03-29 19:31:45 +00:00
ptr_ = u;
}
//
// Copy-constructors
//
2022-05-27 06:18:17 +00:00
poly(const poly &p) {
if (!p) {
2022-03-29 19:31:45 +00:00
return;
}
auto tmp_cb = p.cb_->clone();
ptr_ = tmp_cb->ptr();
cb_ = std::move(tmp_cb);
}
//
// Move-constructors
//
2022-05-27 06:18:17 +00:00
poly(poly &&p) noexcept {
2022-03-29 19:31:45 +00:00
ptr_ = p.ptr_;
cb_ = std::move(p.cb_);
p.ptr_ = nullptr;
}
//
// Converting constructors
//
2022-05-27 06:18:17 +00:00
template<class U,
class V = std::enable_if_t<!std::is_same<T, U>::value &&
std::is_convertible<U *, T *>::value>>
poly(const poly<U> &p) {
2022-03-29 19:31:45 +00:00
poly<U> tmp(p);
ptr_ = tmp.ptr_;
cb_ = std::make_unique<detail::delegating_control_block<T, U>>(
2022-05-27 06:18:17 +00:00
std::move(tmp.cb_));
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
template<class U,
class V = std::enable_if_t<!std::is_same<T, U>::value &&
std::is_convertible<U *, T *>::value>>
poly(poly<U> &&p) {
2022-03-29 19:31:45 +00:00
ptr_ = p.ptr_;
cb_ = std::make_unique<detail::delegating_control_block<T, U>>(
2022-05-27 06:18:17 +00:00
std::move(p.cb_));
2022-03-29 19:31:45 +00:00
p.ptr_ = nullptr;
}
//
// Forwarding constructor
//
2022-05-27 06:18:17 +00:00
template<class U, class V = std::enable_if_t<
std::is_convertible<std::decay_t<U> *, T *>::value &&
!is_poly<std::decay_t<U>>::value>>
poly(U &&u)
: cb_(std::make_unique<
detail::direct_control_block<T, std::decay_t<U>>>(
std::forward<U>(u))) {
2022-03-29 19:31:45 +00:00
ptr_ = cb_->ptr();
}
//
// Assignment
//
2022-05-27 06:18:17 +00:00
poly &operator=(const poly &p) {
if (std::addressof(p) == this) {
2022-03-29 19:31:45 +00:00
return *this;
}
2022-05-27 06:18:17 +00:00
if (!p) {
2022-03-29 19:31:45 +00:00
cb_.reset();
ptr_ = nullptr;
return *this;
}
auto tmp_cb = p.cb_->clone();
ptr_ = tmp_cb->ptr();
cb_ = std::move(tmp_cb);
return *this;
}
//
// Move-assignment
//
2022-05-27 06:18:17 +00:00
poly &operator=(poly &&p) noexcept {
if (std::addressof(p) == this) {
2022-03-29 19:31:45 +00:00
return *this;
}
cb_ = std::move(p.cb_);
ptr_ = p.ptr_;
p.ptr_ = nullptr;
return *this;
}
//
// Modifiers
//
2022-05-27 06:18:17 +00:00
void swap(poly &p) noexcept {
2022-03-29 19:31:45 +00:00
using std::swap;
swap(ptr_, p.ptr_);
swap(cb_, p.cb_);
}
//
// Observers
//
2022-05-27 06:18:17 +00:00
explicit operator bool() const {
return (bool) cb_;
2022-03-29 19:31:45 +00:00
}
2022-05-27 06:18:17 +00:00
const T *operator->() const {
2022-03-29 19:31:45 +00:00
assert(ptr_);
return ptr_;
}
2022-05-27 06:18:17 +00:00
const T &operator*() const {
2022-03-29 19:31:45 +00:00
assert(*this);
return *ptr_;
}
2022-05-27 06:18:17 +00:00
T *operator->() {
2022-03-29 19:31:45 +00:00
assert(*this);
return ptr_;
}
2022-05-27 06:18:17 +00:00
T &operator*() {
2022-03-29 19:31:45 +00:00
assert(*this);
return *ptr_;
}
};
//
// poly creation
//
2022-05-27 06:18:17 +00:00
template<class T, class... Ts>
poly<T> make_poly(Ts &&... ts) {
2022-03-29 19:31:45 +00:00
poly<T> p;
p.cb_ = std::make_unique<detail::direct_control_block<T, T>>(
std::forward<Ts>(ts)...);
p.ptr_ = p.cb_->ptr();
return std::move(p);
}
2022-05-27 06:18:17 +00:00
template<class T, class U, class... Ts>
poly<T> make_poly(Ts &&... ts) {
2022-03-29 19:31:45 +00:00
poly<T> p;
p.cb_ = std::make_unique<detail::direct_control_block<T, U>>(
std::forward<Ts>(ts)...);
p.ptr_ = p.cb_->ptr();
return std::move(p);
}
2022-05-27 06:18:17 +00:00
template<class T, class U>
const T *poly_view(const poly<U> &p) {
2022-03-29 19:31:45 +00:00
if (p) {
2022-05-27 06:18:17 +00:00
return dynamic_cast<const T *>(&*p);
} else {
2022-03-29 19:31:45 +00:00
return nullptr;
}
}
2022-05-27 06:18:17 +00:00
template<class T, class U>
T *poly_view(poly<U> &p) {
2022-03-29 19:31:45 +00:00
if (p) {
2022-05-27 06:18:17 +00:00
return dynamic_cast<T *>(&*p);
} else {
2022-03-29 19:31:45 +00:00
return nullptr;
}
}
2022-05-27 06:18:17 +00:00
template<class T, class U>
2022-03-29 19:31:45 +00:00
poly<T> poly_cast(poly<U> p) {
poly<T> ret;
2022-05-27 06:18:17 +00:00
if (T *ptr = dynamic_cast<T *>(&*p)) {
2022-03-29 19:31:45 +00:00
ret.cb_ = std::make_unique<detail::delegating_control_block<T, U>>(
2022-05-27 06:18:17 +00:00
std::move(p.cb_));
2022-03-29 19:31:45 +00:00
ret.ptr_ = ret.cb_->ptr();
}
return ret;
}
#endif // POLY_H