• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
11 using namespace boost::nfp;
12 
13 ////////////////////////////////////////////////////////////////
14 // Example:
15 
16 #include <iostream>
17 #include <boost/shared_ptr.hpp>
18 
19 namespace 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>
foo1(char const * n,ValueType v,int i)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>
foo(Params const & 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>
boo(Params const & 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>
doo(Params const & 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>
moo1(T * t)57   void moo1( T* t )
58   {
59       std::cout << "non shared " << *t << std::endl;
60   }
61 
62   template<typename T>
moo1(boost::shared_ptr<T> const & t)63   void moo1( boost::shared_ptr<T> const& t )
64   {
65       std::cout << "shared " << *t << std::endl;
66   }
67 
68   template<class Params>
moo(Params const & params)69   void moo(Params const& params)
70   {
71       moo1( params[instance] );
72   }
73 
74   template<class Params>
goo(Params const & params)75   void goo(Params const& params)
76   {
77       params[ref] = 6;
78   }
79 }
80 
main()81 int 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
121