ios_base.h revision f1112427006f4fd9b72cb46acd46ddbbf1e7b741
1/* -*- c++ -*- */
2/*
3 * Copyright (C) 2010 The Android Open Source Project
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef ANDROID_ASTL_IOS_BASE_H__
31#define ANDROID_ASTL_IOS_BASE_H__
32
33#include <ios_pos_types.h>
34
35namespace android {
36// Flags are used to put the stream is a certain state which affect
37// how data is formatted.
38enum IosBaseFlags {
39    ios_baseflags_boolalpha   = 1 << 0,
40    ios_baseflags_dec         = 1 << 1,
41    ios_baseflags_fixed       = 1 << 2,
42    ios_baseflags_hex         = 1 << 3,
43    ios_baseflags_internal    = 1 << 4,
44    ios_baseflags_left        = 1 << 5,
45    ios_baseflags_oct         = 1 << 6,
46    ios_baseflags_right       = 1 << 7,
47    ios_baseflags_scientific  = 1 << 8,
48    ios_baseflags_showbase    = 1 << 9,
49    ios_baseflags_showpoint   = 1 << 10,
50    ios_baseflags_showpos     = 1 << 11,
51    ios_baseflags_skipws      = 1 << 12,
52    ios_baseflags_unitbuf     = 1 << 13,
53    ios_baseflags_uppercase   = 1 << 14,
54    ios_baseflags_adjustfield = ios_baseflags_left | ios_baseflags_right | ios_baseflags_internal,
55    ios_baseflags_basefield   = ios_baseflags_dec | ios_baseflags_oct | ios_baseflags_hex,
56    ios_baseflags_floatfield  = ios_baseflags_scientific | ios_baseflags_fixed,
57    ios_baseflags_end         = 1 << 15
58};
59}  // namespace android
60
61namespace std {
62
63/**
64 * Root of the streams inheritance.
65 * The STL defines ios_base as a template with 2 params char types and
66 * traits. We support only char and no traits.
67 * ios_base defines flags, types and fields to hold these values.
68 * ios_base is extended by basic_ios which wraps a streambuf and
69 * provides common methods for all streams.
70 * The only mode supported for the standards streams (cout, cerr, cin)
71 * is synced with stdio.
72 */
73
74class ios_base
75{
76  public:
77    typedef int io_state;
78    typedef int open_mode;
79    typedef int seek_dir;
80    typedef std::streampos streampos;
81    typedef std::streamoff streamoff;
82
83
84  protected:
85    ios_base();
86
87  public:
88    virtual ~ios_base();
89    typedef int fmtflags;
90
91    // boolalpha:  Insert and extract bool type in alphabetic format.
92    // dec:        Convert integer input or generates integer output in
93    //             decimal base.
94    // fixed:      Generate floating-point output in a fixed-point notation.
95    // hex:        Convert integer input or generates integer output in
96    //             hexadecimal base.
97    // internal:   Adds fill characters as the designated interanl point
98    //             in certain generated output, or identical to right
99    //             if no such point is designated.
100    // left:       Adds fill characters on the right (final positions) of
101    //             certain generated output.
102    // oct:        Convert integer input or generates integer output in octal
103    //             base.
104    // right:      Adds fill characters on the left (initial positions) of
105    //             certain generated output.
106    // scientific: Generates floating point output in scientific notation.
107    // showbase:   Generates a prefix indicating the numeric base of generated
108    //             integer output.
109    // showpoint:  Generate a decimal point character unconditionally in
110    //             generated floating point output.
111    // showpos:    Generate a + sign in non-negative generated numeric output.
112    // skipws:     Skips leading white space before certain input operations.
113    // unitbuf:    Flushes output after each output operation.
114    // uppercase:  Replaces certain lowercase letters with their upppercase
115    //             equivalents in generated output.
116    static const fmtflags boolalpha   = android::ios_baseflags_boolalpha;
117    static const fmtflags dec         = android::ios_baseflags_dec;
118    static const fmtflags fixed       = android::ios_baseflags_fixed;
119    static const fmtflags hex         = android::ios_baseflags_hex;
120    static const fmtflags internal    = android::ios_baseflags_internal;
121    static const fmtflags left        = android::ios_baseflags_left;
122    static const fmtflags oct         = android::ios_baseflags_oct;
123    static const fmtflags right       = android::ios_baseflags_right;
124    static const fmtflags scientific  = android::ios_baseflags_scientific;
125    static const fmtflags showbase    = android::ios_baseflags_showbase;
126    static const fmtflags showpoint   = android::ios_baseflags_showpoint;
127    static const fmtflags showpos     = android::ios_baseflags_showpos;
128    static const fmtflags skipws      = android::ios_baseflags_skipws;
129    static const fmtflags unitbuf     = android::ios_baseflags_unitbuf;
130    static const fmtflags uppercase   = android::ios_baseflags_uppercase;
131
132    static const fmtflags adjustfield = android::ios_baseflags_adjustfield;
133    static const fmtflags basefield   = android::ios_baseflags_basefield;
134    static const fmtflags floatfield  = android::ios_baseflags_floatfield;
135
136    // Set all the flags at once.
137    // @return the previous value of the format flags
138    fmtflags flags(fmtflags flags);
139
140    // @return all the flags at once
141    fmtflags flags() const { return mFlags; }
142
143    // Set flags.
144    // @return the previous value of the format flags
145    fmtflags setf(fmtflags flags);
146
147    // Clears 'mask' and set the 'flags' & 'mask'.
148    // @return the previous value of the format flags
149    fmtflags setf(fmtflags flags, fmtflags mask);
150
151    // Clears 'mask'.
152    void unsetf(fmtflags mask);
153
154    /**
155     * @return The precision (number of digits after the decimal
156     * point) to generate on certain output conversions. 6 by default.
157     */
158    streamsize precision() const { return mPrecision; }
159
160    /**
161     *  @param precision The new precision value. Values < 0 are ignored.
162     *  @return The previous value of precision(). 0 by default;
163     */
164    streamsize precision(streamsize precision);
165
166    /**
167     * @return The minimum field width to generate on output
168     * operations.
169     */
170    streamsize width() const { return mWidth; }
171
172    /**
173     *  @param width The new width value. Values < 0 are ignored.
174     *  @return The previous value of width().
175     */
176    streamsize width(streamsize width);
177
178    // Helper class to initialize the standard streams. Its
179    // construction ensures the initialization of the stdio streams
180    // (cout, cerr,...) declared in iostream. These wrap the standard
181    // C streams declared in <cstdio>.
182    // The destruction of the last instance of this class will flush
183    // the streams.
184    class Init {
185      public:
186        Init();
187        ~Init();
188
189        static bool done() { return sDone; }
190      private:
191        static int sGuard;
192        static bool sDone;
193    };
194
195  private:
196    fmtflags   mFlags;
197    streamsize mPrecision;
198    streamsize mWidth;
199};
200
201}  // namespace std
202
203#endif
204