1%%%%%%%%%%%%%%%%%%%
2% XLiFE++ is an extended library of finite elements written in C++
3%     Copyright (C) 2014  Lunéville, Eric; Kielbasiewicz, Nicolas; Lafranche, Yvon; Nguyen, Manh-Ha; Chambeyron, Colin
4%
5%     This program is free software: you can redistribute it and/or modify
6%     it under the terms of the GNU General Public License as published by
7%     the Free Software Foundation, either version 3 of the License, or
8%     (at your option) any later version.
9%     This program is distributed in the hope that it will be useful,
10%     but WITHOUT ANY WARRANTY; without even the implied warranty of
11%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12%     GNU General Public License for more details.
13%     You should have received a copy of the GNU General Public License
14%     along with this program.  If not, see <http://www.gnu.org/licenses/>.
15%%%%%%%%%%%%%%%%%%%
16
17\section{Parameters management}
18
19It may be useful to have a data structure to store freely any kind of parameters (values, string,
20...) with a very simple interface for the end user. In particular, such structure could be
21attached to user functions (see chap. \ref{s.function}). This is achieved with two classes
22: the {\class Parameter} class which define one parameter and the {\class Parameters} class
23which manages a list of {\class Parameter} objects.
24
25\subsection{The {\classtitle Parameter} class}
26
27The aim of the {\class Parameter} class is to encapsulate in the same structure, a data of
28integer type, of real type, of complex type, of string type, of real vector/complex vector type, of real vector/complex matrix type or of pointer type (void pointer) with the
29capability to name the parameter. Thus, this class proposes as private members:
30\vspace{.1cm}
31\begin{lstlisting}
32class Parameter
33{private :
34  int i_;                  //!<to store int type data
35  real_t r_;               //!<to store real_t type data
36  complex_t c_;            //!<to store complex_t type data
37  String s_;               //!<to store string data
38  void * p_;               //!<to store pointer
39  String name_;            //!<parameter name
40  number_t type_;          //!<type of value
41  ...
42  }
43\end{lstlisting}
44\vspace{.2cm}
45It offers \\
46- a copy constructor and constructors associated to each managed type T (\emph{int, real\_t,
47complex\_t, string, char*, void *, real vector, complex vector, real matrix, complex matrix}), with optional naming:
48\vspace{.1cm}
49\begin{lstlisting}[]{}
50Parameter(const T, const String& nm = String(""));
51Parameter(const Parameter&, const String& nm = String(""));
52\end{lstlisting}
53\vspace{.2cm}
54- assignment operator = with specialized cases
55\vspace{.1cm}
56\begin{lstlisting}[]{}
57Parameter& operator=(const Parameter& p);
58Parameter& operator=(const T&);
59\end{lstlisting}
60\vspace{.2cm}
61The real/complex vector and real/complex matrix are managed using the void pointer p\_. More precisely, when a parameter is defined from a vector (or a matrix), for instance :
62\begin{lstlisting}[]{}
63Parameter par(std::vector<Real>(5,1.);
64\end{lstlisting}
65a copy of the given vector is allocated and its pointer is stored as a void pointer in member p\_. In assignment operator, the \emph{deletePointer()} member function is used to free memory previously allocated.\\
66\\
67The class provides\\
68- some access functions to data
69\begin{lstlisting}[]{}
70T get_s() const; //(T,s)=(int,i),(real_t,r),(complex_t,c),(String,s),(void *,p)
71                 //(T,s)=(std::vector<real_t>&,rv),(std::vector<complex_t>&,cv)
72                 //(T,s)=(Matrix<real_t>&,rm), (Matrix<complex_t>&,cm)
73number_t get_value_type(const String&);
74\end{lstlisting}
75\vspace{.2cm}
76- access to the parameter name and its type
77\vspace{.1cm}
78\begin{lstlisting}[]{}
79string name();              //acces to name
80void name(const String &);  //to change the name
81number_t type() const;      //access to type
82string_t typeName() const ; //access to type name
83 \end{lstlisting}
84\vspace{.2cm}
85- some print and read facilities
86\vspace{.1cm}
87\begin{lstlisting}[]{}
88ostream& operator<<(ostream&, const Parameter&);
89void print(const Parameter&);
90istream& operator>>(istream&, Parameter&);
91 \end{lstlisting}
92\vspace{.2cm}
93- some control functions
94\vspace{.1cm}
95\begin{lstlisting}[]{}
96void illegalOperation(const String& t1, const String& op, const String& t2) const;
97void illegalOperation2(const String& op, const String& t) const;
98void undefinedParameter(const String& t) const ;
99void illegalDivision() const;
100\end{lstlisting}
101\vspace{.2cm}
102When it has meaning, the operator +=, -=, *=, /=,+ , -, *, /, ==, !=, >, >=, <, <= are overloaded
103for parameters and it is possible to call standard mathematical functions (\emph{abs, conj,
104log,} ...) with a \emph{Parameter} object as argument. Obviously, these operations are not
105allowed for pointer or string type parameter.
106\\
107The pointer type parameter has been introduced to give to the user the possibility to store
108anything in a parameter (useful in the context of user functions). \textbf{It is the responsibility
109of the users to well manage their void pointers !}\\
110
111There are casting operators associated to the {\class Parameter} class. Some are explicit:
112\vspace{.1cm}
113\begin{lstlisting}[]{}
114real_t real(const Parameter& p);
115complex_t cmplx(const Parameter& p);
116String str(const Parameter& p);
117void * pointer(const Parameter& p);
118\end{lstlisting}
119\vspace{.2cm}
120and the others are implicit (cast operator):
121\vspace{.1cm}
122\begin{lstlisting}[]{}
123operator int();                    //!<cast to int
124operator real_t();                 //!<cast to real_t
125operator complex_t();              //!<cast to complex_t
126operator String();                 //!<cast to String
127operator void*();                  //!<cast to void*
128operator std::vector<real_t>();    //!< cast to real vector
129operator std::vector<complex_t>(); //!< cast to complex vector
130operator Matrix<real_t>();         //!< cast to real matrix
131operator Matrix<complex_t>();      //!< cast  to complex matrix
132\end{lstlisting}
133\vspace{.1cm}
134These cast operators allow to write such syntax:
135\begin{lstlisting}[]{}
136Parameter pi(Real(3.1415926), "the real pi");
137real_t x=pi;  //autocast
138\end{lstlisting}
139\vspace{.1cm}
140Unfortunately, casting a parameter to complex does not fully work:
141\vspace{.1cm}
142\begin{lstlisting}[]{}
143Parameter i(complex_t(0,1)); //i is a complex parameter
144complex_t j=i;               //is working
145j=i;                         //does not work !!!
146\end{lstlisting}
147The compiler cannot resolve an ambiguity on the operator =  : \emph{complex\_t}=\emph{real\_t}
148or \emph{complex\_t}=\emph{complex\_t}. The reason is that the autocast of the Parameter can
149produce either a \emph{complex\_t} or \emph{real\_t} ! It is not possible to omit the \emph{complex\_t}
150autocast version because then the form \emph{complex\_t j=i;} no longer works.  There is probably
151a solution but we did not find it !\\
152
153The Parameter class is very simple to use, for instance:
154\vspace{.1cm}
155\begin{lstlisting}[deletekeywords={[3] names}]
156Parameter k(3.1415926,"k");              //named real parameter
157Parameter i(complex(0.,1.),"i");         //named complex parameter
158Parameter s("my string");                //no named string parameter
159Parameter pv(RealVector(5,0.5),"vec 5");
160Parameter pam(Matrix<Real>(3,_hilbertMatrix),"hilbert matrix");
161Vector<String> mat_names(3);
162mat_names(1)="iron";mat_names(2)="aluminium";mat_names(1)="air";
163Parameter names(&mat_names,"mat_names"); //named pointer parameter
164real_t vk=get_r();
165Parameter ik=i*k;                        //operation on parameters
166ik.name("i*k");                          //rename parameter
167\end{lstlisting}
168\vspace{.2cm}
169
170\subsection{The {\classtitle Parameters} class: list of parameters}
171
172The {\class Parameters} class manages a list of {\class Parameter} objects. Using stream operator
173$>>$, it offers a friendly way to add a parameter in the list and an index or string index
174to access to the parameters of the list. It is based on the \emph{map} structure of the STL:
175\vspace{.1cm}
176\begin{lstlisting}[deletekeywords={[3] map}]
177class Parameters
178{private :
179  std::map<String,int> index_;      //!<associative list to manage string index
180  std::vector<Parameter*> list_;    //!<list of parameter's pointers
181}
182\end{lstlisting}
183The list of parameters pointers is stored in a vector thus providing a direct access to a parameter
184of the list. The associative map \emph{index\_} also provides a direct access to a parameter
185using a string index.\\
186
187This classes proposes constructors with a single parameter initialization with an optional name:
188\begin{lstlisting}[deletekeywords={[3] name}]
189Parameters();
190Parameters(Parameter&);
191Parameters(const void*, const string_t& nm = string_t(""));
192template<typename T>
193  Parameters(const T& t, const string_t& nm = string_t(""));
194\end{lstlisting}
195some list insertion facilities:
196\begin{lstlisting}[]{}
197void push(Parameter&);                    //!< append a parameter into list
198Parameters& operator<<(Parameter*);       //!< insert a parameter into list
199Parameters& operator<<(Parameter&);       //!< insert a parameter into list
200Parameters& operator<<(const Parameter&); //!< insert a parameter into list
201Parameters& operator<<(const void*);      //!< insert a void pointer into list
202template<typename T>
203  Parameters& operator<<(const T& t)     // insert a T value in list
204\end{lstlisting}
205some direct access operators (by index, string index or parameter):
206\begin{lstlisting}[]{}
207Parameter& operator()(unsigned int);
208Parameter& operator()(const String&);
209Parameter& operator()(const char *);
210Parameter& operator()(const Parameter&);
211\end{lstlisting}
212and some print utilities:
213\begin{lstlisting}[]{}
214void print(std::ostream&) const ;
215void print() const ;
216friend void print(const Parameters&);
217friend std::ostream& operator<<(std::ostream&, const Parameters&);
218\end{lstlisting}
219
220This parameter list is easy to use:
221\begin{lstlisting}[]{}
222Parameter k(3.1415926,"k");  //named real parameter
223RealVector v(10,0.);         //a real vector
224Parameter pv(v,"v1");        //associate vector v to a parameter pv
225Parameters params;           //new list of parameters
226params<<k<<"a string"<<pv;   //insert parameters or explicit data
227RealVector x=params("v1");   //retrieve vector v1
228\end{lstlisting}
229When you retrieve a parameter from the list, you have to know the type of the parameter to
230cast it in the right type. Note that the member function \emph{type()} of the Parameter class
231gives the parameter type.\\
232
233There exists a \emph{defaultParameters} global variable declared in the header \emph{config.h}
234and initialized as an empty list in the header \emph{globalScopeData.h}.
235
236\displayInfos{library=utils, header=Parameters.hpp, implementation=Parameters.cpp, test=test\_Parameters.cpp,
237header dep={config.h, String.hpp}}
238