fmatvec  0.0.0
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 #include <vector>
37 
38 namespace fmatvec {
39 
45 template<typename T>
46 std::string toString(const T& value, int precision=0) {
47  std::stringstream str;
48  str.precision(precision==0?std::numeric_limits<T>::digits10+1:(precision<0?std::numeric_limits<T>::digits10+precision:precision));
49  str<<value;
50  return str.str();
51 }
52 
54 template<int N> using StringLiteral = char[N];
55 template<int N>
56 std::string toString(const StringLiteral<N>& value) {
57  return value;
58 }
59 
65 template<class T>
66 std::string toString(const std::vector<T>& value, int precision=0) {
67  std::ostringstream oss;
68  oss.precision(precision==0?std::numeric_limits<T>::digits10+1:(precision<0?std::numeric_limits<T>::digits10+precision:precision));
69  for(auto ele=value.begin(); ele!=value.end(); ++ele)
70  oss<<(ele==value.begin()?"[":"; ")<< *ele;
71  oss<<"]";
72  return oss.str();
73 }
74 
80 template<class T>
81 std::string toString(const std::vector<std::vector<T>>& value, int precision=0) {
82  std::ostringstream oss;
83  oss.precision(precision==0?std::numeric_limits<T>::digits10+1:(precision<0?std::numeric_limits<T>::digits10+precision:precision));
84  for(auto row=value.begin(); row!=value.end(); ++row)
85  for(auto ele=row->begin(); ele!=row->end(); ++ele)
86  oss<<(row==value.begin() && ele==row->begin()?"[":(ele==row->begin()?"; ":", "))<< *ele;
87  oss<<"]";
88  return oss.str();
89 }
90 
91 }
92 
93 #endif
char[N] StringLiteral
Definition: toString.h:54
Namespace fmatvec.
Definition: _memory.cc:28
std::string toString(const T &value, int precision=0)
Definition: toString.h:46