OW_Format.hpp

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

Generated on Thu Feb 9 08:47:59 2006 for openwbem by  doxygen 1.4.6