Changeset 91ba048 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:19Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
21a7ebc
Parents:
6371c5bf
git-author:
Dzejrou <dzejrou@…> (2017-12-04 15:52:59)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:19)
Message:

cpp: added basic unformatted output to std::ostream

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/ostream.hpp

    r6371c5bf r91ba048  
    3030#define LIBCPP_OSTREAM
    3131
     32#include <ios>
    3233#include <iosfwd>
     34#include <locale>
    3335
    3436namespace std
    3537{
     38    /**
     39     * 27.7.3.1, class template basic_ostream:
     40     */
     41
    3642    template<class Char, class Traits>
    37     class basic_ostream;
     43    class basic_ostream: virtual public basic_ios<Char, Traits>
     44    {
     45        public:
     46            using char_type   = Char;
     47            using traits_type = Traits;
     48            using int_type    = typename traits_type::int_type;
     49            using pos_type    = typename traits_type::pos_type;
     50            using off_type    = typename traits_type::off_type;
     51
     52            /**
     53             * 27.7.3.2, constructor/destructor:
     54             */
     55
     56            explicit basic_ostream(basic_streambuf<char_type, traits_type>* sb)
     57            {
     58                basic_ios<Char, Traits>::init(sb);
     59            }
     60
     61            virtual ~basic_ostream()
     62            { /* DUMMY BODY */ }
     63
     64            /**
     65             * 27.7.3.4, prefix/suffix:
     66             */
     67
     68            class sentry
     69            {
     70                public:
     71                    explicit sentry(basic_ostream<Char, Traits>& os)
     72                        : os_{os}, ok_{false}
     73                    {
     74                        if (os.good())
     75                        {
     76                            if (os.tie())
     77                                os.tie()->flush();
     78                        }
     79
     80                        ok_ = os.good();
     81                    }
     82
     83                    ~sentry()
     84                    {
     85                        if ((os_.flags() & ios_base::unitbuf) && os_.good())
     86                        {
     87                            auto ret = os_.rdbuf()->pubsync();
     88                            (void)ret;
     89                            // TODO: if ret == -1, set badbit in rdstate
     90                        }
     91                    }
     92
     93                    explicit operator bool() const
     94                    {
     95                        return ok_;
     96                    }
     97
     98                    sentry(const sentry&) = delete;
     99                    sentry& operator=(const sentry&) = delete;
     100
     101                private:
     102                    basic_ostream<Char, Traits>& os_;
     103                    bool ok_;
     104            };
     105
     106            /**
     107             * 27.7.3.6, formatted output:
     108             */
     109
     110            basic_ostream<Char, Traits>& operator<<(
     111                basic_ostream<Char, Traits>& (*pf)(basic_ostream<Char, Traits>&)
     112            )
     113            {
     114                return pf(*this);
     115            }
     116
     117            basic_ostream<Char, Traits>& operator<<(
     118                basic_ios<Char, Traits>& (*pf)(basic_ios<Char, Traits>&)
     119            )
     120            {
     121                pf(*this);
     122
     123                return *this;
     124            }
     125
     126            basic_ostream<Char, Traits>& operator<<(
     127                ios_base& (*pf)(ios_base&)
     128            )
     129            {
     130                pf(*this);
     131
     132                return *this;
     133            }
     134
     135            basic_ostream<Char, Traits>& operator<<(bool x)
     136            {
     137                // TODO: sentry etc
     138                /* bool failed = use_facet< */
     139                /*     num_put<char_type, ostreambuf_iterator<char_type, traits_type>> */
     140                /* >(this->getloc()).put(*this, *this, this->fill(), x).failed(); */
     141
     142                /* if (failed) */
     143                /*     this->setstate(ios_base::badbit); */
     144
     145                return *this;
     146            }
     147
     148            basic_ostream<Char, Traits>& operator<<(short x)
     149            {
     150                // TODO: implement
     151                return *this;
     152            }
     153
     154            basic_ostream<Char, Traits>& operator<<(unsigned short x)
     155            {
     156                // TODO: implement
     157                return *this;
     158            }
     159
     160            basic_ostream<Char, Traits>& operator<<(int x)
     161            {
     162                // TODO: implement
     163                return *this;
     164            }
     165
     166            basic_ostream<Char, Traits>& operator<<(unsigned int x)
     167            {
     168                // TODO: implement
     169                return *this;
     170            }
     171
     172            basic_ostream<Char, Traits>& operator<<(long x)
     173            {
     174                // TODO: implement
     175                return *this;
     176            }
     177
     178            basic_ostream<Char, Traits>& operator<<(unsigned long x)
     179            {
     180                // TODO: implement
     181                return *this;
     182            }
     183
     184            basic_ostream<Char, Traits>& operator<<(long long x)
     185            {
     186                // TODO: implement
     187                return *this;
     188            }
     189
     190            basic_ostream<Char, Traits>& operator<<(unsigned long long x)
     191            {
     192                // TODO: implement
     193                return *this;
     194            }
     195
     196            basic_ostream<Char, Traits>& operator<<(float x)
     197            {
     198                // TODO: implement
     199                return *this;
     200            }
     201
     202            basic_ostream<Char, Traits>& operator<<(double x)
     203            {
     204                // TODO: implement
     205                return *this;
     206            }
     207
     208            basic_ostream<Char, Traits>& operator<<(long double x)
     209            {
     210                // TODO: implement
     211                return *this;
     212            }
     213
     214            basic_ostream<Char, Traits>& operator<<(const void* p)
     215            {
     216                // TODO: implement
     217                return *this;
     218            }
     219
     220            basic_ostream<Char, Traits>& operator<<(basic_streambuf<Char, Traits>* sb)
     221            {
     222                // TODO: implement
     223                return *this;
     224            }
     225
     226            /**
     227             * 27.7.3.7, unformatted output:
     228             * TODO: when we have exceptions, complete these
     229             */
     230
     231            basic_ostream<Char, Traits>& put(char_type c)
     232            {
     233                sentry sen{*this};
     234
     235                if (sen)
     236                {
     237                    auto ret = this->rdbuf()->sputc(c);
     238                    if (traits_type::eq_int_type(ret, traits_type::eof()))
     239                        this->setstate(ios_base::badbit);
     240                }
     241
     242                return *this;
     243            }
     244
     245            basic_ostream<Char, Traits>& write(const char_type* s, streamsize n)
     246            {
     247                sentry sen{*this};
     248
     249                if (sen)
     250                {
     251                    for (streamsize i = 0; i < n; ++i)
     252                    {
     253                        auto ret = this->rdbuf()->sputc(s[i]);
     254                        if (traits_type::eq_int_type(ret, traits_type::eof()))
     255                        {
     256                            this->setstate(ios_base::badbit);
     257                            break;
     258                        }
     259                    }
     260                }
     261
     262                return *this;
     263            }
     264
     265            basic_ostream<Char, Traits>& flush()
     266            {
     267                if (this->rdbuf())
     268                {
     269                    sentry sen{*this};
     270
     271                    if (sen)
     272                    {
     273                        auto ret = this->rdbuf()->pubsync();
     274                        if (ret == -1)
     275                            this->setstate(ios_base::badbit);
     276                    }
     277                }
     278
     279                return *this;
     280            }
     281
     282            /**
     283             * 27.7.3.5, seeks:
     284             */
     285
     286            pos_type tellp()
     287            {
     288                // TODO: implement
     289                return pos_type{};
     290            }
     291
     292            basic_ostream<Char, Traits>& seekp(pos_type pos)
     293            {
     294                // TODO: implement
     295                return *this;
     296            }
     297
     298            basic_ostream<Char, Traits>& seekp(off_type off, ios_base::seekdir dir)
     299            {
     300                // TODO: implement
     301                return *this;
     302            }
     303
     304        protected:
     305            basic_ostream(const basic_ostream&) = delete;
     306
     307            basic_ostream(basic_ostream&& other)
     308            {
     309                basic_ios<Char, Traits>::move(other);
     310            }
     311
     312            /**
     313             * 27.7.3.3, assign/swap:
     314             */
     315
     316            basic_ostream& operator=(const basic_ostream&) = delete;
     317
     318            basic_ostream& operator=(basic_ostream&& other)
     319            {
     320                swap(other);
     321
     322                return *this;
     323            }
     324
     325            void swap(basic_ostream& rhs)
     326            {
     327                basic_ios<Char, Traits>::swap(rhs);
     328            }
     329    };
    38330
    39331    using ostream  = basic_ostream<char>;
     
    41333
    42334    template<class Char, class Traits = char_traits<Char>>
    43     basic_ostream<Char, Traits>& endl(basic_ostream<Char, Traits>& os);
     335    basic_ostream<Char, Traits>& endl(basic_ostream<Char, Traits>& os)
     336    {
     337        os.put('\n');
     338        os.flush();
     339
     340        return os;
     341    }
    44342
    45343    template<class Char, class Traits = char_traits<Char>>
Note: See TracChangeset for help on using the changeset viewer.