From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/lib/misc/xalloc.hxx | 135 +++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 tiger-compiler/lib/misc/xalloc.hxx (limited to 'tiger-compiler/lib/misc/xalloc.hxx') diff --git a/tiger-compiler/lib/misc/xalloc.hxx b/tiger-compiler/lib/misc/xalloc.hxx new file mode 100644 index 0000000..8ae692a --- /dev/null +++ b/tiger-compiler/lib/misc/xalloc.hxx @@ -0,0 +1,135 @@ +/** + ** \file misc/xalloc.hxx + ** \brief Implementation for misc/xalloc.hh. + */ + +#pragma once + +#include + +#include + +namespace misc +{ + /*----------------. + | iomanipulator. | + `----------------*/ + + inline std::ostream& operator<<(std::ostream& o, const iomanipulator& g) + { + g(o); + return o; + } + + /*---------------------. + | xalloc. | + `---------------------*/ + + template + template + xalloc::xalloc(Args&&... args) + : index_(std::ios::xalloc()) + , model_{std::forward(args)...} + {} + + template long int xalloc::index() const + { + return index_; + } + + template + StoredType& xalloc::operator()(std::ostream& ostr) const + { + void*& storage_ptr = ostr.pword(index_); + + if (!storage_ptr) + { + storage_ptr = new StoredType{model_}; + ostr.register_callback(xalloc::deallocate, index_); + } + + return *static_cast(storage_ptr); + } + + template + void xalloc::deallocate(std::ios_base::event type, + std::ios_base& ios, + int index) + { + if (type == std::ios_base::erase_event) + { + StoredType* ptr = static_cast(ios.pword(index)); + delete ptr; + } + } + + /*-----------. + | set_type. | + `-----------*/ + + template + xalloc::set_type::set_type(const xalloc& slot, StoredType& data) + : slot_(slot) + , data_(data) + {} + + template + void xalloc::set_type::operator()(std::ostream& ostr) const + { + slot_(ostr) = data_; + } + + template + typename xalloc::set_type + xalloc::set(StoredType& data) const + { + return set_type(*this, data); + } + + /*-----------. + | get_type. | + `-----------*/ + + template + xalloc::get_type::get_type(const xalloc& slot, StoredType& data) + : slot_(slot) + , data_(data) + {} + + template + void xalloc::get_type::operator()(std::ostream& ostr) const + { + data_ = slot_(ostr); + } + + template + typename xalloc::get_type + xalloc::get(StoredType& data) const + { + return get_type(*this, data); + } + + /*------------. + | swap_type. | + `------------*/ + + template + xalloc::swap_type::swap_type(const xalloc& slot, StoredType& data) + : slot_(slot) + , data_(data) + {} + + template + void xalloc::swap_type::operator()(std::ostream& ostr) const + { + std::swap(slot_(ostr), data_); + } + + template + typename xalloc::swap_type + xalloc::swap(StoredType& data) const + { + return swap_type(*this, data); + } + +} // namespace misc -- cgit v1.2.3