parameter.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  2. // Use, modification, and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 81913 $
  10. //
  11. // Description : defines model of formal parameter
  12. // ***************************************************************************
  13. #ifndef BOOST_RT_CLA_PARAMETER_HPP_062604GER
  14. #define BOOST_RT_CLA_PARAMETER_HPP_062604GER
  15. // Boost.Runtime.Parameter
  16. #include <boost/test/utils/runtime/config.hpp>
  17. #include <boost/test/utils/runtime/fwd.hpp>
  18. #include <boost/test/utils/runtime/parameter.hpp>
  19. #include <boost/test/utils/runtime/validation.hpp>
  20. #include <boost/test/utils/runtime/cla/fwd.hpp>
  21. #include <boost/test/utils/runtime/cla/modifier.hpp>
  22. #include <boost/test/utils/runtime/cla/iface/argument_factory.hpp>
  23. #include <boost/test/utils/runtime/cla/iface/id_policy.hpp>
  24. // Boost.Test
  25. #include <boost/test/utils/rtti.hpp>
  26. namespace boost {
  27. namespace BOOST_RT_PARAM_NAMESPACE {
  28. namespace cla {
  29. // ************************************************************************** //
  30. // ************** runtime::cla::parameter ************** //
  31. // ************************************************************************** //
  32. class parameter : public BOOST_RT_PARAM_NAMESPACE::parameter {
  33. public:
  34. parameter( identification_policy& ID, argument_factory& F, bool optional_value = false )
  35. : p_optional( false )
  36. , p_multiplicable( false )
  37. , p_optional_value( optional_value )
  38. , m_id_policy( ID )
  39. , m_arg_factory( F )
  40. {}
  41. // Destructor
  42. virtual ~parameter() {}
  43. unit_test::readwrite_property<bool> p_optional;
  44. unit_test::readwrite_property<bool> p_multiplicable;
  45. unit_test::readwrite_property<bool> p_optional_value;
  46. unit_test::readwrite_property<dstring> p_description;
  47. // parameter properties modification
  48. template<typename Modifier>
  49. void accept_modifier( Modifier const& m )
  50. {
  51. if( m.has( optional_m ) )
  52. p_optional.value = true;
  53. if( m.has( required_m ) )
  54. p_optional.value = false;
  55. if( m.has( multiplicable_m ) )
  56. p_multiplicable.value = true;
  57. if( m.has( optional_value_m ) )
  58. p_optional_value.value = true;
  59. nfp::optionally_assign( p_description.value, m, description );
  60. }
  61. // access methods
  62. bool has_argument() const { return m_actual_argument!=0; }
  63. argument const& actual_argument() const { return *m_actual_argument; }
  64. argument_ptr actual_argument() { return m_actual_argument; }
  65. // identification interface
  66. bool responds_to( cstring name ) const { return m_id_policy.responds_to( name ); }
  67. bool conflict_with( parameter const& p ) const
  68. {
  69. return (id_2_report() == p.id_2_report() && !id_2_report().is_empty()) ||
  70. m_id_policy.conflict_with( p.m_id_policy ) ||
  71. ((m_id_policy.p_type_id != p.m_id_policy.p_type_id) && p.m_id_policy.conflict_with( m_id_policy ));
  72. }
  73. cstring id_2_report() const { return m_id_policy.id_2_report(); }
  74. void usage_info( format_stream& fs ) const
  75. {
  76. m_id_policy.usage_info( fs );
  77. if( p_optional_value )
  78. fs << BOOST_RT_PARAM_LITERAL( '[' );
  79. m_arg_factory.argument_usage_info( fs );
  80. if( p_optional_value )
  81. fs << BOOST_RT_PARAM_LITERAL( ']' );
  82. }
  83. // argument match/produce based on input
  84. bool matching( argv_traverser& tr, bool primary ) const
  85. {
  86. return m_id_policy.matching( *this, tr, primary );
  87. }
  88. // argument production based on different source
  89. void produce_argument( argv_traverser& tr )
  90. {
  91. m_id_policy.matching( *this, tr, true ); // !! can we save this position somehow
  92. m_actual_argument = m_arg_factory.produce_using( *this, tr );
  93. }
  94. void produce_argument( parser const& p )
  95. {
  96. m_actual_argument = m_arg_factory.produce_using( *this, p );
  97. }
  98. private:
  99. //Data members
  100. identification_policy& m_id_policy;
  101. argument_factory& m_arg_factory;
  102. argument_ptr m_actual_argument;
  103. };
  104. //____________________________________________________________________________//
  105. template<typename Parameter,typename Modifier>
  106. inline shared_ptr<Parameter>
  107. operator-( shared_ptr<Parameter> p, Modifier const& m )
  108. {
  109. p->accept_modifier( m );
  110. return p;
  111. }
  112. //____________________________________________________________________________//
  113. } // namespace cla
  114. } // namespace BOOST_RT_PARAM_NAMESPACE
  115. } // namespace boost
  116. #endif // BOOST_RT_CLA_PARAMETER_HPP_062604GER