From 7f19d641647ac4ef313ed88d6b5c140983ce5436 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 15 Jul 2020 08:20:18 +0100 Subject: Squashed 'third_party/immer/' content from commit ad3e3556d git-subtree-dir: third_party/immer git-subtree-split: ad3e3556d38bb75966dd24c61a774970a7c7957e --- extra/guile/scm/type.hpp | 153 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 extra/guile/scm/type.hpp (limited to 'extra/guile/scm/type.hpp') diff --git a/extra/guile/scm/type.hpp b/extra/guile/scm/type.hpp new file mode 100644 index 000000000000..da53ed46ef8b --- /dev/null +++ b/extra/guile/scm/type.hpp @@ -0,0 +1,153 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#pragma once + +#include +#include +#include + +namespace scm { +namespace detail { + +template +struct foreign_type_storage +{ + static SCM data; +}; + +template +SCM foreign_type_storage::data = SCM_UNSPECIFIED; + +template +struct convert_foreign_type +{ + using storage_t = foreign_type_storage; + static T& to_cpp(SCM v) + { + assert(storage_t::data != SCM_UNSPECIFIED && + "can not convert to undefined type"); + scm_assert_foreign_object_type(storage_t::data, v); + return *(T*)scm_foreign_object_ref(v, 0); + } + + template + static SCM to_scm(U&& v) + { + assert(storage_t::data != SCM_UNSPECIFIED && + "can not convert from undefined type"); + return scm_make_foreign_object_1( + storage_t::data, + new (scm_gc_malloc(sizeof(T), "scmpp")) T( + std::forward(v))); + } +}; + +// Assume that every other type is foreign +template +struct convert::value && + // only value types are supported at + // the moment but the story might + // change later... + !std::is_pointer::value>> + : convert_foreign_type +{ +}; + +template +struct type_definer : move_sequence +{ + using this_t = type_definer; + using next_t = type_definer; + + std::string type_name_ = {}; + scm_t_struct_finalize finalizer_ = nullptr; + + type_definer(type_definer&&) = default; + + type_definer(std::string type_name) + : type_name_(std::move(type_name)) + {} + + ~type_definer() + { + if (!moved_from_) { + using storage_t = detail::foreign_type_storage; + assert(storage_t::data == SCM_UNSPECIFIED); + storage_t::data = scm_make_foreign_object_type( + scm_from_utf8_symbol(("<" + type_name_ + ">").c_str()), + scm_list_1(scm_from_utf8_symbol("data")), + finalizer_); + } + } + + template > + type_definer(type_definer r) + : move_sequence{std::move(r)} + , type_name_{std::move(r.type_name_)} + , finalizer_{std::move(r.finalizer_)} + {} + + next_t constructor() && + { + define_impl(type_name_, [] { return T{}; }); + return { std::move(*this) }; + } + + template + next_t constructor(Fn fn) && + { + define_impl(type_name_, fn); + return { std::move(*this) }; + } + + next_t finalizer() && + { + finalizer_ = (scm_t_struct_finalize) +finalizer_wrapper( + [] (T& x) { x.~T(); }); + return { std::move(*this) }; + } + + template + next_t finalizer(Fn fn) && + { + finalizer_ = (scm_t_struct_finalize) +finalizer_wrapper(fn); + return { std::move(*this) }; + } + + next_t maker() && + { + define_impl("make-" + type_name_, [] { return T{}; }); + return { std::move(*this) }; + } + + template + next_t maker(Fn fn) && + { + define_impl("make-" + type_name_, fn); + return { std::move(*this) }; + } + + template + next_t define(std::string name, Fn fn) && + { + define_impl(type_name_ + "-" + name, fn); + return { std::move(*this) }; + } +}; + +} // namespace detail + +template +detail::type_definer type(std::string type_name) +{ + return { type_name }; +} + +} // namespace scm -- cgit 1.4.1