1 /*!
2  * \file   MFrontUtilities.hxx
3  * \brief This header declares a few helper function to write
4  * information in the mfront storage format
5  * \author Thomas Helfer
6  * \date   15 août 2015
7  * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights
8  * reserved.
9  * This project is publicly released under either the GNU GPL Licence
10  * or the CECILL-A licence. A copy of thoses licences are delivered
11  * with the sources of TFEL. CEA or EDF may also distribute this
12  * project under specific licensing conditions.
13  */
14 
15 #ifndef LIB_MFRONT_MFRONTUTILITIES_HXX
16 #define LIB_MFRONT_MFRONTUTILITIES_HXX
17 
18 #include <map>
19 #include <tuple>
20 #include <string>
21 #include <vector>
22 #include <iosfwd>
23 #include <initializer_list>
24 
25 #include "TFEL/Utilities/CxxTokenizer.hxx"
26 #include "MFront/MFrontConfig.hxx"
27 
28 namespace mfront {
29 
30   // forward declaration
31   struct VariableBoundsDescription;
32 
33   /*!
34    * \return the keys of a map
35    * \param[in] m: map
36    */
37   template <typename Value>
38   std::vector<std::string> getKeys(const std::map<std::string, Value>&);
39 
40   /*!
41    * \brief conditionnally insert a string into
42    * a vector of string : an element is inserted if not already
43    * present or empty.
44    * \param[out] d : destination
45    * \param[in]  v : value
46    */
47   MFRONT_VISIBILITY_EXPORT void insert_if(std::vector<std::string>&,
48                                           const std::string&);
49 
50   /*!
51    * \brief conditionnally insert a string into
52    * a vector of string : an element is inserted if not already
53    * present or has zero size or is nulltpr.
54    * \param[out] d : destination
55    * \param[in]  v : value
56    */
57   MFRONT_VISIBILITY_EXPORT void insert_if(std::vector<std::string>&,
58                                           const char* const);
59 
60   /*!
61    * \brief call insert_if on every element of a
62    * container.
63    * \pre Container::value_type must be convertible to a string
64    * \tparam Container : type of source
65    * \param[out] d : destination
66    * \param[in]  s : source
67    */
68   template <typename Container>
69   void insert_if(std::vector<std::string>&, const Container&);
70 
71   /*!
72    * \brief call insert_if on every element of an
73    * initializer_list.
74    * \pre Value must be convertible to a string
75    * \tparam Value : type of the objects of the initializer list
76    * \param[out] d : destination
77    * \param[in]  s : source
78    */
79   template <typename Value>
80   void insert_if(std::vector<std::string>&,
81                  const std::initializer_list<Value>&);
82   /*!
83    * \brief write a vector of string to the output stream
84    * \param[in] os : output stream
85    * \param[in] v  : vector of string
86    * \param[in] id : identifier
87    */
88   MFRONT_VISIBILITY_EXPORT void write(std::ostream&,
89                                       const std::vector<std::string>&,
90                                       const std::string&);
91   /*!
92    * \brief read an object of type T from a stream created by the
93    * CxxTokenizer class
94    * \tparam T         : type to be read
95    * \param[in,out] p  : current position in the stream
96    * \param[in] pe     : end of the stream
97    * \return the object read.
98    * If this function succeed, p points past the last token treated.
99    * If this function fails,   p shall be unchanged.
100    */
101   template <typename T>
102   T read(tfel::utilities::CxxTokenizer::const_iterator&,
103          const tfel::utilities::CxxTokenizer::const_iterator);
104   /*!
105    * \brief read an object of type T from a stream created by the
106    * CxxTokenizer class
107    * \tparam T         : type to be read
108    * \tparam v         : value read
109    * \param[in,out] p  : current position in the stream
110    * \param[in]     pe : end of the stream
111    * \return the object read.
112    * If this function succeed, p points past the last token treated.
113    * If this function fails,   p shall be unchanged.
114    */
115   template <typename T>
116   void read(T&,
117             tfel::utilities::CxxTokenizer::const_iterator&,
118             const tfel::utilities::CxxTokenizer::const_iterator);
119   /*!
120    * \brief read a string from a stream created by the CxxTokenizer
121    * class
122    * \param[in,out] p  : current position in the stream
123    * \param[in]     pe : end of the stream
124    * \return the string read.
125    * If this function succeed, p points past the last token treated.
126    * If this function fails,   p is unchanged.
127    */
128   template <>
129   MFRONT_VISIBILITY_EXPORT double read(
130       tfel::utilities::CxxTokenizer::const_iterator&,
131       const tfel::utilities::CxxTokenizer::const_iterator);
132   /*!
133    * \brief read a string from a stream created by the CxxTokenizer
134    * class
135    * \param[in,out] p  : current position in the stream
136    * \param[in]     pe : end of the stream
137    * \return the string read.
138    * If this function succeed, p points past the last token treated.
139    * If this function fails,   p is unchanged.
140    */
141   template <>
142   MFRONT_VISIBILITY_EXPORT std::string read(
143       tfel::utilities::CxxTokenizer::const_iterator&,
144       const tfel::utilities::CxxTokenizer::const_iterator);
145   /*!
146    * \brief read a vector of strings from a stream created by the
147    * CxxTokenizer class
148    * \param[in,out] p  : current position in the stream
149    * \param[in]     pe : end of the stream
150    * \return the vector read.
151    * If this function succeed, p points past the last token treated.
152    * If this function fails,   p is unchanged.
153    */
154   template <>
155   MFRONT_VISIBILITY_EXPORT std::vector<std::string> read(
156       tfel::utilities::CxxTokenizer::const_iterator&,
157       const tfel::utilities::CxxTokenizer::const_iterator);
158   /*!
159    * \brief read the bounds associated to a variable
160    * \param[in,out] p  : current position in the stream
161    * \param[in]     pe : end of the stream
162    * \return a tuple giving the variable name and the bounds
163    */
164   MFRONT_VISIBILITY_EXPORT
165   std::pair<std::string, VariableBoundsDescription> readVariableBounds(
166       tfel::utilities::CxxTokenizer::const_iterator& p,
167       const tfel::utilities::CxxTokenizer::const_iterator pe);
168   /*!
169    * \brief extract the name of a variable and an array position from a string.
170    * \param[in] n: variable name and array position
171    * \return a tuple giving the variable name, the fact that a
172    * position was read, the read position if any.
173    *
174    * For example, calling this function with the `A[10]` argument
175    * results in the following tuple: `{"A",true,10}`.
176    *
177    * With `B` as argument, the result is the following tuple:
178    * `{"B",false,0}`.
179    */
180   MFRONT_VISIBILITY_EXPORT
181   std::tuple<std::string, bool, unsigned short>
182   extractVariableNameAndArrayPosition(const std::string&);
183 
184 }  // end of mfront
185 
186 #include "MFront/MFrontUtilities.ixx"
187 
188 #endif /* LIB_MFRONT_MFRONTUTILITIES_HXX */
189