Added missing catch block for std::exception (#598)
[platform/upstream/iotivity.git] / resource / android / OCAndroid.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 MediaTek All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <sstream>
22 #include "OCAndroid.h"
23
24 namespace OC {
25         template <typename T>
26         void from_string(const std::string& s, T& result) {
27                 std::stringstream ss(s);
28                 ss >> result;    // TODO handle errors
29         }
30
31         /*
32         template <typename T>
33         std::string to_string(T value)
34         {
35         std::ostringstream os ;
36         os << value ;
37         return os.str() ;
38         }
39         */
40
41 }
42
43 namespace std {
44
45         int stoi(const string& s)
46         {
47                 int ret;
48                 int &ref = ret;
49                 OC::from_string(s, ref);
50                 return ret;
51         }
52
53         double stod(const std::string& s)
54         {
55                 double ret;
56                 double &ref = ret;
57                 OC::from_string(s, ref);
58                 return ret;
59         }
60
61         long long stoll(const std::string& s)
62         {
63                 long long ret;
64                 long long int &ref = ret;
65                 OC::from_string(s, ref);
66                 return ret;
67         }
68
69         unsigned long long stoull(const std::string& s)
70         {
71                 unsigned long long ret;
72                 unsigned long long  &ref = ret;
73                 OC::from_string(s, ref);
74                 return ret;
75         }
76
77         long double stold(const string& s)
78         {
79                 long double ret;
80                 long double &ref = ret;
81                 OC::from_string(s, ref);
82                 return ret;
83         }
84
85         #define TO_STRING(_t) { \
86                 std::ostringstream os; \
87                 os << _t; \
88                 return os.str(); \
89         } \
90
91         std::string to_string(int val)
92         {
93                 TO_STRING(val)
94         }
95
96         std::string to_string(long val)
97         {
98                 TO_STRING(val)
99         }
100
101         std::string to_string(long long val)
102         {
103                 TO_STRING(val)
104         }
105
106         std::string to_string(unsigned val)
107         {
108                 TO_STRING(val)
109         }
110
111         std::string to_string(unsigned long val)
112         {
113                 TO_STRING(val)
114         }
115
116         std::string to_string(unsigned long long val)
117         {
118                 TO_STRING(val)
119         }
120
121         std::string to_string(float val)
122         {
123                 TO_STRING(val)
124         }
125
126         std::string to_string(double val)
127         {
128                 TO_STRING(val)
129         }
130
131         std::string to_string(long double val)
132         {
133                 TO_STRING(val)
134         }
135 } // std