ios_base.cpp revision d1e702c1f745428a7bc53cbbd80b0c283ca52de1
1/*
2 * Copyright (C) 2010 The Android Open Source Project
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 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <ios_base.h>
30#include <cstdio>
31#include <new>  // for placement new.
32#include <iostream>  // For cout, cerr
33#include <ostream>
34#include <stdio_filebuf.h>
35#include <streambuf>
36
37// Defined in bionic/libstdc++/src/one_time_construction.cpp
38extern "C" int __cxa_guard_acquire(int volatile * gv);
39extern "C" void __cxa_guard_release(int volatile * gv);
40
41namespace std {
42int ios_base::Init::sGuard = 0;
43bool ios_base::Init::sDone = false;
44
45// Implementation of the ios_base, common stuff for all the streams.
46
47ios_base::ios_base()
48    : mPrecision(6), mWidth(0) {}
49
50ios_base::~ios_base() {}
51
52streamsize ios_base::precision(streamsize precision) {
53    const streamsize prev = mPrecision;
54    if (precision >= 0) {  // Not sure what a negative precision would mean.
55        mPrecision = precision;
56    }
57    return prev;
58}
59
60streamsize ios_base::width(streamsize width) {
61    const streamsize prev = mWidth;
62    if (width >= 0) {  // Not sure what a negative width would mean.
63        mWidth = width;
64    }
65    return prev;
66}
67
68// TODO: This is a temporary class used to illustrate how the
69// construction will happen.
70
71class std_filebuf: public streambuf {
72  public:
73    std_filebuf() {}
74    virtual ~std_filebuf() {}
75};
76
77// Storage is declared in src/ios_globals.cpp
78extern android::stdio_filebuf stdio_filebuf_cout;
79extern android::stdio_filebuf stdio_filebuf_cerr;
80
81ios_base::Init::Init() {
82    if (__cxa_guard_acquire(&sGuard) == 1) {
83        if (!sDone) {
84            // Create the global cout and cerr
85            // structures. stdio_filebuf_cout/stdio_filebuf_cerr and
86            // cout/cerr storage are in ios_globals.cpp.
87            new (&stdio_filebuf_cout) android::stdio_filebuf(stdout);
88            new (&stdio_filebuf_cerr) android::stdio_filebuf(stderr);
89            new (&cout) ostream(&stdio_filebuf_cout);
90            new (&cerr) ostream(&stdio_filebuf_cerr);
91            sDone = true;
92        }
93        __cxa_guard_release(&sGuard);
94    }
95}
96
97ios_base::Init::~Init() {
98    cout.flush();
99    cerr.flush();
100}
101
102}  // namespace std
103