RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
SafeConvert.hpp
1 /*
2  * SafeConvert.hpp
3  *
4  * Copyright (C) 2022 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_SAFE_CONVERT_HPP
25 #define LAUNCHER_PLUGINS_SAFE_CONVERT_HPP
26 
27 #include <string>
28 #include <ios>
29 #include <iostream>
30 #include <locale>
31 
32 #include <boost/lexical_cast.hpp>
33 #include <boost/numeric/conversion/cast.hpp>
34 #include <Optional.hpp>
35 
36 #include <Error.hpp>
37 
38 namespace rstudio {
39 namespace launcher_plugins {
40 namespace safe_convert {
41 
52 template <typename T>
53 T stringTo(const std::string& in_strValue, T in_defaultValue)
54 {
55  try
56  {
57  return boost::lexical_cast<T>(in_strValue);
58  }
59  catch(boost::bad_lexical_cast&)
60  {
61  return in_defaultValue;
62  }
63 }
64 
73 template <typename T>
74 Optional<T> stringTo(const std::string& str)
75 {
76  Optional<T> result;
77 
78  try
79  {
80  result = boost::lexical_cast<T>(str);
81  }
82  catch(boost::bad_lexical_cast&)
83  {
84  }
85 
86  return result;
87 }
88 
100 template <typename T>
101 T stringTo(const std::string& in_strValue,
102  T in_defaultValue,
103  std::ios_base& (*in_f)(std::ios_base&))
104 {
105  std::istringstream iss(in_strValue);
106  T result;
107  if ((iss >> in_f >> result).fail())
108  return in_defaultValue;
109  return result;
110 }
111 
120 inline std::string numberToString(double in_input, bool in_localeIndependent = true)
121 {
122  try
123  {
124  std::ostringstream stream;
125  if (in_localeIndependent)
126  stream.imbue(std::locale::classic()); // force locale-independence
127  stream << std::fixed;
128  stream << in_input;
129  return stream.str();
130  }
131  CATCH_UNEXPECTED_EXCEPTION
132 
133  // return empty string for unexpected error
134  return std::string();
135 }
136 
146 template <typename T>
147 std::string numberToString(T input, bool localeIndependent = true)
148 {
149  try
150  {
151  std::ostringstream stream;
152  if (localeIndependent)
153  stream.imbue(std::locale::classic()); // force locale-independence
154 
155  stream << input;
156  return stream.str();
157  }
158  CATCH_UNEXPECTED_EXCEPTION
159 
160  // return empty string for unexpected error
161  return std::string();
162 }
163 
174 template <typename TInput, typename TOutput>
175 TOutput numberTo(TInput input, TOutput defaultValue)
176 {
177  try
178  {
179  return boost::numeric_cast<TOutput>(input);
180  }
181  catch(...)
182  {
183  return defaultValue;
184  }
185 }
186 
196 template <typename TInput, typename TOutput>
197 Optional<TOutput> numberTo(TInput input)
198 {
199  Optional<TOutput> result;
200 
201  try
202  {
203  result = boost::numeric_cast<TOutput>(input);
204  }
205  catch(...)
206  {
207  }
208 
209  return result;
210 }
211 
212 } // namespace safe_convert
213 } // namespace launcher_plugins
214 } // namespace rstudio
215 
216 #endif // LAUNCHER_PLUGINS_SAFE_CONVERT_HPP