blocxx

DelayedFormat.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Quest Software, 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 *       Quest Software, 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_DELAYED_FORMAT_HPP
00039 #define BLOCXX_DELAYED_FORMAT_HPP
00040 #include "blocxx/BLOCXX_config.h"
00041 #include <iosfwd>
00042 #include "blocxx/Format.hpp"
00043 #include "blocxx/Array.hpp"
00044 #include "blocxx/String.hpp"
00045 #include "blocxx/Reference.hpp"
00046 
00047 namespace BLOCXX_NAMESPACE
00048 {
00049    // These "internals" define a base class interface for the templated derived
00050    // classes to provide.  By doing this, we can create an array of these
00051    // objects for simplicity of storage.
00052    namespace DelayedFormatInternals
00053    {
00054       // Provide an interface for a virtual << operator.
00055       class BLOCXX_COMMON_API DelayedFormatReferenceBase
00056       {
00057       protected:
00058          DelayedFormatReferenceBase();
00059       public:
00060          virtual ~DelayedFormatReferenceBase();
00061          std::ostream& dumpToStream(std::ostream& o) const;
00062       private:
00063          virtual std::ostream& doDumpToStream(std::ostream& o) const = 0;
00064       };
00065       std::ostream& operator<<(std::ostream&, const DelayedFormatReferenceBase& s);
00066 
00067 
00068       // The templated version which any non-const reference (that defines a <<
00069       // operator for std::ostream) can be used.
00070       template<class T>
00071       class DelayedFormatReference : public DelayedFormatReferenceBase
00072       {
00073       public:
00074          DelayedFormatReference(T& t): ref_(t)
00075          {
00076          }
00077          ~DelayedFormatReference()
00078          {
00079          }
00080       private:
00081          // Not implemented.
00082          DelayedFormatReference(const DelayedFormatReference&);
00083          DelayedFormatReference& operator=(const DelayedFormatReference&);
00084 
00085          // Dump it to the stream.
00086          virtual std::ostream& doDumpToStream(std::ostream& o) const
00087          {
00088             return o << ref_;
00089          }
00090          T& ref_;
00091       };
00092    }
00093 
00104    class BLOCXX_COMMON_API DelayedFormat
00105    {
00106    public:
00114       DelayedFormat(const String& format);
00115       template <typename A>
00116       DelayedFormat(const String& format, A& a);
00117       template <typename A, typename B>
00118       DelayedFormat(const String& format, A& a, B& b);
00119       template <typename A, typename B, typename C>
00120       DelayedFormat(const String& format, A& a, B& b, C& c);
00121       template <typename A, typename B, typename C, typename D>
00122       DelayedFormat(const String& format, A& a, B& b, C& c, D& d);
00123       template <typename A, typename B, typename C, typename D, typename E>
00124       DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e);
00125       template <typename A, typename B, typename C, typename D, typename E, typename F>
00126       DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f);
00127       template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
00128       DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g);
00129       template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
00130       DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h);
00131       template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
00132       DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i);
00133 
00135       Format format() const;
00136 
00138       operator String() const;
00139 
00141       Format formatWithString(const String& fs) const;
00142       Format formatWithString(const char* fs) const;
00143    private:
00145       template <class T>
00146       void append(T& t);
00147 
00149       String formatString;
00150       typedef Reference<DelayedFormatInternals::DelayedFormatReferenceBase> paramEntry;
00152       Array<paramEntry> formatParameters;
00153    };
00154 
00155    template <typename T>
00156    void DelayedFormat::append(T& t)
00157    {
00158       formatParameters.append(paramEntry(new DelayedFormatInternals::DelayedFormatReference<T>(t)));
00159    }
00160 
00161    // Everything below here is just for the mess of constructors.
00162    template <typename A>
00163    DelayedFormat::DelayedFormat(const String& format, A& a)
00164       : formatString(format), formatParameters()
00165    {
00166       formatParameters.reserve(1);
00167       append(a);
00168    }
00169 
00170    template <typename A, typename B>
00171    DelayedFormat::DelayedFormat(const String& format, A& a, B& b)
00172       : formatString(format), formatParameters()
00173    {
00174       formatParameters.reserve(2);
00175       append(a); append(b);
00176    }
00177 
00178    template <typename A, typename B, typename C>
00179    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c)
00180       : formatString(format), formatParameters()
00181    {
00182       formatParameters.reserve(3);
00183       append(a); append(b); append(c);
00184    }
00185 
00186    template <typename A, typename B, typename C, typename D>
00187    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d)
00188       : formatString(format), formatParameters()
00189    {
00190       formatParameters.reserve(4);
00191       append(a); append(b); append(c); append(d);
00192    }
00193 
00194    template <typename A, typename B, typename C, typename D, typename E>
00195    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e)
00196       : formatString(format), formatParameters()
00197    {
00198       formatParameters.reserve(5);
00199       append(a); append(b); append(c); append(d); append(e);
00200    }
00201 
00202    template <typename A, typename B, typename C, typename D, typename E, typename F>
00203    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f)
00204       : formatString(format), formatParameters()
00205    {
00206       formatParameters.reserve(6);
00207       append(a); append(b); append(c); append(d); append(e); append(f);
00208    }
00209 
00210    template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
00211    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g)
00212       : formatString(format), formatParameters()
00213    {
00214       formatParameters.reserve(7);
00215       append(a); append(b); append(c); append(d); append(e); append(f); append(g);
00216    }
00217 
00218    template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
00219    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h)
00220       : formatString(format), formatParameters()
00221    {
00222       formatParameters.reserve(8);
00223       append(a); append(b); append(c); append(d); append(e); append(f); append(g); append(h);
00224    }
00225 
00226    template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
00227    DelayedFormat::DelayedFormat(const String& format, A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i)
00228       : formatString(format), formatParameters()
00229    {
00230       formatParameters.reserve(9);
00231       append(a); append(b); append(c); append(d); append(e); append(f); append(g); append(h); append(i);
00232    }
00233 } // end namespace BLOCXX_NAMESPACE
00234 
00235 #endif