Generic Image Library: dynamic_at_c.hpp Source File

###  | #  |

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

dynamic_at_c.hpp
Go to the documentation of this file.
1 /*
2 Copyright 2005-2007 Adobe Systems Incorporated
3
4 Use, modification and distribution are subject to the Boost Software License,
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt).
7
8 See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10
11 /*************************************************************************************************/
12
13 #ifndef GIL_DYNAMIC_AT_C_HPP
14 #define GIL_DYNAMIC_AT_C_HPP
15
16 #include "../../gil_config.hpp"
17 #include <cassert>
18 #include <stdexcept>
19 #include <boost/mpl/at.hpp>
20 #include <boost/mpl/size.hpp>
21
22
31
32 namespace boost { namespace gil {
33
34 #define GIL_AT_C_VALUE(z, N, text) mpl::at_c<IntTypes,S+N>::type::value,
35 #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
36
37 #define GIL_AT_C_LOOKUP(z, NUM, text) \
38 template<std::size_t S> \
39 struct at_c_fn<S,NUM> { \
40 template <typename IntTypes, typename ValueType> inline \
41 static ValueType apply(std::size_t index) { \
42 static ValueType table[] = { \
43 BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY) \
44 }; \
45 return table[index]; \
46 } \
47 };
48
49 namespace detail {
50 namespace at_c {
51 template <std::size_t START, std::size_t NUM> struct at_c_fn;
52 BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
53
54 template <std::size_t QUOT> struct at_c_impl;
55
56 template <>
57 struct at_c_impl<0> {
58 template <typename IntTypes, typename ValueType> inline
59 static ValueType apply(std::size_t index) {
60 return at_c_fn<0,mpl::size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
61 }
62 };
63
64 template <>
65 struct at_c_impl<1> {
66 template <typename IntTypes, typename ValueType> inline
67 static ValueType apply(std::size_t index) {
68 const std::size_t SIZE=mpl::size<IntTypes>::value;
69 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
70 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
71 case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
72 case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
73 };
74 throw;
75 }
76 };
77
78 template <>
79 struct at_c_impl<2> {
80 template <typename IntTypes, typename ValueType> inline
81 static ValueType apply(std::size_t index) {
82 const std::size_t SIZE=mpl::size<IntTypes>::value;
83 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
84 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
85 case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
86 case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
87 case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
88 };
89 throw;
90 }
91 };
92
93 template <>
94 struct at_c_impl<3> {
95 template <typename IntTypes, typename ValueType> inline
96 static ValueType apply(std::size_t index) {
97 const std::size_t SIZE=mpl::size<IntTypes>::value;
98 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
99 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
100 case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
101 case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
102 case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
103 case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
104 };
105 throw;
106 }
107 };
108 }
109 }
110
117
118 template <typename IntTypes, typename ValueType> inline
119 ValueType at_c(std::size_t index) {
120 const std::size_t Size=mpl::size<IntTypes>::value;
121 return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
122 }
123
124 #undef GIL_AT_C_VALUE
125 #undef GIL_DYNAMIC_AT_C_LIMIT
126 #undef GIL_AT_C_LOOKUP
127
128 } } // namespace boost::gil
129
130 #endif