Loading...
Searching...
No Matches
Converter.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef DRAIN_CONVERTER
33#define DRAIN_CONVERTER
34
35// #include <map>
36// #include <list>
37// #include <iterator>
38#include <sstream>
39
40#include "Log.h"
41//#include "MapTools.h"
42//#include "Sprinter.h"
43// #include "StringTools.h"
44//#include "drain/EnumUtils.h"
45
46namespace drain {
47
49
52template <typename T>
53class Converter {
54public:
55
56 static
57 void convert(const T & value, std::string &s){
58 std::ostringstream sstr;
59 sstr << value;
60 if (!sstr.good()){
61 Logger(__FILE__, __FUNCTION__).warn("failed in reading ", value);
62 }
63 s.assign(sstr.str());
64 };
65
66 static
67 void convert(const std::string &s, T & value){
68 std::istringstream sstr(s);
69 sstr >> value;
70 if (sstr){
71 Logger(__FILE__, __FUNCTION__).warn(sstr.str(), " was unread from ", s);
72 }
73 };
74
75
76};
77
78// ChatGPT
79/*
80template <class Enum>
81struct enum_traits;
82
83template <class E>
84struct enum_traits<E> {
85
86 static std::string_view to_string(E e) {
87 return Enum<E>::dict.getKey(e);
88 }
89
90 static E from_string(std::string_view s) {
91 return Enum<E>::dict.getValue(s);
92 }
93};
94*/
95
96
97} // drain
98
99#endif // DRAIN_CONVERTER
100
101
Utility class with static conversions.
Definition Converter.h:53
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Definition DataSelector.cpp:1277