blocxx
Cstr.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Quest Software, Inc.,
15 * nor Novell, Inc.,
16 * nor Network Associates,
17 * nor the names of its contributors or employees may be used to
18 * endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33 
34 
35 #ifndef BLOCXX_CSTR_HPP_INCLUDE_GUARD_
36 #define BLOCXX_CSTR_HPP_INCLUDE_GUARD_
37 
42 #include "blocxx/BLOCXX_config.h"
43 #include "blocxx/CommonFwd.hpp"
44 #include "blocxx/Array.hpp"
45 #include <cstdlib>
46 
47 namespace BLOCXX_NAMESPACE
48 {
49 
50 namespace Cstr
51 {
52 
53 template <typename S>
55 {
56  enum { value = false };
57 };
58 
59 template <>
60 struct is_char_ptr<char *>
61 {
62  enum { value = true };
63 };
64 
65 template <>
66 struct is_char_ptr<char const *>
67 {
68  enum { value = true };
69 };
70 
71 template <std::size_t N>
72 struct is_char_ptr<char[N]>
73 {
74  enum { value = true };
75 };
76 
77 template <std::size_t N>
78 struct is_char_ptr<char const [N]>
79 {
80  enum { value = true };
81 };
82 
83 template <typename S, bool is_char_pointer>
85 {
86  static char const * c_str(S const & s)
87  {
88  return s.c_str();
89  }
90 };
91 
92 template <typename S>
93 struct CstrStringAux<S, true>
94 {
95  static char const * c_str(S const & s)
96  {
97  return s;
98  }
99 };
100 
101 template <typename S>
102 struct CstrString : public CstrStringAux<S, is_char_ptr<S>::value>
103 {
104 };
105 
109 //
110 template <typename S>
111 inline char const * to_char_ptr(S const & s)
112 {
113  return CstrString<S>::c_str(s);
114 }
115 
129 template <typename SA>
130 struct CstrArr
131 {
133  char const * const * sarr;
134 
135 private:
140  CstrArr(SA const & s);
141 };
142 
143 template <bool b>
144 struct ctassert;
145 
146 template <>
147 struct ctassert<true>
148 {
149 };
150 
151 template <typename S>
153 {
154  char const * const * sarr;
155 
156  CstrArr(S const * sarr0)
157  : sarr(sarr0)
158  {
159  }
160 };
161 
162 template <typename S>
163 struct CstrArr<S const *> : private ctassert<is_char_ptr<S>::value>
164 {
165  char const * const * sarr;
166 
167  CstrArr(S const * sarr0)
168  : sarr(sarr0)
169  {
170  }
171 };
172 
173 template <std::size_t N, typename S>
174 struct CstrArr<S[N]> : private ctassert<is_char_ptr<S>::value>
175 {
176  char const * const * sarr;
177 
178  CstrArr(S const sarr0[N])
179  : sarr(sarr0)
180  {
181  }
182 };
183 
184 template <std::size_t N, typename S>
185 struct CstrArr<S const [N]> : private ctassert<is_char_ptr<S>::value>
186 {
187  char const * const * sarr;
188 
189  CstrArr(S const sarr0[N])
190  : sarr(sarr0)
191  {
192  }
193 };
194 
195 template <typename S>
196 struct CstrArr<Array<S> >
197 {
199  char const * const * sarr;
200 
201  CstrArr(Array<S> const & s)
202  {
203  typename Array<S>::const_iterator it, itend = s.end();
204  for (it = s.begin(); it != itend; ++it)
205  {
206  a.push_back(to_char_ptr(*it));
207  }
208  a.push_back(0);
209  sarr = &a[0];
210  }
211 };
212 
213 } // namespace Cstr
214 
215 } // namespace BLOCXX_NAMESPACE
216 
217 #endif