Home | History | Annotate | Download | only in include
      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_IOMANIP__
     31 #define ANDROID_ASTL_IOMANIP__
     32 
     33 #include <ostream>
     34 
     35 // iomanips are structures used to manipulate streams. There are 3
     36 // parts to a manip:
     37 // - a struct to hold the parameters of the manip.
     38 // - a function in the std namespace to generate the above structure.
     39 // - a stream operator that takes the structure as second argument and
     40 //   apply the manip using the params in the structure.
     41 //
     42 // This allows something like that : cout << setprecision(10);
     43 
     44 namespace android {
     45 // Structures passed to the streams to set various aspect of it.
     46 struct SetBase { int base; };
     47 struct SetPrecision { int precision; };
     48 }
     49 
     50 namespace std {
     51 
     52 // Sent to a stream, calls 'precision(size_type)' on the instance.
     53 inline android::SetPrecision setprecision(int precision) {
     54     android::SetPrecision params;
     55     params.precision = precision;
     56     return params;
     57 }
     58 
     59 inline ostream& operator<<(ostream& os, android::SetPrecision params) {
     60     os.precision(params.precision);
     61     return os;
     62 }
     63 
     64 // Sent to a stream, set the ios_base::basefield on the instance.
     65 // Sent to a stream, calls 'precision(size_type)' on the instance.
     66 inline android::SetBase setbase(int base) {
     67     android::SetBase params;
     68     params.base = base;
     69     return params;
     70 }
     71 
     72 inline ostream& operator<<(ostream& os, android::SetBase params) {
     73     os.setf(params.base == 8  ? ios_base::oct :
     74             params.base == 10 ? ios_base::dec :
     75             params.base == 16 ? ios_base::hex :
     76             ios_base::fmtflags(0), ios_base::basefield);
     77     return os;
     78 }
     79 
     80 }  // namespace std
     81 
     82 #endif
     83