Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / Commons / StringUtils.h
index be6bcc8..ee7cbe8 100644 (file)
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
+/**
+ * @file   StringUtils.h
+ * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
+ */
+
 #ifndef WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
 #define WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
 
 #include <string>
 #include <vector>
+#include <sstream>
+#include <utility>
+#include <Commons/Exception.h>
+#include <Commons/Deprecated.h>
 
 namespace WrtDeviceApis {
 namespace Commons {
@@ -50,8 +59,7 @@ std::string trim(const std::string& str);
  * @param startStr String to be found at the beginning of str.
  * @return true when str starts with startStr, false otherwise.
  */
-bool startsWith(const std::string& str,
-        const std::string& startStr);
+bool startsWith(const std::string& str, const std::string& startStr);
 
 /**
  * Checks if str ends with startStr
@@ -59,8 +67,7 @@ bool startsWith(const std::string& str,
  * @param endStr String to be found at the end of str.
  * @return true when str starts with startStr, false otherwise.
  */
-bool endsWith(const std::string& str,
-        const std::string& endStr);
+bool endsWith(const std::string& str, const std::string& endStr);
 
 /**
  * Merges consecutive groups of duplicate characters into one.
@@ -75,8 +82,7 @@ bool endsWith(const std::string& str,
  * ...
  * @endcode
  */
-std::string unique(const std::string& str,
-        const char needle);
+std::string unique(const std::string& str, const char needle);
 
 /**
  * Checks if supplied string exists in specified array of c-strings.
@@ -86,8 +92,7 @@ std::string unique(const std::string& str,
  * @remarks Last element of array should be NULL, otherwise there is no
  *          way to calculate its number of elements.
  */
-bool inCStringArray(const std::string& needle,
-        const char* stack[]);
+bool inCStringArray(const std::string& needle, const char* stack[]);
 
 /**
  * Duplicates a string.
@@ -104,15 +109,61 @@ inline char* strdup(const std::string& str)
 }
 
 std::string merge(const std::vector<std::string>& strs,
-        std::string::value_type delim);
+                  std::string::value_type delim);
 
 std::vector<std::string> split(const std::string& str,
-        std::string::value_type delim);
+                               std::string::value_type delim);
 
-int toInt(const std::string &text);
+/**
+ * @deprecated Use convertTo<int>().
+ */
+int toInt(const std::string& str) WRT_PLUGINS_DEPRECATED;
 
-} // String
+/**
+ * Converts string to specified type.
+ *
+ * @tparam T Type to convert to.
+ * @param str String to convert.
+ * @return Converted value.
+ * @throw ConversionException If conversion fails.
+ *
+ * @remarks T must implement default constructor and an implementation
+ *          of input string operator for T must exist.
+ */
+template<typename T>
+T convertTo(const std::string& str)
+{
+    T result;
+    std::istringstream iss(str);
+    if (!(iss >> result)) {
+        Throw(ConversionException);
+    }
+    return std::move(result);
+}
+
+/**
+ * Converts a given value to string.
+ *
+ * @tparam T Type of the value to convert. It is deduced by the compiler.
+ * @param data Value to convert.
+ * @return Value converted to string.
+ * @throw ConversionException If operation fails.
+ *
+ * @remarks Output stream operator for type T must exist.
+ */
+template<typename T>
+std::string parse(const T& data)
+{
+    std::ostringstream oss;
+    if (!(oss << data))
+    {
+        Throw(ConversionException);
+    }
+    return oss.str();
+}
+
+}
+}
 }
-} // WrtDeviceApisCommon
 
-#endif // WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
+#endif