/** ** \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