| 1 | // |
| 2 | //======================================================================= |
| 3 | // Author: Jeremiah Willcock |
| 4 | // |
| 5 | // Copyright 2012, Trustees of Indiana University |
| 6 | // |
| 7 | // Distributed under the Boost Software License, Version 1.0. (See |
| 8 | // accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | //======================================================================= |
| 11 | // |
| 12 | |
| 13 | #include <boost/property_map/function_property_map.hpp> |
| 14 | #include <boost/concept/assert.hpp> |
| 15 | #include <boost/property_map/property_map.hpp> |
| 16 | #include <boost/core/lightweight_test.hpp> |
| 17 | #include <boost/static_assert.hpp> |
| 18 | |
| 19 | template <typename T> |
| 20 | struct add1 {typedef T result_type; T operator()(const T& x) const {return x + 1;}}; |
| 21 | |
| 22 | template <typename T> |
| 23 | struct add1_val {typedef T result_type; T operator()(T x) const {return x + 1;}}; |
| 24 | |
| 25 | template <typename T> |
| 26 | struct return_fixed_ref { |
| 27 | int* ptr; |
| 28 | return_fixed_ref(int* ptr): ptr(ptr) {} |
| 29 | typedef int& result_type; |
| 30 | int& operator()(const T&) const {return *ptr;} |
| 31 | }; |
| 32 | |
| 33 | int main() { |
| 34 | using namespace boost; |
| 35 | BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<function_property_map<add1<int>, int>, int>)); |
| 36 | BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<function_property_map<add1<int>, int, double>, int>)); |
| 37 | BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<function_property_map<add1_val<int>, int>, int>)); |
| 38 | BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<function_property_map<add1_val<int>, int, double>, int>)); |
| 39 | BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int>)); |
| 40 | BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int>)); |
| 41 | BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int>)); |
| 42 | BOOST_CONCEPT_ASSERT((LvaluePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int>)); |
| 43 | |
| 44 | BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<function_property_map<add1<int>, int> >::category, boost::readable_property_map_tag>::value)); |
| 45 | BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<function_property_map<add1_val<int>, int> >::category, boost::readable_property_map_tag>::value)); |
| 46 | BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<function_property_map<return_fixed_ref<int>, int> >::category, boost::lvalue_property_map_tag>::value)); |
| 47 | |
| 48 | BOOST_TEST(get(function_property_map<add1<int>, int>(), 3) == 4); |
| 49 | BOOST_TEST(get(function_property_map<add1<int>, int>(add1<int>()), 4) == 5); |
| 50 | BOOST_TEST(get(make_function_property_map<int>(add1<int>()), 5) == 6); |
| 51 | BOOST_TEST(get(function_property_map<add1_val<int>, int>(), 3) == 4); |
| 52 | BOOST_TEST(get(function_property_map<add1_val<int>, int>(add1_val<int>()), 4) == 5); |
| 53 | BOOST_TEST(get(make_function_property_map<int>(add1_val<int>()), 5) == 6); |
| 54 | int val; |
| 55 | const function_property_map<return_fixed_ref<int>, int> pm = return_fixed_ref<int>((&val)); |
| 56 | put(pa: pm, k: 1, v: 6); |
| 57 | BOOST_TEST(get(pm, 2) == 6); |
| 58 | BOOST_TEST((get(pm, 3) = 7) == 7); |
| 59 | BOOST_TEST(get(pm, 4) == 7); |
| 60 | const function_property_map<return_fixed_ref<int>, int> pm2 = pm; // Check shallow copying |
| 61 | BOOST_TEST(get(pm2, 5) == 7); |
| 62 | put(pa: pm2, k: 3, v: 1); |
| 63 | BOOST_TEST(get(pm, 1) == 1); |
| 64 | |
| 65 | return boost::report_errors(); |
| 66 | } |
| 67 | |