[78a794ab] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_INTERNAL_IOMANIP
|
---|
| 30 | #define LIBCPP_INTERNAL_IOMANIP
|
---|
| 31 |
|
---|
| 32 | #include <ios>
|
---|
| 33 | #include <utility>
|
---|
| 34 |
|
---|
| 35 | namespace std::aux
|
---|
| 36 | {
|
---|
| 37 | /**
|
---|
| 38 | * Note: This allows us to lower the amount
|
---|
| 39 | * of code duplication in this module
|
---|
| 40 | * as we can avoid operator<</>> overloading
|
---|
| 41 | * for the manipulators.
|
---|
| 42 | */
|
---|
| 43 | template<class Manipulator>
|
---|
| 44 | struct manip_wrapper
|
---|
| 45 | {
|
---|
| 46 | template<class... Args>
|
---|
| 47 | manip_wrapper(Args&&... args)
|
---|
| 48 | : manipulator{forward<Args>(args)...}
|
---|
| 49 | { /* DUMMY BODY */ }
|
---|
| 50 |
|
---|
| 51 | void operator()(ios_base& str)
|
---|
| 52 | {
|
---|
| 53 | manipulator(str);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | Manipulator manipulator;
|
---|
| 57 | };
|
---|
| 58 | template<class Char, class Traits, class Manipulator>
|
---|
| 59 | basic_ostream<Char, Traits>& operator<<(
|
---|
| 60 | basic_ostream<Char, Traits>& os, manip_wrapper<Manipulator> manip
|
---|
| 61 | )
|
---|
| 62 | {
|
---|
| 63 | manip(os);
|
---|
| 64 |
|
---|
| 65 | return os;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | template<class Char, class Traits, class Manipulator>
|
---|
| 69 | basic_istream<Char, Traits>& operator>>(
|
---|
| 70 | basic_istream<Char, Traits>& is, manip_wrapper<Manipulator> manip
|
---|
| 71 | )
|
---|
| 72 | {
|
---|
| 73 | manip(is);
|
---|
| 74 |
|
---|
| 75 | return is;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | struct resetiosflags_t
|
---|
| 79 | {
|
---|
| 80 | resetiosflags_t(ios_base::fmtflags m);
|
---|
| 81 |
|
---|
| 82 | void operator()(ios_base& str) const;
|
---|
| 83 |
|
---|
| 84 | ios_base::fmtflags mask;
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 | struct setiosflags_t
|
---|
| 88 | {
|
---|
| 89 | setiosflags_t(ios_base::fmtflags m);
|
---|
| 90 |
|
---|
| 91 | void operator()(ios_base& str) const;
|
---|
| 92 |
|
---|
| 93 | ios_base::fmtflags mask;
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | struct setbase_t
|
---|
| 97 | {
|
---|
| 98 | setbase_t(int b);
|
---|
| 99 |
|
---|
| 100 | void operator()(ios_base& str) const;
|
---|
| 101 |
|
---|
| 102 | int base;
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Note: setfill is only for basic_ostream so
|
---|
| 107 | * this is the only case where we overload
|
---|
| 108 | * the appropriate operator again.
|
---|
| 109 | */
|
---|
| 110 | template<class Char>
|
---|
| 111 | struct setfill_t
|
---|
| 112 | {
|
---|
| 113 | setfill_t(Char c)
|
---|
| 114 | : fill{c}
|
---|
| 115 | { /* DUMMY BODY */ }
|
---|
| 116 |
|
---|
| 117 | template<class Traits>
|
---|
| 118 | void operator()(basic_ios<Char, Traits>& str) const
|
---|
| 119 | {
|
---|
| 120 | str.fill(fill);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | Char fill;
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | template<class Char, class Traits>
|
---|
| 127 | basic_ostream<Char, Traits>& operator<<(
|
---|
| 128 | basic_ostream<Char, Traits>& is,
|
---|
| 129 | setfill_t<Char> manip
|
---|
| 130 | )
|
---|
| 131 | {
|
---|
| 132 | manip(is);
|
---|
| 133 |
|
---|
| 134 | return is;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | struct setprecision_t
|
---|
| 138 | {
|
---|
| 139 | setprecision_t(int);
|
---|
| 140 |
|
---|
| 141 | void operator()(ios_base&) const;
|
---|
| 142 |
|
---|
| 143 | int prec;
|
---|
| 144 | };
|
---|
| 145 |
|
---|
| 146 | struct setw_t
|
---|
| 147 | {
|
---|
| 148 | setw_t(int);
|
---|
| 149 |
|
---|
| 150 | void operator()(ios_base&) const;
|
---|
| 151 |
|
---|
| 152 | int width;
|
---|
| 153 | };
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | #endif
|
---|