aimsdata 6.0.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>
41
42namespace carto {
43
44
46 // SingleOption< Reader<T> > //
48
49template <class T>
50class SingleOption< aims::Reader<T> > : public OptionBase
51{
52 aims::Reader<T> &_value;
53 bool _optional;
54 bool _valueRead;
55
56public:
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//-----------------------------------------------------------------------------
69template <class T>
70inline
72 const std::string &name,
73 const std::string &info,
74 bool optional ) :
76 _value( value ),
77 _optional( optional ),
78 _valueRead( false )
79{
80}
81
82//-----------------------------------------------------------------------------
83template <class T>
85
86//-----------------------------------------------------------------------------
87template <class T> inline
89{
90 if( _nameInList( n ) ) {
91 if ( _valueRead ) {
92 throw unexpected_option( n );
93 }
94 return true;
95 }
96 return false;
97}
98
99//-----------------------------------------------------------------------------
100template <class T> inline
102{
103 if ( ! _optional && ! _valueRead ) {
104 throw missing_option( name() );
105 }
106}
107
108//-----------------------------------------------------------------------------
109template <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//-----------------------------------------------------------------------------
123template <class T> inline
124bool 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
140template <class T>
141class SingleOption< aims::Writer<T> > : public OptionBase
142{
143 aims::Writer<T> &_value;
144 bool _optional;
145 bool _valueRead;
146
147public:
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//-----------------------------------------------------------------------------
160template <class T>
161inline
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//-----------------------------------------------------------------------------
174template <class T>
176
177//-----------------------------------------------------------------------------
178template <class T> inline
180{
181 if( _nameInList( n ) ) {
182 if ( _valueRead ) {
183 throw unexpected_option( n );
184 }
185 return true;
186 }
187 return false;
188}
189
190//-----------------------------------------------------------------------------
191template <class T> inline
193{
194 if ( ! _optional && ! _valueRead ) {
195 throw missing_option( name() );
196 }
197}
198
199//-----------------------------------------------------------------------------
200template <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//-----------------------------------------------------------------------------
213template <class T> inline
214bool 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
227namespace aims
228{
229
231 // AimsApplication //
233
235{
236public:
237
238 AimsApplication( int argc, const char **argv,
239 const std::string &documentation );
241
243
244 virtual void initialize();
245
247};
248
249} // namespace aims
250
251
252#endif // ifndef AIMS_GETOPT_GETOPT2_H
static AimsApplication * globalApplication()
AimsApplication(int argc, const char **argv, const std::string &documentation)
virtual void initialize()
Generic reader for every format of Aims object.
Definition reader.h:70
Generic writer for every format of Aims object.
Definition writer.h:94
std::string name()
std::string names() const
OptionBase(const std::string &name, const std::string &info)
std::string _info
const std::string & name() const
bool _nameInList(const std::string &name) const
virtual bool feed(const std::string &)
Definition getopt2.h:124
virtual bool recognizeName(const std::string &)
Definition getopt2.h:88
SingleOption(aims::Reader< T > &value, const std::string &name, const std::string &info, bool optional)
Definition getopt2.h:71
SingleOption(aims::Writer< T > &value, const std::string &name, const std::string &info, bool optional)
Definition getopt2.h:162
virtual bool recognizeName(const std::string &)
Definition getopt2.h:179
virtual bool feed(const std::string &)
Definition getopt2.h:214
std::string info() const
SingleOption(T &value, const std::string &name, const std::string &info, bool optional)
virtual bool recognizeName(const std::string &)
virtual ~SingleOption()
virtual void check()
virtual bool feed(const std::string &)
The class for EcatSino data write operation.