1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : formal parameter definition
13 // ***************************************************************************
15 #ifndef BOOST_TEST_UTILS_RUNTIME_PARAMETER_HPP
16 #define BOOST_TEST_UTILS_RUNTIME_PARAMETER_HPP
18 // Boost.Test Runtime parameters
19 #include <boost/test/utils/runtime/fwd.hpp>
20 #include <boost/test/utils/runtime/modifier.hpp>
21 #include <boost/test/utils/runtime/argument.hpp>
22 #include <boost/test/utils/runtime/argument_factory.hpp>
25 #include <boost/test/utils/class_properties.hpp>
26 #include <boost/test/utils/foreach.hpp>
29 #include <boost/function/function2.hpp>
30 #include <boost/algorithm/cxx11/all_of.hpp>
35 #include <boost/test/detail/suppress_warnings.hpp>
40 // ************************************************************************** //
41 // ************** runtime::parameter_cla_id ************** //
42 // ************************************************************************** //
43 // set of attributes identifying the parameter in the command line
45 struct parameter_cla_id {
46 parameter_cla_id( cstring prefix, cstring tag, cstring value_separator, bool negatable )
47 : m_prefix( prefix.begin(), prefix.size() )
48 , m_tag( tag.begin(), tag.size() )
49 , m_value_separator( value_separator.begin(), value_separator.size() )
50 , m_negatable( negatable )
53 BOOST_TEST_I_ASSRT( algorithm::all_of( m_prefix.begin(), m_prefix.end(), valid_prefix_char ),
54 invalid_cla_id() << "Parameter " << m_tag
55 << " has invalid characters in prefix." );
57 BOOST_TEST_I_ASSRT( algorithm::all_of( m_tag.begin(), m_tag.end(), valid_name_char ),
58 invalid_cla_id() << "Parameter " << m_tag
59 << " has invalid characters in name." );
61 BOOST_TEST_I_ASSRT( algorithm::all_of( m_value_separator.begin(), m_value_separator.end(), valid_separator_char ),
62 invalid_cla_id() << "Parameter " << m_tag
63 << " has invalid characters in value separator." );
66 static bool valid_prefix_char( char c )
68 return c == '-' || c == '/' ;
70 static bool valid_separator_char( char c )
72 return c == '=' || c == ':' || c == ' ' || c == '\0';
74 static bool valid_name_char( char c )
76 return std::isalnum( c ) || c == '+' || c == '_' || c == '?';
81 std::string m_value_separator;
85 typedef std::vector<parameter_cla_id> param_cla_ids;
87 // ************************************************************************** //
88 // ************** runtime::basic_param ************** //
89 // ************************************************************************** //
91 cstring const help_prefix("////");
94 typedef function<void (cstring)> callback_type;
95 typedef unit_test::readwrite_property<bool> bool_property;
98 /// Constructor with modifiers
99 template<typename Modifiers>
100 basic_param( cstring name, bool is_optional, bool is_repeatable, Modifiers const& m )
101 : p_name( name.begin(), name.end() )
102 , p_description( nfp::opt_get( m, description, std::string() ) )
103 , p_help( nfp::opt_get( m, runtime::help, std::string() ) )
104 , p_env_var( nfp::opt_get( m, env_var, std::string() ) )
105 , p_value_hint( nfp::opt_get( m, value_hint, std::string() ) )
106 , p_optional( is_optional )
107 , p_repeatable( is_repeatable )
108 , p_has_optional_value( m.has( optional_value ) )
109 , p_has_default_value( m.has( default_value ) || is_repeatable )
110 , p_callback( nfp::opt_get( m, callback, callback_type() ) )
112 add_cla_id( help_prefix, name, ":" );
116 virtual ~basic_param() {}
119 std::string const p_name;
120 std::string const p_description;
121 std::string const p_help;
122 std::string const p_env_var;
123 std::string const p_value_hint;
124 bool const p_optional;
125 bool const p_repeatable;
126 bool_property p_has_optional_value;
127 bool_property p_has_default_value;
128 callback_type const p_callback;
130 /// interface for cloning typed parameters
131 virtual basic_param_ptr clone() const = 0;
134 param_cla_ids const& cla_ids() const { return m_cla_ids; }
135 void add_cla_id( cstring prefix, cstring tag, cstring value_separator )
137 add_cla_id_impl( prefix, tag, value_separator, false, true );
140 /// interface for producing argument values for this parameter
141 virtual void produce_argument( cstring token, bool negative_form, arguments_store& store ) const = 0;
142 virtual void produce_default( arguments_store& store ) const = 0;
144 /// interfaces for help message reporting
145 virtual void usage( std::ostream& ostr, cstring negation_prefix_ )
147 ostr << "Parameter: " << p_name << '\n';
148 if( !p_description.empty() )
149 ostr << ' ' << p_description << '\n';
151 ostr << " Command line formats:\n";
152 BOOST_TEST_FOREACH( parameter_cla_id const&, id, cla_ids() ) {
153 if( id.m_prefix == help_prefix )
156 ostr << " " << id.m_prefix;
158 cla_name_help( ostr, id.m_tag, negation_prefix_ );
160 cla_name_help( ostr, id.m_tag, "" );
162 bool optional_value_ = false;
164 if( p_has_optional_value ) {
165 optional_value_ = true;
169 if( id.m_value_separator.empty() )
172 ostr << id.m_value_separator;
177 if( optional_value_ )
182 if( !p_env_var.empty() )
183 ostr << " Environment variable: " << p_env_var << '\n';
186 virtual void help( std::ostream& ostr, cstring negation_prefix_ )
188 usage( ostr, negation_prefix_ );
190 if( !p_help.empty() )
191 ostr << '\n' << p_help << '\n';
195 void add_cla_id_impl( cstring prefix,
197 cstring value_separator,
199 bool validate_value_separator )
201 BOOST_TEST_I_ASSRT( !tag.is_empty(),
202 invalid_cla_id() << "Parameter can't have an empty name." );
204 BOOST_TEST_I_ASSRT( !prefix.is_empty(),
205 invalid_cla_id() << "Parameter " << tag
206 << " can't have an empty prefix." );
208 BOOST_TEST_I_ASSRT( !value_separator.is_empty(),
209 invalid_cla_id() << "Parameter " << tag
210 << " can't have an empty value separator." );
212 // We trim value separator from all the spaces, so token end will indicate separator
213 value_separator.trim();
214 BOOST_TEST_I_ASSRT( !validate_value_separator || !value_separator.is_empty() || !p_has_optional_value,
215 invalid_cla_id() << "Parameter " << tag
216 << " with optional value attribute can't use space as value separator." );
218 m_cla_ids.push_back( parameter_cla_id( prefix, tag, value_separator, negatable ) );
222 /// interface for usage/help customization
223 virtual void cla_name_help( std::ostream& ostr, cstring cla_tag, cstring negation_prefix_ ) const
227 virtual void value_help( std::ostream& ostr ) const
229 if( p_value_hint.empty() )
232 ostr << p_value_hint;
236 param_cla_ids m_cla_ids;
239 // ************************************************************************** //
240 // ************** runtime::parameter ************** //
241 // ************************************************************************** //
244 OPTIONAL_PARAM, // 0-1
245 REQUIRED_PARAM, // exactly 1
246 REPEATABLE_PARAM // 0-N
249 //____________________________________________________________________________//
251 template<typename ValueType, args_amount a = runtime::OPTIONAL_PARAM, bool is_enum = false>
252 class parameter : public basic_param {
254 /// Constructor with modifiers
255 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
256 template<typename Modifiers=nfp::no_params_type>
257 parameter( cstring name, Modifiers const& m = nfp::no_params )
259 template<typename Modifiers>
260 parameter( cstring name, Modifiers const& m )
262 : basic_param( name, a != runtime::REQUIRED_PARAM, a == runtime::REPEATABLE_PARAM, m )
265 BOOST_TEST_I_ASSRT( !m.has( default_value ) || a == runtime::OPTIONAL_PARAM,
266 invalid_param_spec() << "Parameter " << name
267 << " is not optional and can't have default_value." );
269 BOOST_TEST_I_ASSRT( !m.has( optional_value ) || !this->p_repeatable,
270 invalid_param_spec() << "Parameter " << name
271 << " is repeatable and can't have optional_value." );
275 virtual basic_param_ptr clone() const
277 return basic_param_ptr( new parameter( *this ) );
279 virtual void produce_argument( cstring token, bool , arguments_store& store ) const
281 m_arg_factory.produce_argument( token, this->p_name, store );
283 virtual void produce_default( arguments_store& store ) const
285 if( !this->p_has_default_value )
288 m_arg_factory.produce_default( this->p_name, store );
292 typedef argument_factory<ValueType, is_enum, a == runtime::REPEATABLE_PARAM> factory_t;
293 factory_t m_arg_factory;
296 //____________________________________________________________________________//
298 class option : public basic_param {
300 /// Constructor with modifiers
301 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
302 template<typename Modifiers=nfp::no_params_type>
303 option( cstring name, Modifiers const& m = nfp::no_params )
305 template<typename Modifiers>
306 option( cstring name, Modifiers const& m )
308 : basic_param( name, true, false, nfp::opt_append( nfp::opt_append( m, optional_value = true), default_value = false) )
309 , m_arg_factory( nfp::opt_append( nfp::opt_append( m, optional_value = true), default_value = false) )
313 void add_cla_id( cstring prefix, cstring tag, cstring value_separator, bool negatable = false )
315 add_cla_id_impl( prefix, tag, value_separator, negatable, false );
319 virtual basic_param_ptr clone() const
321 return basic_param_ptr( new option( *this ) );
324 virtual void produce_argument( cstring token, bool negative_form, arguments_store& store ) const
327 store.set( p_name, !negative_form );
329 BOOST_TEST_I_ASSRT( !negative_form,
330 format_error( p_name ) << "Can't set value to negative form of the argument." );
332 m_arg_factory.produce_argument( token, p_name, store );
336 virtual void produce_default( arguments_store& store ) const
338 m_arg_factory.produce_default( p_name, store );
340 virtual void cla_name_help( std::ostream& ostr, cstring cla_tag, cstring negation_prefix_ ) const
342 if( negation_prefix_.is_empty() )
345 ostr << '[' << negation_prefix_ << ']' << cla_tag;
347 virtual void value_help( std::ostream& ostr ) const
349 if( p_value_hint.empty() )
350 ostr << "<boolean value>";
352 ostr << p_value_hint;
356 typedef argument_factory<bool, false, false> factory_t;
357 factory_t m_arg_factory;
360 //____________________________________________________________________________//
362 template<typename EnumType, args_amount a = runtime::OPTIONAL_PARAM>
363 class enum_parameter : public parameter<EnumType, a, true> {
364 typedef parameter<EnumType, a, true> base;
366 /// Constructor with modifiers
367 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
368 template<typename Modifiers=nfp::no_params_type>
369 enum_parameter( cstring name, Modifiers const& m = nfp::no_params )
371 template<typename Modifiers>
372 enum_parameter( cstring name, Modifiers const& m )
376 #ifdef BOOST_TEST_CLA_NEW_API
377 auto const& values = m[enum_values<EnumType>::value];
378 auto it = values.begin();
380 std::vector<std::pair<cstring, EnumType> > const& values = m[enum_values<EnumType>::value];
381 typename std::vector<std::pair<cstring, EnumType> >::const_iterator it = values.begin();
383 while( it != values.end() ) {
384 m_valid_names.push_back( it->first );
390 virtual basic_param_ptr clone() const
392 return basic_param_ptr( new enum_parameter( *this ) );
395 virtual void value_help( std::ostream& ostr ) const
397 if( this->p_value_hint.empty() ) {
400 BOOST_TEST_FOREACH( cstring, name, m_valid_names ) {
410 ostr << this->p_value_hint;
414 std::vector<cstring> m_valid_names;
418 // ************************************************************************** //
419 // ************** runtime::parameters_store ************** //
420 // ************************************************************************** //
422 class parameters_store {
424 bool operator()( cstring lh, cstring rh ) const
426 return std::lexicographical_compare(lh.begin(), lh.end(),
427 rh.begin(), rh.end());
432 typedef std::map<cstring, basic_param_ptr, lg_compare> storage_type;
434 /// Adds parameter into the persistent store
435 void add( basic_param const& in )
437 basic_param_ptr p = in.clone();
439 BOOST_TEST_I_ASSRT( m_parameters.insert( std::make_pair( cstring(p->p_name), p ) ).second,
440 duplicate_param() << "Parameter " << p->p_name << " is duplicate." );
443 /// Returns true if there is no parameters registered
444 bool is_empty() const { return m_parameters.empty(); }
445 /// Returns map of all the registered parameter
446 storage_type const& all() const { return m_parameters; }
447 /// Returns true if parameter with psecified name is registered
448 bool has( cstring name ) const
450 return m_parameters.find( name ) != m_parameters.end();
452 /// Returns map of all the registered parameter
453 basic_param_ptr get( cstring name ) const
455 storage_type::const_iterator const& found = m_parameters.find( name );
456 BOOST_TEST_I_ASSRT( found != m_parameters.end(),
457 unknown_param() << "Parameter " << name << " is unknown." );
459 return found->second;
464 storage_type m_parameters;
467 } // namespace runtime
470 #include <boost/test/detail/enable_warnings.hpp>
472 #endif // BOOST_TEST_UTILS_RUNTIME_PARAMETER_HPP