DGtal  1.0.0
Parameters.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
21  *
22  * @date 2018/06/26
23  *
24  * Implementation of inline methods defined in Parameters.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 // ----------------------------------------------------------------------------
39 inline
40 DGtal::ParameterValue::ParameterValue ( const std::string& v )
41  : myValue( v )
42 {}
43 // ----------------------------------------------------------------------------
44 template <typename X>
45 DGtal::ParameterValue::ParameterValue ( const X& v )
46  : myValue( detail::ValueConverter< X, std::string>::cast( v ) )
47 {}
48 // ----------------------------------------------------------------------------
49 template <typename T>
50 T
51 DGtal::ParameterValue::as() const
52 {
53  return detail::ValueConverter< std::string, T >::cast( myValue );
54 }
55 // ----------------------------------------------------------------------------
56 inline
57 void
58 DGtal::ParameterValue::selfDisplay ( std::ostream & out ) const
59 {
60  out << myValue;
61 }
62 
63 // ----------------------------------------------------------------------------
64 inline
65 DGtal::Parameters::Parameters( std::string name, ParameterValue pv )
66 {
67  operator()( name, pv );
68 }
69 // ----------------------------------------------------------------------------
70 inline
71 DGtal::Parameters::Self&
72 DGtal::Parameters::operator()( std::string name, ParameterValue pv )
73 {
74  myParameters[ name ] = pv;
75  return *this;
76 }
77 // ----------------------------------------------------------------------------
78 inline
79 DGtal::Parameters::Self&
80 DGtal::Parameters::operator()( const Parameters& params )
81 {
82  // if keys collide, values of params overrides this's values.
83  for(auto& it : params.myParameters)
84  myParameters[it.first] = it.second;
85  return *this;
86 }
87 // ----------------------------------------------------------------------------
88 inline
89 DGtal::Parameters::Self
90 DGtal::Parameters::operator|( const Self& other ) const
91 {
92  return Parameters( *this )( other );
93 }
94 
95 // ----------------------------------------------------------------------------
96 inline
97 DGtal::ParameterValue
98 DGtal::Parameters::operator[]( std::string name ) const
99 {
100  auto it = myParameters.find( name );
101  return ( it != myParameters.end() ) ? it->second : ParameterValue( "UNSET PARAMETER" );
102 }
103 // ----------------------------------------------------------------------------
104 inline
105 bool
106 DGtal::Parameters::count( std::string name ) const
107 {
108  auto it = myParameters.find( name );
109  return it != myParameters.end();
110 }
111 
112 // ----------------------------------------------------------------------------
113 inline
114 void
115 DGtal::Parameters::selfDisplay ( std::ostream & out ) const
116 {
117  out << "{ ";
118  for ( auto it = myParameters.begin(), itE = myParameters.end();
119  it != itE; )
120  {
121  out << it->first << ": " << it->second;
122  ++it;
123  if ( it != itE ) out << ", ";
124  }
125  out << " }";
126 }
127 // ----------------------------------------------------------------------------
128 inline
129 bool
130 DGtal::Parameters::isValid() const
131 {
132  return true;
133 }
134 
135 ///////////////////////////////////////////////////////////////////////////////
136 // ----------------------- Standard services ------------------------------
137 
138 
139 ///////////////////////////////////////////////////////////////////////////////
140 // Interface - public :
141 
142 ///////////////////////////////////////////////////////////////////////////////
143 // Implementation of inline functions //
144 
145 // ----------------------------------------------------------------------------
146 inline
147 std::ostream&
148 DGtal::operator<< ( std::ostream & out,
149  const ParameterValue & object )
150 {
151  object.selfDisplay( out );
152  return out;
153 }
154 
155 // ----------------------------------------------------------------------------
156 inline
157 std::ostream&
158 DGtal::operator<< ( std::ostream & out,
159  const Parameters & object )
160 {
161  object.selfDisplay( out );
162  return out;
163 }
164 
165 // //
166 ///////////////////////////////////////////////////////////////////////////////
167 
168