All Classes Namespaces Functions Variables Typedefs Enumerations Pages
toString.h
1 /*
2  * Author: Markus Friedrich
3  *
4  * This file is free and unencumbered software released into the public domain.
5  *
6  * Anyone is free to copy, modify, publish, use, compile, sell, or
7  * distribute this software, either in source code form or as a compiled
8  * binary, for any purpose, commercial or non-commercial, and by any
9  * means.
10  *
11  * In jurisdictions that recognize copyright laws, the author or authors
12  * of this software dedicate any and all copyright interest in the
13  * software to the public domain. We make this dedication for the benefit
14  * of the public at large and to the detriment of our heirs and
15  * successors. We intend this dedication to be an overt act of
16  * relinquishment in perpetuity of all present and future rights to this
17  * software under copyright law.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * For more information, please refer to <http://unlicense.org/>
28  */
29 
30 #ifndef _TOSTRING_H_
31 #define _TOSTRING_H_
32 
33 #include <string>
34 #include <sstream>
35 #include <limits>
36 
37 namespace fmatvec {
38 
44 template<typename T>
45 std::string toString(const T& value, int precision=0) {
46  std::stringstream str;
47  str.precision(precision==0?std::numeric_limits<T>::digits10+1:(precision<0?std::numeric_limits<T>::digits10+precision:precision));
48  str<<value;
49  return str.str();
50 }
51 
53 template<int N> using StringLiteral = char[N];
54 template<int N>
55 std::string toString(const StringLiteral<N>& value) {
56  return value;
57 }
58 
64 template<class T>
65 std::string toString(const std::vector<T>& value, int precision=0) {
66  std::ostringstream oss;
67  oss.precision(precision==0?std::numeric_limits<T>::digits10+1:(precision<0?std::numeric_limits<T>::digits10+precision:precision));
68  for(auto ele=value.begin(); ele!=value.end(); ++ele)
69  oss<<(ele==value.begin()?"[":"; ")<< *ele;
70  oss<<"]";
71  return oss.str();
72 }
73 
79 template<class T>
80 std::string toString(const std::vector<std::vector<T>>& value, int precision=0) {
81  std::ostringstream oss;
82  oss.precision(precision==0?std::numeric_limits<T>::digits10+1:(precision<0?std::numeric_limits<T>::digits10+precision:precision));
83  for(auto row=value.begin(); row!=value.end(); ++row)
84  for(auto ele=row->begin(); ele!=row->end(); ++ele)
85  oss<<(row==value.begin() && ele==row->begin()?"[":(ele==row->begin()?"; ":", "))<< *ele;
86  oss<<"]";
87  return oss.str();
88 }
89 
90 }
91 
92 #endif
char[N] StringLiteral
Definition: toString.h:53
std::string toString(const T &value, int precision=0)
Definition: toString.h:45

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home