Nektar++
ParseUtils.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ParseUtils.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: This file contains various parsing utilities, primarily used
32 // by SpatialDomains to process input files.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
37 #include <boost/core/ignore_unused.hpp>
38 #include <boost/spirit/include/qi_auto.hpp>
39 #include <boost/spirit/include/qi_core.hpp>
40 #include <sstream>
41 
42 namespace qi = boost::spirit::qi;
43 namespace fusion = boost::fusion;
44 
45 namespace Nektar
46 {
47 
48 /**
49  * @brief Helper functors for holding a vector of numbers to be parsed by
50  * boost::spirit.
51  *
52  * @see ParseUtils::GenerateSeqVector
53  */
54 template <typename T> struct PushBackFunctor
55 {
56  PushBackFunctor(std::vector<T> &in) : m_vec(in)
57  {
58  }
59 
60  /**
61  * @brief Pushes back values onto #m_vec as given by @p num.
62  */
63  void operator()(T num) const
64  {
65  m_vec.push_back(num);
66  }
67 
68  /**
69  * @brief Pushes back values onto #m_vec between the range supplied by @p
70  * num. Valid for only integer types.
71  */
72  void operator()(fusion::vector<T, T> num) const
73  {
74  static_assert(std::is_integral<T>::value, "Integer type required.");
75  for (T i = fusion::at_c<0>(num); i <= fusion::at_c<1>(num); ++i)
76  {
77  m_vec.push_back(i);
78  }
79  }
80 
81 private:
82  /// Storage vector that will hold parsed variables from boost::spirit.
83  std::vector<T> &m_vec;
84 };
85 
86 /**
87  * @brief Takes a comma-separated compressed string and converts it to entries
88  * in a vector.
89  *
90  * This routine is the inverse of ParseUtils::GenerateSeqString. For example,
91  *
92  * std::string input = "1-4,6-8,5,2,3";
93  * std::vector<unsigned int> output;
94  * ParseUtils::GenerateSeqString(input, output);
95  *
96  * produces an `output` vector with the entries `{1,2,3,4,6,7,8,5,2,3}`.
97  *
98  * @param str Input CSV string of unsigned integers.
99  * @param out Output vector.
100  *
101  * @see ParseUtils::GenerateSeqString
102  */
103 bool ParseUtils::GenerateSeqVector(const std::string &str,
104  std::vector<unsigned int> &out)
105 {
106  PushBackFunctor<unsigned int> f1(out), f2(out);
107 
108  auto it = str.begin();
109  bool success = qi::phrase_parse(
110  it, str.end(),
111  ((qi::uint_ >> '-' >> qi::uint_)[f2] | qi::uint_[f1]) % ',',
112  qi::ascii::space);
113 
114  return success && it == str.end();
115 }
116 
117 /**
118  * @brief Takes a comma-separated string and converts it to entries in a vector.
119  *
120  * This routine splits up a comma-separated string and returns a vector with the
121  * entries. Template specialisations should be defined in this file (and not in
122  * the header file) as the use of boost::spirit::qi makes compilation times
123  * quite slow.
124  *
125  * @param str Input CSV string.
126  * @param out Output vector.
127  */
128 template <typename T>
129 bool ParseUtils::GenerateVector(const std::string &str, std::vector<T> &out)
130 {
131  auto it = str.begin();
132  bool success =
133  qi::phrase_parse(it, str.end(), qi::auto_ % ',', qi::ascii::space, out);
134  return success && it == str.end();
135 }
136 
137 template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<int>(
138  const std::string &str, std::vector<int> &out);
139 template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<long>(
140  const std::string &str, std::vector<long> &out);
141 template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<unsigned int>(
142  const std::string &str, std::vector<unsigned int> &out);
143 template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<double>(
144  const std::string &str, std::vector<double> &out);
145 template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<float>(
146  const std::string &str, std::vector<float> &out);
147 
148 /**
149  * @brief Specialised version of ParseUtils::GenerateVector for std::string.
150  *
151  * This routine specialises for the std::string data type as this type is not
152  * supported by boost::spirit::qi::auto_.
153  */
154 template <>
156  const std::string &str, std::vector<std::string> &out)
157 {
158  auto it = str.begin();
159  bool success = qi::phrase_parse(it, str.end(), +~qi::char_(",") % ',',
160  qi::ascii::space, out);
161  return success && it == str.end();
162 }
163 
164 } // namespace Nektar
#define LIB_UTILITIES_EXPORT
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:129
static bool GenerateSeqVector(const std::string &str, std::vector< unsigned int > &out)
Takes a comma-separated compressed string and converts it to entries in a vector.
Definition: ParseUtils.cpp:103
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
Helper functors for holding a vector of numbers to be parsed by boost::spirit.
Definition: ParseUtils.cpp:55
void operator()(T num) const
Pushes back values onto m_vec as given by num.
Definition: ParseUtils.cpp:63
void operator()(fusion::vector< T, T > num) const
Pushes back values onto m_vec between the range supplied by num. Valid for only integer types.
Definition: ParseUtils.cpp:72
std::vector< T > & m_vec
Storage vector that will hold parsed variables from boost::spirit.
Definition: ParseUtils.cpp:83
PushBackFunctor(std::vector< T > &in)
Definition: ParseUtils.cpp:56