Web Wispers 1.2.2 C++ Unit Test Coverage |
|
|
|
|
Branch data Line data Source code
1 : : /* Copyright 2006-2009 Savarese Software Research Corporation
2 : : *
3 : : * Licensed under the Apache License, Version 2.0 (the "License");
4 : : * you may not use this file except in compliance with the License.
5 : : * You may obtain a copy of the License at
6 : : *
7 : : * https://www.savarese.com/software/ApacheLicense-2.0
8 : : *
9 : : * Unless required by applicable law or agreed to in writing, software
10 : : * distributed under the License is distributed on an "AS IS" BASIS,
11 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : * See the License for the specific language governing permissions and
13 : : * limitations under the License.
14 : : */
15 : :
16 : : /**
17 : : * @file
18 : : * This header defines the StringTo class.
19 : : */
20 : :
21 : : #ifndef __SSRC_WSPR_UTILITY_STRING_TO_H
22 : : #define __SSRC_WSPR_UTILITY_STRING_TO_H
23 : :
24 : : #include <ssrc/wispers-packages.h>
25 : :
26 : : #include <typeinfo>
27 : : #include <sstream>
28 : : #include <cstring>
29 : :
30 : : __BEGIN_NS_SSRC_WSPR_UTILITY
31 : :
32 : : using std::string;
33 : : using std::istringstream;
34 : :
35 : : #if defined(__GNUC__) && (__GNUC__ >= 4)
36 : : #define __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
37 : : #endif
38 : :
39 : : /**
40 : : *
41 : : */
42 : 7 : class StringTo {
43 : : istringstream _istream;
44 : :
45 : : public:
46 : : typedef istringstream::fmtflags fmtflags;
47 : : typedef istringstream::iostate iostate;
48 : :
49 : : StringTo(const StringTo &) { }
50 : :
51 : 7 : StringTo() { }
52 : :
53 : : /**
54 : : * Converts a string to an object.
55 : : *
56 : : *
57 : : *
58 : : *
59 : : *
60 : : */
61 : : template<typename T> T cast(const string & str)
62 : : SSRC_DECL_THROW(std::bad_cast)
63 : : {
64 : : T result;
65 : :
66 : : #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
67 : : _istream.clear();
68 : : _istream.rdbuf()->pubsetbuf(const_cast<char *>(str.c_str()), str.size());
69 : : // _istream.seekg(0);
70 : : #else
71 : : _istream.str(str);
72 : : #endif
73 : :
74 : : if(!(_istream >> result))
75 : : throw std::bad_cast();
76 : :
77 : : return result;
78 : : }
79 : :
80 : 11 : template<typename T> T cast(const char *str)
81 : : SSRC_DECL_THROW(std::bad_cast)
82 : : {
83 : : T result;
84 : :
85 : : #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
86 : 11 : _istream.clear();
87 : 11 : _istream.rdbuf()->pubsetbuf(const_cast<char *>(str), std::strlen(str));
88 : : // _istream.seekg(0);
89 : : #else
90 : : _istream.str(str);
91 : : #endif
92 : :
93 [ - + - + : 11 : if(!(_istream >> result))
- + ]
94 : 0 : throw std::bad_cast();
95 : :
96 : 11 : return result;
97 : : }
98 : :
99 : : fmtflags flags() const {
100 : : return _istream.flags();
101 : : }
102 : :
103 : : fmtflags flags(const fmtflags flags) {
104 : : return _istream.flags(flags);
105 : : }
106 : :
107 : : fmtflags setf(const fmtflags flags) {
108 : : return _istream.setf(flags);
109 : : }
110 : :
111 : : fmtflags setf(const fmtflags flags,
112 : : const fmtflags mask)
113 : : {
114 : : return _istream.setf(flags, mask);
115 : : }
116 : :
117 : : void unsetf(const fmtflags flags) {
118 : : _istream.unsetf(flags);
119 : : }
120 : :
121 : : iostate rdstate() const {
122 : : return _istream.rdstate();
123 : : }
124 : :
125 : : void clear(iostate state = istringstream::goodbit) {
126 : : _istream.clear(state);
127 : : }
128 : :
129 : : void setstate(iostate state) {
130 : : _istream.setstate(state);
131 : : }
132 : :
133 : : };
134 : :
135 : : #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
136 : : #undef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
137 : : #endif
138 : :
139 : : template<typename T>
140 : : inline T string_to(const string & str)
141 : : SSRC_DECL_THROW(std::bad_cast)
142 : : {
143 : : StringTo number_cast;
144 : : return number_cast.cast<T>(str);
145 : : }
146 : :
147 : : template<>
148 : : inline string string_to<string>(const string & str)
149 : : SSRC_DECL_THROW(std::bad_cast)
150 : : {
151 : : return str;
152 : : }
153 : :
154 : : template<>
155 : : inline const char * string_to<const char *>(const string & str)
156 : : SSRC_DECL_THROW(std::bad_cast)
157 : : {
158 : : return str.c_str();
159 : : }
160 : :
161 : : template<typename T>
162 : 6 : inline T string_to(const char *str)
163 : : SSRC_DECL_THROW(std::bad_cast)
164 : : {
165 : 12 : StringTo number_cast;
166 [ + - + - : 12 : return number_cast.cast<T>(str);
+ - ]
167 : : }
168 : :
169 : : template<>
170 : : inline string string_to<string>(const char *str)
171 : : SSRC_DECL_THROW(std::bad_cast)
172 : : {
173 : : return string(str);
174 : : }
175 : :
176 : : template<>
177 : : inline const char * string_to<const char *>(const char *str)
178 : : SSRC_DECL_THROW(std::bad_cast)
179 : : {
180 : : return str;
181 : : }
182 : :
183 : : __END_NS_SSRC_WSPR_UTILITY
184 : :
185 : : #endif
|
Copyright © 2017 Savarese Software Research Corporation. All rights reserved