Ticket #9020: kth.cpp

File kth.cpp, 1.9 KB (added by Shaun Marko <smarko@…>, 9 years ago)

Example program illustrating the issue.

Line 
1#include <iostream>
2#include <boost/gil/gil_all.hpp>
3using namespace boost;
4using namespace gil;
5
6//
7// Do-nothing dereference adaptor
8//
9template <typename View>
10struct deref_fn {
11 BOOST_STATIC_CONSTANT(bool, is_mutable=false);
12 typedef deref_fn<typename View::const_t> const_t;
13 typedef typename View::value_type value_type;
14 typedef typename View::reference reference;
15 typedef typename View::point_t argument_type;
16 typedef reference result_type;
17
18 deref_fn() {;}
19 deref_fn(const View& v) : mView(v) { ; }
20 result_type operator()(const argument_type& p) const {
21 return mView(p);
22 }
23
24 View mView;
25};
26
27typedef rgb8_planar_image_t image_t;
28typedef image_t::view_t view_t;
29typedef deref_fn<view_t> fn_t;
30typedef virtual_2d_locator<fn_t, false> loc_t;
31typedef image_view<loc_t> virt_view_t;
32typedef kth_channel_view_type<0, virt_view_t>::type chan0_view_t;
33
34int main(int argc, char **argv) {
35 image_t img(4,4,rgb8_pixel_t(1,2,3),0);
36 view_t v(view(img));
37
38 fn_t fn(v);
39 loc_t loc(view_t::point_t(0,0), view_t::point_t(1,1), fn);
40 virt_view_t virtView(v.dimensions(), loc);
41
42 //
43 // Here detail::kth_channel_deref_fn<K,SrcP> is instantiated with
44 // SrcP = 'planar_pixel_reference<bits8&, rgb_t>'. The
45 // kth_channel_deref_fn then declares its result_type (and thereby
46 // the chan0_view_t:reference) to be a
47 // 'pixel<bits8&, gray_layout_t>&'
48 //
49 chan0_view_t chan0View(kth_channel_view<0,virt_view_t>(virtView));
50
51 assert((is_same<chan0_view_t::reference,
52 pixel<bits8&, gray_layout_t>& >::value));
53
54 //
55 // This will typically cause a bus error because the result_type
56 // of the deref adaptor is a 'pixel<bits8&, gray_layout_t>&'
57 // which gets initialized in the deref function by a
58 // 'pixel<bits8, gray_layout_t>&'
59 //
60 assert(chan0View(0,0) == gray8_pixel_t(1));
61 return 0;
62}