blocxx

Cstr.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 Network Associates, 
00017 *       nor the names of its contributors or employees may be used to 
00018 *       endorse or promote products derived from this software without 
00019 *       specific prior written permission.
00020 * 
00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00025 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00026 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 * POSSIBILITY OF SUCH DAMAGE.
00032 *******************************************************************************/
00033 
00034 
00035 #ifndef BLOCXX_CSTR_HPP_INCLUDE_GUARD_
00036 #define BLOCXX_CSTR_HPP_INCLUDE_GUARD_
00037 
00042 #include "blocxx/BLOCXX_config.h"
00043 #include "blocxx/CommonFwd.hpp"
00044 #include "blocxx/Array.hpp"
00045 #include <cstdlib>
00046 
00047 namespace BLOCXX_NAMESPACE
00048 {
00049 
00050 namespace Cstr
00051 {
00052 
00053 template <typename S>
00054 struct is_char_ptr
00055 {
00056    enum { value = false };
00057 };
00058 
00059 template <>
00060 struct is_char_ptr<char *>
00061 {
00062    enum { value = true };
00063 };
00064 
00065 template <>
00066 struct is_char_ptr<char const *>
00067 {
00068    enum { value = true };
00069 };
00070 
00071 template <std::size_t N>
00072 struct is_char_ptr<char[N]>
00073 {
00074    enum { value = true };
00075 };
00076 
00077 template <std::size_t N>
00078 struct is_char_ptr<char const [N]>
00079 {
00080    enum { value = true };
00081 };
00082 
00083 template <typename S, bool is_char_pointer>
00084 struct CstrStringAux
00085 {
00086    static char const * c_str(S const & s)
00087    {
00088       return s.c_str();
00089    }
00090 };
00091 
00092 template <typename S>
00093 struct CstrStringAux<S, true>
00094 {
00095    static char const * c_str(S const & s)
00096    {
00097       return s;
00098    }
00099 };
00100 
00101 template <typename S>
00102 struct CstrString : public CstrStringAux<S, is_char_ptr<S>::value>
00103 {
00104 };
00105 
00109 //
00110 template <typename S>
00111 inline char const * to_char_ptr(S const & s)
00112 {
00113    return CstrString<S>::c_str(s);
00114 }
00115 
00129 template <typename SA>
00130 struct CstrArr
00131 {
00133    char const * const * sarr;
00134 
00135 private:
00140    CstrArr(SA const & s);
00141 };
00142 
00143 template <bool b>
00144 struct ctassert;
00145 
00146 template <>
00147 struct ctassert<true>
00148 {
00149 };
00150 
00151 template <typename S>
00152 struct CstrArr<S *> : private ctassert<is_char_ptr<S>::value>
00153 {
00154    char const * const * sarr;
00155    
00156    CstrArr(S const * sarr0)
00157    : sarr(sarr0)
00158    {
00159    }
00160 };
00161 
00162 template <typename S>
00163 struct CstrArr<S const *> : private ctassert<is_char_ptr<S>::value>
00164 {
00165    char const * const * sarr;
00166    
00167    CstrArr(S const * sarr0)
00168    : sarr(sarr0)
00169    {
00170    }
00171 };
00172 
00173 template <std::size_t N, typename S>
00174 struct CstrArr<S[N]> : private ctassert<is_char_ptr<S>::value>
00175 {
00176    char const * const * sarr;
00177    
00178    CstrArr(S const sarr0[N])
00179    : sarr(sarr0)
00180    {
00181    }
00182 };
00183 
00184 template <std::size_t N, typename S>
00185 struct CstrArr<S const [N]> : private ctassert<is_char_ptr<S>::value>
00186 {
00187    char const * const * sarr;
00188    
00189    CstrArr(S const sarr0[N])
00190    : sarr(sarr0)
00191    {
00192    }
00193 };
00194 
00195 template <typename S>
00196 struct CstrArr<Array<S> >
00197 {
00198    Array<char const *> a;
00199    char const * const * sarr;
00200 
00201    CstrArr(Array<S> const & s)
00202    {
00203       typename Array<S>::const_iterator it, itend = s.end();
00204       for (it = s.begin(); it != itend; ++it)
00205       {
00206          a.push_back(to_char_ptr(*it));
00207       }
00208       a.push_back(0);
00209       sarr = &a[0];
00210    }
00211 };
00212 
00213 } // namespace Cstr
00214 
00215 } // namespace BLOCXX_NAMESPACE
00216 
00217 #endif