00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 }
00214
00215 }
00216
00217 #endif