aimsdata  4.7.0
Neuroimaging data handling
getopt2.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_GETOPT_GETOPT2_H
35 #define AIMS_GETOPT_GETOPT2_H
36 
37 #include <aims/io/reader.h>
38 #include <aims/io/writer.h>
39 #include <aims/io/datatypecode.h>
41 
42 namespace carto {
43 
44 
46  // SingleOption< Reader<T> > //
48 
49 template <class T>
50 class SingleOption< aims::Reader<T> > : public OptionBase
51 {
53  bool _optional;
54  bool _valueRead;
55 
56 public:
57 
58  inline SingleOption( aims::Reader<T> &value, const std::string &name,
59  const std::string &info, bool optional );
60  virtual inline ~SingleOption();
61 
62  virtual bool recognizeName( const std::string & );
63  virtual bool feed( const std::string & );
64  virtual void check();
65  std::string info() const;
66 };
67 
68 //-----------------------------------------------------------------------------
69 template <class T>
70 inline
72  const std::string &name,
73  const std::string &info,
74  bool optional ) :
75  OptionBase( name, info ),
76  _value( value ),
77  _optional( optional ),
78  _valueRead( false )
79 {
80 }
81 
82 //-----------------------------------------------------------------------------
83 template <class T>
85 
86 //-----------------------------------------------------------------------------
87 template <class T> inline
88 bool SingleOption< aims::Reader<T> >::recognizeName( const std::string &n )
89 {
90  if( _nameInList( n ) ) {
91  if ( _valueRead ) {
92  throw unexpected_option( n );
93  }
94  return true;
95  }
96  return false;
97 }
98 
99 //-----------------------------------------------------------------------------
100 template <class T> inline
102 {
103  if ( ! _optional && ! _valueRead ) {
104  throw missing_option( name() );
105  }
106 }
107 
108 //-----------------------------------------------------------------------------
109 template <class T> inline
111 {
112  if ( _optional ) {
113  return std::string( "[ " ) + names() + " <file name (read only): " +
114  DataTypeCode<T>::name() + "> ]\n" + _info;
115  } else {
116  return names() + " <file name (read only): "
118  + ">\n" + _info;
119  }
120 }
121 
122 //-----------------------------------------------------------------------------
123 template <class T> inline
124 bool SingleOption< aims::Reader<T> >::feed( const std::string &value )
125 {
126  if ( _valueRead ) {
127  return false;
128  } else {
129  _value.setFileName( value );
130  _valueRead = true;
131  return true;
132  }
133 }
134 
135 
137  // SingleOption< Writer<T> > //
139 
140 template <class T>
141 class SingleOption< aims::Writer<T> > : public OptionBase
142 {
144  bool _optional;
145  bool _valueRead;
146 
147 public:
148 
149  inline SingleOption( aims::Writer<T> &value, const std::string &name,
150  const std::string &info, bool optional );
151  virtual inline ~SingleOption();
152 
153  virtual bool recognizeName( const std::string & );
154  virtual bool feed( const std::string & );
155  virtual void check();
156  std::string info() const;
157 };
158 
159 //-----------------------------------------------------------------------------
160 template <class T>
161 inline
163  const std::string &name,
164  const std::string &info,
165  bool optional ) :
166  OptionBase( name, info ),
167  _value( value ),
168  _optional( optional ),
169  _valueRead( false )
170 {
171 }
172 
173 //-----------------------------------------------------------------------------
174 template <class T>
176 
177 //-----------------------------------------------------------------------------
178 template <class T> inline
179 bool SingleOption< aims::Writer<T> >::recognizeName( const std::string &n )
180 {
181  if( _nameInList( n ) ) {
182  if ( _valueRead ) {
183  throw unexpected_option( n );
184  }
185  return true;
186  }
187  return false;
188 }
189 
190 //-----------------------------------------------------------------------------
191 template <class T> inline
193 {
194  if ( ! _optional && ! _valueRead ) {
195  throw missing_option( name() );
196  }
197 }
198 
199 //-----------------------------------------------------------------------------
200 template <class T> inline
202 {
203  if ( _optional ) {
204  return std::string( "[ " ) + names() + " <filename: "
205  + DataTypeCode<T>::name() + "> ]\n" + _info;
206  } else {
207  return names() + " <filename: "
208  + DataTypeCode<T>::name() + ">\n" + _info;
209  }
210 }
211 
212 //-----------------------------------------------------------------------------
213 template <class T> inline
214 bool SingleOption< aims::Writer<T> >::feed( const std::string &value )
215 {
216  if ( _valueRead ) {
217  return false;
218  } else {
219  _value.setFileName( value );
220  _valueRead = true;
221  return true;
222  }
223 }
224 
225 } // namespace carto
226 
227 namespace aims
228 {
229 
231  // AimsApplication //
233 
235 {
236 public:
237 
238  AimsApplication( int argc, const char **argv,
239  const std::string &documentation );
240  ~AimsApplication();
241 
242  static AimsApplication *globalApplication();
243 
244  virtual void initialize();
245 
247 };
248 
249 } // namespace aims
250 
251 
252 #endif // ifndef AIMS_GETOPT_GETOPT2_H
virtual bool recognizeName(const std::string &)
virtual ~SingleOption()
SingleOption(T &value, const std::string &name, const std::string &info, bool optional)
The class for EcatSino data write operation.
Definition: border.h:42
std::string _info
std::string name()
bool _nameInList(const std::string &name) const
virtual bool feed(const std::string &)
virtual void check()
Generic reader for every format of Aims object.
Definition: reader.h:69
std::string names() const
Generic writer for every format of Aims object.
Definition: writer.h:92
const std::string & name() const
std::string info() const