blocxx

Format.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00038 #ifndef BLOCXX_FORMAT_HPP
00039 #define BLOCXX_FORMAT_HPP
00040 #include "blocxx/BLOCXX_config.h"
00041 #include <iosfwd>
00042 #include "blocxx/StringStream.hpp"
00043 #include "blocxx/String.hpp"
00044 
00045 namespace BLOCXX_NAMESPACE
00046 {
00047 
00048 //  Format class declaration  -----------------------------------------------//
00049 class BLOCXX_COMMON_API Format
00050 {
00051 public:
00052    
00053    operator String() const;
00054    String toString() const;
00055    const char* c_str() const;
00056    // generic templated constructors
00057    template<typename A>
00058    Format(const char* ca, const A& a);
00059    template<typename A, typename B>
00060    Format(const char* ca, const A& a, const B& b);
00061    template<typename A, typename B, typename C>
00062    Format(const char* ca, const A& a, const B& b, const C& c);
00063    template<typename A, typename B, typename C, typename D>
00064    Format(const char* ca, const A& a, const B& b, const C& c, const D& d);
00065    template<typename A, typename B, typename C, typename D, typename E>
00066       Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e);
00067    template<typename A, typename B, typename C, typename D, typename E, typename F>
00068    Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f);
00069    template<typename A, typename B, typename C, typename D, typename E, typename F, typename G>
00070    Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g);
00071    template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
00072    Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h);
00073    template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
00074    Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h, const I& i);
00075    // These specific versions are to help prevent template bloat
00076    Format(const char* ca, const String& a);
00077    Format(const char* ca, const String& a, const String& b);
00078    Format(const char* ca, const String& a, const String& b, const String& c);
00079 private:
00080    OStringStream oss;
00081    char process(String& f, char c0);
00082    template<typename T> void put(const T& t);
00083    // These are to help prevent template bloat
00084    void put (const String& t);
00085    void put (char t);
00086    void put (unsigned char t);
00087    void put (short t);
00088    void put (unsigned short t);
00089    void put (int t);
00090    void put (unsigned int t);
00091    void put (long t);
00092    void put (unsigned long t);
00093    void put (long long t);
00094    void put (unsigned long long t);
00095 public:
00096    friend BLOCXX_COMMON_API std::ostream& operator<<(std::ostream& os, const Format& f);
00097 }; // class Format
00098 
00099 template<typename T>
00100 void Format::put(const T& t)
00101 { // t is inserted into oss
00102    if (!oss.good())
00103       return;
00104    oss << t;
00105 }
00106 
00107 template<typename A>
00108 Format::Format(const char* ca, const A& a) : oss()
00109 {
00110    String fmt(ca);
00111    while (!fmt.empty())
00112    {
00113       switch (process(fmt, '1'))
00114       {
00115          case '1': put(a); break;
00116       }
00117    }
00118 }
00119 template<typename A, typename B>
00120 Format::Format(const char* ca, const A& a, const B& b) : oss()
00121 {
00122    String fmt(ca);
00123    while (!fmt.empty())
00124    {
00125       switch (process(fmt, '2'))
00126       {
00127          case '1': put(a); break;
00128          case '2': put(b); break;
00129       }
00130    }
00131 }
00132 template<typename A, typename B, typename C>
00133 Format::Format(const char* ca, const A& a, const B& b, const C& c) : oss()
00134 {
00135    String fmt(ca);
00136    while (!fmt.empty())
00137    {
00138       switch (process(fmt, '3'))
00139       {
00140          case '1': put(a); break;
00141          case '2': put(b); break;
00142          case '3': put(c); break;
00143       }
00144    }
00145 }
00146 template<typename A, typename B, typename C, typename D>
00147 Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d) : oss()
00148 {
00149    String fmt(ca);
00150    while (!fmt.empty())
00151    {
00152       switch (process(fmt, '4'))
00153       {
00154          case '1': put(a); break;
00155          case '2': put(b); break;
00156          case '3': put(c); break;
00157          case '4': put(d); break;
00158       }
00159    }
00160 }
00161 template<typename A, typename B, typename C, typename D, typename E>
00162 Format::	Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e) : oss()
00163 {
00164    String fmt(ca);
00165    while (!fmt.empty())
00166    {
00167       switch (process(fmt, '5'))
00168       {
00169          case '1': put(a); break;
00170          case '2': put(b); break;
00171          case '3': put(c); break;
00172          case '4': put(d); break;
00173          case '5': put(e); break;
00174       }
00175    }
00176 }
00177 template<typename A, typename B, typename C, typename D, typename E, typename F>
00178 Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f) : oss()
00179 {
00180    String fmt(ca);
00181    while (!fmt.empty())
00182    {
00183       switch (process(fmt, '6'))
00184       {
00185          case '1': put(a); break;
00186          case '2': put(b); break;
00187          case '3': put(c); break;
00188          case '4': put(d); break;
00189          case '5': put(e); break;
00190          case '6': put(f); break;
00191       }
00192    }
00193 }
00194 template<typename A, typename B, typename C, typename D, typename E, typename F, typename G>
00195 Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g) : oss()
00196 {
00197    String fmt(ca);
00198    while (!fmt.empty())
00199    {
00200       switch (process(fmt, '7'))
00201       {
00202          case '1': put(a); break;
00203          case '2': put(b); break;
00204          case '3': put(c); break;
00205          case '4': put(d); break;
00206          case '5': put(e); break;
00207          case '6': put(f); break;
00208          case '7': put(g); break;
00209       }
00210    }
00211 }
00212 template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
00213 Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h) : oss()
00214 {
00215    String fmt(ca);
00216    while (!fmt.empty())
00217    {
00218       switch (process(fmt, '8'))
00219       {
00220          case '1': put(a); break;
00221          case '2': put(b); break;
00222          case '3': put(c); break;
00223          case '4': put(d); break;
00224          case '5': put(e); break;
00225          case '6': put(f); break;
00226          case '7': put(g); break;
00227          case '8': put(h); break;
00228       }
00229    }
00230 }
00231 template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
00232 Format::Format(const char* ca, const A& a, const B& b, const C& c, const D& d, const E& e, const F& f, const G& g, const H& h, const I& i) : oss()
00233 {
00234    String fmt(ca);
00235    while (!fmt.empty())
00236    {
00237       switch (process(fmt, '9'))
00238       {
00239          case '1': put(a); break;
00240          case '2': put(b); break;
00241          case '3': put(c); break;
00242          case '4': put(d); break;
00243          case '5': put(e); break;
00244          case '6': put(f); break;
00245          case '7': put(g); break;
00246          case '8': put(h); break;
00247          case '9': put(i); break;
00248       }
00249    }
00250 }
00251 
00252 } // end namespace BLOCXX_NAMESPACE
00253 
00254 #endif
00255