LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
param_map.h
Go to the documentation of this file.
1// -*-c++-*-
2
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa AKIYAMA
12
13 This code is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 *EndCopyright:
28 */
29
31
32#ifndef RCSC_PARAM_PARAM_MAP_H
33#define RCSC_PARAM_PARAM_MAP_H
34
35#include <boost/lexical_cast.hpp>
36
37#include <memory>
38#include <vector>
39#include <map>
40#include <string>
41#include <iostream>
42#include <cassert>
43
44namespace rcsc {
45
50struct NegateBool {
51 bool * ptr_;
52
57 explicit
58 NegateBool( bool * ptr )
59 : ptr_( ptr )
60 {
61 assert( ptr );
62 }
63
64private:
65 // not used
66 NegateBool() = delete;
67};
68
73struct BoolSwitch {
74 bool * ptr_;
75
80 explicit
81 BoolSwitch( bool * ptr )
82 : ptr_( ptr )
83 {
84 assert( ptr );
85 }
86
87private:
88 // not used
89 BoolSwitch() = delete;
90};
91
92
98 bool * ptr_;
99
104 explicit
105 NegateSwitch( bool * ptr )
106 : ptr_( ptr )
107 {
108 assert( ptr );
109 }
110
115 explicit
116 NegateSwitch( const BoolSwitch & value )
117 : ptr_( value.ptr_ )
118 {
119 assert( ptr_ );
120 }
121
122private:
123 // not used
124 NegateSwitch() = delete;
125};
126
127/*-------------------------------------------------------------------*/
133public:
134
136 typedef std::shared_ptr< ParamEntity > Ptr;
137
138private:
140 std::string M_long_name;
142 std::string M_short_name;
144 std::string M_description;
145
147 ParamEntity() = delete;
148
149protected:
150
157 ParamEntity( const std::string & long_name,
158 const std::string & short_name,
159 const char * description = "" )
160 : M_long_name( long_name ),
161 M_short_name( short_name ),
162 M_description( description )
163 { }
164
165public:
166
170 virtual
172 { }
173
178 const std::string & longName() const
179 {
180 return M_long_name;
181 }
182
187 const std::string & shortName() const
188 {
189 return M_short_name;
190 }
195 const std::string & description() const
196 {
197 return M_description;
198 }
199
204 std::string helpName() const;
205
210 virtual
211 bool isSwitch() const
212 {
213 return false;
214 }
215
220 virtual
221 bool analyze( const std::string & value_str ) = 0;
222
228 virtual
229 std::ostream & printFormat( std::ostream & os ) const;
230
236 virtual
237 std::ostream & printValue( std::ostream & os ) const = 0;
238
239};
240
241/*-------------------------------------------------------------------*/
246template < typename ValueType >
248 : public ParamEntity {
249public:
250 typedef ValueType Type;
251
252private:
254 Type * M_value_ptr;
255
256public:
257
265 ParamGeneric( const std::string & long_name,
266 const std::string & short_name,
267 Type * value_ptr,
268 const char * description = "" )
269 : ParamEntity( long_name, short_name, description ),
270 M_value_ptr( value_ptr )
271 {
272 assert( value_ptr );
273 }
274
280 bool analyze( const std::string & value_str )
281 {
282 try
283 {
284 *M_value_ptr = boost::lexical_cast< Type >( value_str );
285 return true;
286 }
287 catch ( boost::bad_lexical_cast & e )
288 {
289 std::cerr << e.what() << " [" << value_str << "]"
290 << std::endl;
291 return false;
292 }
293 }
294
295private:
296
297 template < typename T >
298 std::ostream & printValueImpl( std::ostream & os,
299 const T & value ) const
300 {
301 os << value;
302 return os;
303 }
304
305 std::ostream & printValueImpl( std::ostream & os,
306 const std::string & value ) const
307 {
308 os << '"' << value << '"';
309 return os;
310 }
311
312public:
318 std::ostream & printValue( std::ostream & os ) const
319 {
320 return printValueImpl( os, *M_value_ptr );
321 }
322
323};
324
325/*-------------------------------------------------------------------*/
330template <>
331class ParamGeneric< bool >
332 : public ParamEntity {
333private:
334 const bool M_negate;
335
337 bool * M_value_ptr;
338
339public:
340
348 ParamGeneric( const std::string & long_name,
349 const std::string & short_name,
350 bool * value_ptr,
351 const char * description = "" )
352 : ParamEntity( long_name, short_name, description ),
353 M_negate( false ),
354 M_value_ptr( value_ptr )
355 {
356 assert( value_ptr );
357 }
358
366 ParamGeneric( const std::string & long_name,
367 const std::string & short_name,
368 const NegateBool & value,
369 const char * description = "" )
370 : ParamEntity( long_name, short_name, description ),
371 M_negate( true ),
372 M_value_ptr( value.ptr_ )
373 {
374 assert( M_value_ptr );
375 }
376
382 bool analyze( const std::string & value_str );
383
389 std::ostream & printValue( std::ostream & os ) const;
390
391};
392
393/*-------------------------------------------------------------------*/
399 : public ParamEntity {
400private:
401 const bool M_negate;
402
404 bool * M_value_ptr;
405
406public:
407
415 ParamSwitch( const std::string & long_name,
416 const std::string & short_name,
417 bool * value_ptr,
418 const char * description = "" )
419 : ParamEntity( long_name, short_name, description ),
420 M_negate( false ),
421 M_value_ptr( value_ptr )
422 {
423 assert( value_ptr );
424 }
425
426
434 ParamSwitch( const std::string & long_name,
435 const std::string & short_name,
436 const NegateSwitch & value,
437 const char * description = "" )
438 : ParamEntity( long_name, short_name, description ),
439 M_negate( true ),
440 M_value_ptr( value.ptr_ )
441 {
442 assert( M_value_ptr );
443 }
444
449 virtual
450 bool isSwitch() const
451 {
452 return true;
453 }
454
460 bool analyze( const std::string & value_str );
461
467 std::ostream & printFormat( std::ostream & os ) const;
468
474 std::ostream & printValue( std::ostream & os ) const;
475
476};
477
478
479/*-------------------------------------------------------------------*/
484class ParamMap {
485private:
486
491 class Registrar {
492 private:
494 ParamMap & M_param_map;
496 Registrar() = delete;
497 public:
502 explicit
503 Registrar( ParamMap & pmap )
504 : M_param_map( pmap )
505 { }
506
515 template < typename ValueType >
516 Registrar & operator()( const std::string & long_name,
517 const std::string & short_name,
518 ValueType * value_ptr,
519 const char * description = "" )
520 {
521 if ( ! checkName( long_name, short_name ) )
522 {
523 M_param_map.M_valid = false;
524 return *this;
525 }
526
527 if ( ! value_ptr )
528 {
529 std::cerr << "***ERROR*** detected null pointer for the option "
530 << long_name << std::endl;
531 M_param_map.M_valid = false;
532 return *this;
533 }
534
536 short_name,
537 value_ptr,
538 description ) );
539 M_param_map.add( ptr );
540 return *this;
541 }
542
551 Registrar & operator()( const std::string & long_name,
552 const std::string & short_name,
553 const NegateBool & value,
554 const char * description = "" );
555
564 Registrar & operator()( const std::string & long_name,
565 const std::string & short_name,
566 const BoolSwitch & value,
567 const char * description = "" );
568
577 Registrar & operator()( const std::string & long_name,
578 const std::string & short_name,
579 const NegateSwitch & value,
580 const char * description = "" );
581
582 private:
583
588 bool checkName( const std::string & long_nam,
589 const std::string & short_name ) const;
590 };
591
593 bool M_valid;
594
596 Registrar M_registrar;
597
599 std::string M_group_name;
600
602 std::vector< ParamEntity::Ptr > M_parameters;
603
605 std::map< std::string, ParamEntity::Ptr > M_long_name_map;
606
608 std::map< std::string, ParamEntity::Ptr > M_short_name_map;
609
610
611 // no copyable
612 ParamMap( const ParamMap & );
613 ParamMap & operator=( const ParamMap & );
614
615public:
616
621 : M_valid( true ),
622 M_registrar( *this )
623 { }
624
629 explicit
630 ParamMap( const std::string & group_name )
631 : M_valid( true ),
632 M_registrar( *this ),
633 M_group_name( group_name )
634 { }
635
640 { }
641
646 bool isValid() const
647 {
648 return M_valid;
649 }
650
655 const std::string & groupName() const
656 {
657 return M_group_name;
658 }
659
664 const std::vector< ParamEntity::Ptr > & parameters() const
665 {
666 return M_parameters;
667 }
668
673 const std::map< std::string, ParamEntity::Ptr > & longNameMap() const
674 {
675 return M_long_name_map;
676 }
677
682 const std::map< std::string, ParamEntity::Ptr > & shortNameMap() const
683 {
684 return M_short_name_map;
685 }
686
691 Registrar & add()
692 {
693 return M_registrar;
694 }
695
700 Registrar & add( ParamEntity::Ptr param );
701
706 void remove( const std::string & long_name );
707
713 ParamEntity::Ptr findLongName( const std::string & long_name );
714
720 ParamEntity::Ptr findShortName( const std::string & short_name );
721
728 std::ostream & printHelp( std::ostream & os,
729 const bool with_default = true ) const;
730
736 std::ostream & printValues( std::ostream & os ) const;
737};
738
739}
740
741#endif
abstract parameter
Definition: param_map.h:132
const std::string & shortName() const
get long name of parameter
Definition: param_map.h:187
const std::string & description() const
get description message
Definition: param_map.h:195
virtual std::ostream & printFormat(std::ostream &os) const
print help name strings
virtual ~ParamEntity()
destructor as virtual method
Definition: param_map.h:171
virtual bool analyze(const std::string &value_str)=0
pure virtual method. analyze value string.
virtual std::ostream & printValue(std::ostream &os) const =0
pure virtual method. print value to stream
std::shared_ptr< ParamEntity > Ptr
ParamEntity smart pointer type.
Definition: param_map.h:136
virtual bool isSwitch() const
(virtual) check if this parameter is switch type or not.
Definition: param_map.h:211
ParamEntity(const std::string &long_name, const std::string &short_name, const char *description="")
construct with all arguments
Definition: param_map.h:157
std::string helpName() const
get the formatted name string for help messages
const std::string & longName() const
get long name of parameter
Definition: param_map.h:178
ParamGeneric(const std::string &long_name, const std::string &short_name, bool *value_ptr, const char *description="")
constructor
Definition: param_map.h:348
bool analyze(const std::string &value_str)
analyze value string and substitute it to variable.
ParamGeneric(const std::string &long_name, const std::string &short_name, const NegateBool &value, const char *description="")
constructor
Definition: param_map.h:366
std::ostream & printValue(std::ostream &os) const
print value to stream
generic parameter
Definition: param_map.h:248
ParamGeneric(const std::string &long_name, const std::string &short_name, Type *value_ptr, const char *description="")
constructor
Definition: param_map.h:265
bool analyze(const std::string &value_str)
analyze value string and substitute it to variable.
Definition: param_map.h:280
ValueType Type
value type alias
Definition: param_map.h:250
std::ostream & printValue(std::ostream &os) const
print value to stream
Definition: param_map.h:318
parameter container
Definition: param_map.h:484
Registrar & add()
get a parameter registrar
Definition: param_map.h:691
const std::vector< ParamEntity::Ptr > & parameters() const
get the container of all parameters
Definition: param_map.h:664
ParamMap()
default constructor. create registrer
Definition: param_map.h:620
std::ostream & printValues(std::ostream &os) const
output parameter name and value
const std::string & groupName() const
get the name of parameter group
Definition: param_map.h:655
~ParamMap()
destructor. nothing to do
Definition: param_map.h:639
ParamEntity::Ptr findLongName(const std::string &long_name)
get parameter entry that has the argument name
ParamMap(const std::string &group_name)
construct with option name string
Definition: param_map.h:630
const std::map< std::string, ParamEntity::Ptr > & longNameMap() const
get the long name parameter map
Definition: param_map.h:673
std::ostream & printHelp(std::ostream &os, const bool with_default=true) const
output parameter usage by command line option style
Registrar & add(ParamEntity::Ptr param)
add new parameter entry
const std::map< std::string, ParamEntity::Ptr > & shortNameMap() const
get the short name parameter map
Definition: param_map.h:682
bool isValid() const
check if all registered options are valid or not.
Definition: param_map.h:646
void remove(const std::string &long_name)
remove registered parameter pointer
ParamEntity::Ptr findShortName(const std::string &short_name)
get parameter entry that has the argument name
switch type parameter
Definition: param_map.h:399
std::ostream & printValue(std::ostream &os) const
print value to stream
std::ostream & printFormat(std::ostream &os) const
print usage format.
bool analyze(const std::string &value_str)
analyze value string and substitute it to variable.
ParamSwitch(const std::string &long_name, const std::string &short_name, bool *value_ptr, const char *description="")
constructor
Definition: param_map.h:415
ParamSwitch(const std::string &long_name, const std::string &short_name, const NegateSwitch &value, const char *description="")
constructor
Definition: param_map.h:434
virtual bool isSwitch() const
(virtual) check if this parameter is switch type or not.
Definition: param_map.h:450
wrapper for bool switch.
Definition: param_map.h:73
bool * ptr_
raw pointer to the parameter variable
Definition: param_map.h:74
BoolSwitch(bool *ptr)
constructor
Definition: param_map.h:81
negate wrapper.
Definition: param_map.h:50
bool * ptr_
raw pointer to the parameter variable
Definition: param_map.h:51
NegateBool(bool *ptr)
constructor
Definition: param_map.h:58
negate wrapper for bool switch.
Definition: param_map.h:97
NegateSwitch(const BoolSwitch &value)
constructor
Definition: param_map.h:116
bool * ptr_
pointer to the parameter variable.
Definition: param_map.h:98
NegateSwitch(bool *ptr)
constructor
Definition: param_map.h:105