OSDN Git Service

54c92c7dd65b65397678ea75c3d94e3efc9f4112
[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
30 namespace l7vs
31 {
32         class bad_lexical_cast : public std::bad_cast
33         {
34         public:
35                 bad_lexical_cast() {}
36         };
37
38         template <typename T>
39         T lexical_cast(const std::string& s)
40         {
41                 std::stringstream ss;
42                 T d;
43                 ss << s.c_str();
44                 while( !ss.eof() ){
45                         ss >> d;
46                         if(ss.fail()) {
47                                 throw bad_lexical_cast();
48                         }
49                 }
50                 return d;
51         }
52 }
53
54 #endif  //__LEXICAL_CAST_H__