|
| 1 | +// Copyright (c) 2024-2026 Jakub Musiał |
| 2 | +// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl). |
| 3 | +// Licensed under the MIT License. See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "gl/attributes/force_inline.hpp" |
| 8 | +#include "gl/traits.hpp" |
| 9 | + |
| 10 | +#include <iostream> |
| 11 | +#include <utility> |
| 12 | + |
| 13 | +namespace gl::io { |
| 14 | + |
| 15 | +using index_type = int; |
| 16 | +using iword_type = long; |
| 17 | +using bit_position_type = unsigned; |
| 18 | + |
| 19 | +inline constexpr iword_type iword_bit = 1ul; |
| 20 | + |
| 21 | +class options_manip { |
| 22 | +public: |
| 23 | + options_manip() = delete; |
| 24 | + |
| 25 | + constexpr explicit options_manip(iword_type set_mask, iword_type clear_mask = 0ul) |
| 26 | + : _set_mask(set_mask), _clear_mask(clear_mask) {} |
| 27 | + |
| 28 | + friend std::ostream& operator<<(std::ostream& os, const options_manip& manip) { |
| 29 | + os.iword(_iostream_property_map_index()) &= ~manip._clear_mask; |
| 30 | + os.iword(_iostream_property_map_index()) |= manip._set_mask; |
| 31 | + return os; |
| 32 | + } |
| 33 | + |
| 34 | + friend std::istream& operator>>(std::istream& is, const options_manip& manip) { |
| 35 | + is.iword(_iostream_property_map_index()) &= ~manip._clear_mask; |
| 36 | + is.iword(_iostream_property_map_index()) |= manip._set_mask; |
| 37 | + return is; |
| 38 | + } |
| 39 | + |
| 40 | + [[nodiscard]] gl_attr_force_inline static bool is_option_set( |
| 41 | + std::ios_base& stream, bit_position_type bit_position |
| 42 | + ) { |
| 43 | + return (stream.iword(_iostream_property_map_index()) & (iword_bit << bit_position)) != 0; |
| 44 | + } |
| 45 | + |
| 46 | + [[nodiscard]] gl_attr_force_inline static bool is_option_set( |
| 47 | + std::ios_base& stream, traits::c_enum auto bit |
| 48 | + ) { |
| 49 | + return is_option_set(stream, static_cast<bit_position_type>(bit)); |
| 50 | + } |
| 51 | + |
| 52 | + [[nodiscard]] gl_attr_force_inline static bool are_options_set( |
| 53 | + std::ios_base& stream, iword_type bitmask |
| 54 | + ) { |
| 55 | + return (stream.iword(_iostream_property_map_index()) & bitmask) == bitmask; |
| 56 | + } |
| 57 | + |
| 58 | +private: |
| 59 | + [[nodiscard]] gl_attr_force_inline static index_type _iostream_property_map_index() { |
| 60 | + static index_type index = std::ios_base::xalloc(); |
| 61 | + return index; |
| 62 | + } |
| 63 | + |
| 64 | + iword_type _set_mask; |
| 65 | + iword_type _clear_mask; |
| 66 | +}; |
| 67 | + |
| 68 | +namespace detail { |
| 69 | + |
| 70 | +// clang-format off |
| 71 | + |
| 72 | +template <typename... Args> |
| 73 | +[[nodiscard]] constexpr iword_type build_mask(Args... bits) { |
| 74 | + return ((iword_bit << static_cast<bit_position_type>(bits)) | ...); |
| 75 | +} |
| 76 | + |
| 77 | +// clang-format on |
| 78 | + |
| 79 | +template <typename T> |
| 80 | +[[nodiscard]] constexpr iword_type build_mask_from(std::initializer_list<T> bits) { |
| 81 | + iword_type mask = 0ul; |
| 82 | + for (auto b : bits) |
| 83 | + mask |= iword_bit << static_cast<bit_position_type>(b); |
| 84 | + return mask; |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace detail |
| 88 | + |
| 89 | +template <typename... Args> |
| 90 | +[[nodiscard]] constexpr options_manip set_options(Args... bits) { |
| 91 | + return options_manip{detail::build_mask(bits...), 0ul}; |
| 92 | +} |
| 93 | + |
| 94 | +template <typename T> |
| 95 | +[[nodiscard]] constexpr options_manip set_options(std::initializer_list<T> bits) { |
| 96 | + return options_manip{detail::build_mask_from(bits), 0ul}; |
| 97 | +} |
| 98 | + |
| 99 | +template <typename... Args> |
| 100 | +[[nodiscard]] constexpr options_manip clear_options(Args... bits) { |
| 101 | + return options_manip{0ul, detail::build_mask(bits...)}; |
| 102 | +} |
| 103 | + |
| 104 | +template <typename T> |
| 105 | +[[nodiscard]] constexpr options_manip clear_options(std::initializer_list<T> bits) { |
| 106 | + return options_manip{0ul, detail::build_mask_from(bits)}; |
| 107 | +} |
| 108 | + |
| 109 | +[[nodiscard]] gl_attr_force_inline bool is_option_set( |
| 110 | + std::ios_base& stream, bit_position_type bit_position |
| 111 | +) { |
| 112 | + return options_manip::is_option_set(stream, bit_position); |
| 113 | +} |
| 114 | + |
| 115 | +[[nodiscard]] gl_attr_force_inline bool is_option_set( |
| 116 | + std::ios_base& stream, traits::c_enum auto bit |
| 117 | +) { |
| 118 | + return options_manip::is_option_set(stream, bit); |
| 119 | +} |
| 120 | + |
| 121 | +[[nodiscard]] gl_attr_force_inline bool are_options_set(std::ios_base& stream, iword_type bitmask) { |
| 122 | + return options_manip::are_options_set(stream, bitmask); |
| 123 | +} |
| 124 | + |
| 125 | +template <typename... Args> |
| 126 | +[[nodiscard]] gl_attr_force_inline bool are_options_set(std::ios_base& stream, Args... bits) { |
| 127 | + return options_manip::are_options_set(stream, detail::build_mask(bits...)); |
| 128 | +} |
| 129 | + |
| 130 | +template <typename T> |
| 131 | +[[nodiscard]] gl_attr_force_inline bool are_options_set( |
| 132 | + std::ios_base& stream, std::initializer_list<T> bits |
| 133 | +) { |
| 134 | + return options_manip::are_options_set(stream, detail::build_mask_from(bits)); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace gl::io |
0 commit comments