diff options
author | Nicolas Catania <niko@google.com> | 2010-01-28 15:12:24 -0800 |
---|---|---|
committer | Nicolas Catania <niko@google.com> | 2010-02-01 13:17:18 -0800 |
commit | 7c0a09a5022e5fb75b7a7dd6d68d4f811f6c462b (patch) | |
tree | ffd992cc1654229a347efe5d9afd858ed4b99d8e | |
parent | 74a6fdea77d52a17be4bc38831fe02a31cefbf34 (diff) | |
download | astl-7c0a09a5022e5fb75b7a7dd6d68d4f811f6c462b.tar.gz |
In ostream, added code to do the one time init of the stdio streams.
The new inner class Init will be in charge of constructing the
streams cout and cerr.
-rw-r--r-- | include/ios_base.h | 27 | ||||
-rw-r--r-- | include/iostream | 64 | ||||
-rw-r--r-- | include/ostream | 8 | ||||
-rw-r--r-- | src/Android.mk | 1 | ||||
-rw-r--r-- | src/ios_base.cpp | 41 | ||||
-rw-r--r-- | src/ios_globals.cpp | 47 | ||||
-rw-r--r-- | src/ostream.cpp | 6 | ||||
-rw-r--r-- | tests/Android.mk | 1 | ||||
-rw-r--r-- | tests/test_ios_base.cpp | 2 | ||||
-rw-r--r-- | tests/test_iostream.cpp | 75 |
10 files changed, 264 insertions, 8 deletions
diff --git a/include/ios_base.h b/include/ios_base.h index 858bb96..18b0ef8 100644 --- a/include/ios_base.h +++ b/include/ios_base.h @@ -27,8 +27,8 @@ * SUCH DAMAGE. */ -#ifndef ANDROID_ASTL_IOS_BASE__ -#define ANDROID_ASTL_IOS_BASE__ +#ifndef ANDROID_ASTL_IOS_BASE_H__ +#define ANDROID_ASTL_IOS_BASE_H__ #include <ios_pos_types.h> @@ -38,6 +38,11 @@ namespace std { * Root of the streams inheritance. * The STL defines ios_base as a template with 2 params char types and * traits. We support only char and no traits. + * ios_base defines flags, types and fields to hold these values. + * ios_base is extended by basic_ios which wraps a streambuf and + * provides common methods for all streams. + * The only mode supported for the standards streams (cout, cerr, cin) + * is synced with stdio. */ class ios_base @@ -80,7 +85,23 @@ class ios_base */ streamsize width(streamsize width); -private: + // Helper class to initialize the standard streams. Its + // construction ensures the initialization of the stdio + // streams (cout, cerr,...) declared in iostream. + // The destruction of the last instance of this class will flush + // the streams. + class Init { + public: + Init(); + ~Init(); + + static bool done() { return sDone; } + private: + static int sGuard; + static bool sDone; + }; + + private: streamsize mPrecision; streamsize mWidth; }; diff --git a/include/iostream b/include/iostream new file mode 100644 index 0000000..abfc6b2 --- /dev/null +++ b/include/iostream @@ -0,0 +1,64 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2010 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef ANDROID_ASTL_IOSTREAM__ +#define ANDROID_ASTL_IOSTREAM__ + +#include <ios_base.h> +#include <ostream> + +/** + * Declaration of the standard stream objects. + * + * The inclusion of this header will guarantee the initialization of + * the std streams. Since it uses statics (see ioinit below) to + * initialize the cout/cerr streams and that statics are initialized + * in their declaration order, it is best to include this file first + * just in case another included one uses cout/cerr in a static + * instance constructor. + * + * The storage for cout and cerr is in ios_globals.cpp. + */ + +namespace std { +// cin and clog are not supported. +// No wchar support either. +extern ostream cout; // Linked to standard output +extern ostream cerr; // Linked to standard error (unbuffered) + +// Each translation unit that includes this file will get a copy of +// this static variable. Its construction ensures that the standard +// streams (cout, cerr, etc...) have been initialized. +// The destruction of each instance of ioinit will flush the +// streams (e.g on unload). +static ios_base::Init ioinit; + +} // namespace std + +#endif diff --git a/include/ostream b/include/ostream index ef31b34..2bfb722 100644 --- a/include/ostream +++ b/include/ostream @@ -41,7 +41,7 @@ namespace std { * wchar. Since Android supports only char, we don't use a template * here. */ - +class streambuf; class ostream: public ios_base { public: @@ -54,8 +54,14 @@ class ostream: public ios_base ostream(); public: + // TODO: implement. + ostream(streambuf *sb) {} virtual ~ostream(); + // Synchronize the stream buffer. + // TODO: implement. + ostream& flush() { return *this; } + /** * Interface for manipulators (e.g std::endl, std::hex in * expression like "std::cout << std::endl"). See iomanip header. diff --git a/src/Android.mk b/src/Android.mk index c6c3d54..9613728 100644 --- a/src/Android.mk +++ b/src/Android.mk @@ -20,6 +20,7 @@ LOCAL_PATH := $(call my-dir) astl_common_src_files := \ ios_base.cpp \ + ios_globals.cpp \ ios_pos_types.cpp \ ostream.cpp \ streambuf.cpp \ diff --git a/src/ios_base.cpp b/src/ios_base.cpp index 4fe7921..2ed71e3 100644 --- a/src/ios_base.cpp +++ b/src/ios_base.cpp @@ -27,8 +27,19 @@ */ #include <ios_base.h> +#include <new> // for placement new. +#include <iostream> // For cout, cerr +#include <ostream> +#include <streambuf> + +// Defined in bionic/libstdc++/src/one_time_construction.cpp +extern "C" int __cxa_guard_acquire(int volatile * gv); +extern "C" void __cxa_guard_release(int volatile * gv); namespace std { +int ios_base::Init::sGuard = 0; +bool ios_base::Init::sDone = false; + // Implementation of the ios_base, common stuff for all the streams. ios_base::ios_base() @@ -52,4 +63,34 @@ streamsize ios_base::width(streamsize width) { return prev; } +// TODO: This is a temporary class used to illustrate how the +// construction will happen. + +class std_filebuf: public streambuf { + public: + std_filebuf() {} + virtual ~std_filebuf() {} +}; + +static std_filebuf real_cout; +static std_filebuf real_cerr; + +ios_base::Init::Init() { + if (__cxa_guard_acquire(&sGuard) == 1) { + if (!sDone) { + // Create the global cout and cerr structures. cout/cerr + // storage is in ios_globals.cpp. + new (&cout) ostream(&real_cout); + new (&cerr) ostream(&real_cerr); + sDone = true; + } + __cxa_guard_release(&sGuard); + } +} + +ios_base::Init::~Init() { + cout.flush(); + cerr.flush(); +} + } // namespace std diff --git a/src/ios_globals.cpp b/src/ios_globals.cpp new file mode 100644 index 0000000..1333721 --- /dev/null +++ b/src/ios_globals.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +// Make sure <iostream> is not included directly or indirectly. You +// can include <ostream> and/or <istream> just fine but <iostream> +// contains forward declarations for cout, cerr... that will conflict +// with the ones below. +#include <ostream> + +namespace std { + +// Global instances of cout and cerr. Here we reserve the memory for +// the stdio filebuf. The first time ios_base::Init::Init() is called, +// placement new is used to initialize these areas with proper +// instances of the streams. + +typedef char ostream_mem[sizeof(ostream)] +__attribute__ ((aligned(__alignof__(ostream)))); +ostream_mem cout; +ostream_mem cerr; + +} // namespace std diff --git a/src/ostream.cpp b/src/ostream.cpp index 63d3b0a..baa05f1 100644 --- a/src/ostream.cpp +++ b/src/ostream.cpp @@ -29,10 +29,10 @@ #include <ostream> namespace std { -// Implementation of the std::ostream class. +// Defined in bionic/libstdc++/src/one_time_construction.cpp -ostream::ostream() {} +ostream::ostream() { } -ostream::~ostream() {} +ostream::~ostream() { } } // namespace std diff --git a/tests/Android.mk b/tests/Android.mk index 5de2eb7..d46ba81 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -66,6 +66,7 @@ sources := \ test_functional.cpp \ test_ios_base.cpp \ test_ios_pos_types.cpp \ + test_iostream.cpp \ test_iterator.cpp \ test_limits.cpp \ test_memory.cpp \ diff --git a/tests/test_ios_base.cpp b/tests/test_ios_base.cpp index 4e87419..e7eb096 100644 --- a/tests/test_ios_base.cpp +++ b/tests/test_ios_base.cpp @@ -28,7 +28,7 @@ */ #include "../include/ios_base.h" -#ifndef ANDROID_ASTL_IOS_BASE__ +#ifndef ANDROID_ASTL_IOS_BASE_H__ #error "Wrong header included!!" #endif #include "common.h" diff --git a/tests/test_iostream.cpp b/tests/test_iostream.cpp new file mode 100644 index 0000000..c1d1332 --- /dev/null +++ b/tests/test_iostream.cpp @@ -0,0 +1,75 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2010 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "../include/iostream" +#ifndef ANDROID_ASTL_IOSTREAM__ +#error "Wrong header included!!" +#endif +#include "common.h" + +namespace android { + +class A { + public: + A() { + mPassed = std::cout.precision() == 6; + } + static A mInstance; + bool mPassed; +}; +A A::mInstance; + +bool testStaticInit() { + EXPECT_TRUE(A::mInstance.mPassed); + return true; +} + +bool testOstream() { + EXPECT_TRUE(std::cout.precision() == 6); + EXPECT_TRUE(std::cerr.precision() == 6); + + std::cout.precision(20); + std::cerr.precision(20); + + EXPECT_TRUE(std::cout.precision() == 20); + EXPECT_TRUE(std::cerr.precision() == 20); + // reset back to the default value. + std::cout.precision(6); + std::cerr.precision(6); + return true; +} + +} // namespace android + +int main(int argc, char **argv){ + FAIL_UNLESS(testStaticInit); + FAIL_UNLESS(testOstream); + + return kPassed; +} |