blocxx

DelayedFormat.cpp

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 #include "blocxx/BLOCXX_config.h"
00039 #include "blocxx/DelayedFormat.hpp"
00040 #include "blocxx/StringStream.hpp"
00041 #include "blocxx/Assertion.hpp"
00042 
00043 namespace BLOCXX_NAMESPACE
00044 {
00045    namespace DelayedFormatInternals
00046    {
00047       DelayedFormatReferenceBase::DelayedFormatReferenceBase()
00048       {
00049       }
00050       DelayedFormatReferenceBase::~DelayedFormatReferenceBase()
00051       {
00052       }
00053       std::ostream& DelayedFormatReferenceBase::dumpToStream(std::ostream& o) const
00054       {
00055          return doDumpToStream(o);
00056       }
00057       std::ostream& operator<<(std::ostream& o, const DelayedFormatReferenceBase& s)
00058       {
00059          return s.dumpToStream(o);
00060       }
00061    }
00062 
00063    DelayedFormat::DelayedFormat(const String& format)
00064       : formatString(format), formatParameters()
00065    {
00066    }
00067 
00068    DelayedFormat::operator String() const
00069    {
00070       return format().toString();
00071    }
00072 
00073    Format DelayedFormat::format() const
00074    {
00075       return formatWithString(formatString.c_str());
00076    }
00077 
00078    Format DelayedFormat::formatWithString(const String& fs) const
00079    {
00080       return formatWithString(fs.c_str());
00081    }
00082 
00083    Format DelayedFormat::formatWithString(const char* fsp) const
00084    {
00085       // The constructors allow up to 9 (inclusive) parameters.
00086       BLOCXX_ASSERT( formatParameters.size() <= 9 );
00087       const Array<Reference<DelayedFormatInternals::DelayedFormatReferenceBase> >& fp = formatParameters;
00088       switch( formatParameters.size() )
00089       {
00090       case 0:
00091          return Format(fsp, "");
00092       case 1:
00093          return Format(fsp, *fp[0]);
00094       case 2:
00095          return Format(fsp, *fp[0], *fp[1]);
00096       case 3:
00097          return Format(fsp, *fp[0], *fp[1], *fp[2]);
00098       case 4:
00099          return Format(fsp, *fp[0], *fp[1], *fp[2], *fp[3]);
00100       case 5:
00101          return Format(fsp, *fp[0], *fp[1], *fp[2], *fp[3], *fp[4]);
00102       case 6:
00103          return Format(fsp, *fp[0], *fp[1], *fp[2], *fp[3], *fp[4], *fp[5]);
00104       case 7:
00105          return Format(fsp, *fp[0], *fp[1], *fp[2], *fp[3], *fp[4], *fp[5], *fp[6]);
00106       case 8:
00107          return Format(fsp, *fp[0], *fp[1], *fp[2], *fp[3], *fp[4], *fp[5], *fp[6], *fp[7]);
00108       case 9:
00109          return Format(fsp, *fp[0], *fp[1], *fp[2], *fp[3], *fp[4], *fp[5], *fp[6], *fp[7], *fp[8]);
00110       }
00111       // Never reached.
00112       return Format("","");
00113    }
00114 
00115 
00116 } // end namespace BLOCXX_NAMESPACE