Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/test/example/named_param_example.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

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