Stride Reference Manual  - generated for commit 9643b11
CSVRow.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include "util/StringUtils.h"
24 
25 #include <boost/lexical_cast.hpp>
26 #include <iosfwd>
27 
28 namespace stride {
29 namespace util {
30 
31 class CSV;
32 
36 class CSVRow
37 {
38 public:
40  CSVRow(const CSV* parent, const std::vector<std::string>& values);
41 
42  CSVRow(const CSVRow&) = default;
43 
44  CSVRow& operator=(const CSVRow&) = default;
45 
47  template <typename T = std::string>
48  T GetValue(size_t index) const;
49 
51  template <typename T = std::string>
52  T GetValue(const std::string& label) const;
53 
55  bool operator==(const CSVRow& other) const;
56 
58  friend std::ostream& operator<<(std::ostream& os, const CSVRow& row);
59 
60 protected:
61  const CSV* m_parent;
62  std::vector<std::string> m_values;
63 };
64 
73 template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
74 inline T safe_cast(const std::string& val)
75 {
76  if (std::is_floating_point<T>::value) {
77  return boost::numeric_cast<T>(boost::lexical_cast<double>(val));
78  }
79  return boost::numeric_cast<T>(boost::lexical_cast<long long>(val));
80 }
81 
89 template <typename T, std::enable_if_t<!std::is_arithmetic<T>::value, int> = 0>
90 inline T safe_cast(const std::string& val)
91 {
92  return boost::lexical_cast<T>(val);
93 }
94 
96 template <>
97 std::string CSVRow::GetValue<std::string>(size_t index) const;
98 
100 template <>
101 std::string CSVRow::GetValue<std::string>(const std::string& label) const;
102 
104 template <typename T>
105 inline T CSVRow::GetValue(size_t index) const
106 {
107  return safe_cast<T>(GetValue<std::string>(index));
108 }
109 
111 template <typename T>
112 inline T CSVRow::GetValue(const std::string& label) const
113 {
114  return safe_cast<T>(GetValue<std::string>(label));
115 }
116 
117 } // namespace util
118 } // namespace stride
Row in CSV file.
Definition: CSVRow.h:36
bool operator==(const CSVRow &other) const
Compare operator.
Definition: CSVRow.cpp:70
std::vector< std::string > m_values
Definition: CSVRow.h:62
T GetValue(size_t index) const
Get value at index. When T is specified, StringUtils are used to try to convert the value to type T...
Definition: CSVRow.h:105
friend std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Print to stream.
Definition: CSVRow.cpp:72
T safe_cast(const std::string &val)
Converts a string to an arithmetic type, in a safe manner.
Definition: CSVRow.h:74
CSVRow(const CSV *parent, const std::vector< std::string > &values)
CSVRow initialized with values. Should no be called by user code. CSV has convenience functions...
Definition: CSVRow.cpp:41
const CSV * m_parent
Definition: CSVRow.h:61
CSVRow & operator=(const CSVRow &)=default
Miscellaneous string utilities.
A collection of CSVRow elements.
Definition: CSV.h:46
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28