named_param_example.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // (C) Copyright Gennadiy Rozental 2001-2014.
  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)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. // Library Code
  7. #include <boost/test/utils/named_params.hpp>
  8. using namespace boost::nfp;
  9. ////////////////////////////////////////////////////////////////
  10. // Example:
  11. #include <iostream>
  12. #include <boost/shared_ptr.hpp>
  13. namespace test {
  14. typed_keyword<char const*,struct name_t> name;
  15. typed_keyword<int,struct test_index_t> index;
  16. keyword<struct value_t,true> value;
  17. keyword<struct instance_t,true> instance;
  18. keyword<struct ref_t> ref;
  19. template<typename ValueType>
  20. void foo1( char const* n, ValueType v, int i )
  21. {
  22. std::cout << n << '[' << i << "]=" << v << std::endl;
  23. }
  24. template<class Params>
  25. void foo(Params const& params)
  26. {
  27. int i = params[index];
  28. foo1( params[name], params[value], i );
  29. }
  30. template<class Params>
  31. void boo(Params const& params)
  32. {
  33. foo1( params[name], params[value], params.has(index) ? params[index] : 0 );
  34. }
  35. template<class Params>
  36. void doo(Params const& params)
  37. {
  38. char const* nm;
  39. if( params.has(name) )
  40. nm = params[name];
  41. else
  42. nm = "abc";
  43. foo1( nm, params[value], params.has(index) ? params[index] : 0 );
  44. }
  45. template<typename T>
  46. void moo1( T* t )
  47. {
  48. std::cout << "non shared " << *t << std::endl;
  49. }
  50. template<typename T>
  51. void moo1( boost::shared_ptr<T> const& t )
  52. {
  53. std::cout << "shared " << *t << std::endl;
  54. }
  55. template<class Params>
  56. void moo(Params const& params)
  57. {
  58. moo1( params[instance] );
  59. }
  60. template<class Params>
  61. void goo(Params const& params)
  62. {
  63. params[ref] = 6;
  64. }
  65. }
  66. int main()
  67. {
  68. using test::foo;
  69. using test::boo;
  70. using test::moo;
  71. using test::doo;
  72. using test::goo;
  73. using test::name;
  74. using test::value;
  75. using test::index;
  76. using test::instance;
  77. using test::ref;
  78. foo(( name = "foo", index = 0, value = 2.5 ));
  79. foo(( value = 'a', index = 1, name = "foo" ));
  80. foo(( name = "foo", value = "abc", index = 1 ));
  81. try {
  82. foo(( name = "foo", value = "abc" ));
  83. }
  84. catch( nfp_detail::access_to_invalid_parameter const& ) {
  85. std::cout << "Got access_to_invalid_parameter" << std::endl;
  86. }
  87. boo(( name = "boo", value = "abc" ));
  88. boo(( name = "boo", index = 1, value = "abc" ));
  89. doo(( value = "abc" ));
  90. doo(( value = 1.56, name = "ytr" ));
  91. int i = 5;
  92. moo( instance = &i );
  93. moo( instance = boost::shared_ptr<float>( new float(1.2) ) );
  94. goo( ref = i );
  95. return 0;
  96. }
  97. // EOF