Format.hpp

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

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6