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
47namespace BLOCXX_NAMESPACE
48{
49
50namespace Cstr
51{
52
53template <typename S>
55{
56 enum { value = false };
57};
58
59template <>
60struct is_char_ptr<char *>
61{
62 enum { value = true };
63};
64
65template <>
66struct is_char_ptr<char const *>
67{
68 enum { value = true };
69};
70
71template <std::size_t N>
72struct is_char_ptr<char[N]>
73{
74 enum { value = true };
75};
76
77template <std::size_t N>
78struct is_char_ptr<char const [N]>
79{
80 enum { value = true };
81};
82
83template <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
92template <typename S>
93struct CstrStringAux<S, true>
94{
95 static char const * c_str(S const & s)
96 {
97 return s;
98 }
99};
100
101template <typename S>
102struct CstrString : public CstrStringAux<S, is_char_ptr<S>::value>
103{
104};
105
109//
110template <typename S>
111inline char const * to_char_ptr(S const & s)
112{
113 return CstrString<S>::c_str(s);
114}
115
129template <typename SA>
131{
133 char const * const * sarr;
134
135private:
140 CstrArr(SA const & s);
141};
142
143template <bool b>
144struct ctassert;
145
146template <>
147struct ctassert<true>
148{
149};
150
151template <typename S>
153{
154 char const * const * sarr;
155
156 CstrArr(S const * sarr0)
157 : sarr(sarr0)
158 {
159 }
160};
161
162template <typename S>
163struct 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
173template <std::size_t N, typename S>
174struct 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
184template <std::size_t N, typename S>
185struct 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
195template <typename S>
196struct 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
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
Definition ArrayFwd.hpp:46
V::const_iterator const_iterator
Definition Array.hpp:85
void push_back(const T &x)
Append an element to the end of the Array.
char const * to_char_ptr(S const &s)
Definition Cstr.hpp:111
Taken from RFC 1321.
Class for converting values of type S into char const * const *.
Definition Cstr.hpp:131
CstrArr(SA const &s)
Initializes sarr with the null-terminated char const * const * value corresponding to sa.
char const *const * sarr
Converted value.
Definition Cstr.hpp:133
static char const * c_str(S const &s)
Definition Cstr.hpp:95
static char const * c_str(S const &s)
Definition Cstr.hpp:86