OSDN Git Service

52a449cbbdfee5e88e762c0818c0f4cebd7b67f4
[ultramonkey-l7/sslproxy.git] / logger / lexical_cast.h
1 /*
2  * @file  lexical_cast.h
3  *
4  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
5  * Copyright (C) 2008  NTT COMWARE Corporation.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *      
22  **********************************************************************/
23
24 #ifndef __LEXICAL_CAST_H__
25 #define __LEXICAL_CAST_H__
26
27 #include <string>
28 #include <sstream>
29 #include <typeinfo>
30
31 namespace l7vs
32 {
33         class bad_lexical_cast : public std::bad_cast
34         {
35         public:
36                 bad_lexical_cast() {}
37         };
38
39         template <typename T>
40         T lexical_cast(const std::string& s)
41         {
42                 std::stringstream ss;
43                 T d;
44                 ss << s.c_str();
45                 while( !ss.eof() ){
46                         ss >> d;
47                         if(ss.fail()) {
48                                 throw bad_lexical_cast();
49                         }
50                 }
51                 return d;
52         }
53 }
54
55 #endif  //__LEXICAL_CAST_H__