cbe7bb60754040ab6d238c32b4b539b700b99aba
[libcds.git] / cds / details / aligned_type.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSLIB_DETAILS_ALIGNED_TYPE_H
32 #define CDSLIB_DETAILS_ALIGNED_TYPE_H
33
34 #include <cds/details/defs.h>
35
36 namespace cds { namespace details {
37
38     /// Aligned type
39     /**
40         This meta-algorithm solves compiler problem when you need to declare a type \p T with alignment
41         equal to another type alignment. For example, the following declaration produces an error in Microsoft Visual Studio 2008 compiler:
42         \code
43             typedef double  my_double;
44             typedef __declspec(align( __alignof(my_double) )) int   aligned_int;
45         \endcode
46         In MS VS, the __declspec(align(N)) construction requires that N must be a integer constant (1, 2, 4 and so on)
47         but not an integer constant expression.
48
49         The result of this meta-algo is a type \p aligned_type<T,Alignment>::type that is \p T aligned by \p Alignment.
50         For example, with \p aligned_type the prevoius example will not generate an error:
51         \code
52             typedef double  my_double;
53             typedef typename cds::details::aligned_type<int, __alignof(my_double)>::type   aligned_int;
54         \endcode
55         and result of this declaration is equivalent to
56         \code
57             typedef __declspec(align(8)) int   aligned_int;
58         \endcode
59
60         The \p Alignment template parameter must be a constant expression and its result must be power of two.
61         The maximum of its value is 1024.
62
63         See also \ref align_as
64     */
65     template <typename T, size_t Alignment>
66     struct aligned_type
67 #ifdef CDS_DOXYGEN_INVOKED
68         {}
69 #endif
70 ;
71
72     //@cond none
73 #   define CDS_ALIGNED_TYPE_impl(nAlign) template <typename T> struct aligned_type<T,nAlign> { typedef CDS_TYPE_ALIGNMENT(nAlign) T type; }
74     CDS_ALIGNED_TYPE_impl(1);
75     CDS_ALIGNED_TYPE_impl(2);
76     CDS_ALIGNED_TYPE_impl(4);
77     CDS_ALIGNED_TYPE_impl(8);
78     CDS_ALIGNED_TYPE_impl(16);
79     CDS_ALIGNED_TYPE_impl(32);
80     CDS_ALIGNED_TYPE_impl(64);
81     CDS_ALIGNED_TYPE_impl(128);
82     CDS_ALIGNED_TYPE_impl(256);
83     CDS_ALIGNED_TYPE_impl(512);
84     CDS_ALIGNED_TYPE_impl(1024);
85 #   undef CDS_ALIGNED_TYPE_impl
86     //@endcond
87
88     /** Alignment by example
89
90         This meta-algo is similar to \ref aligned_type <T, alignof(AlignAs)>.
91
92         For example, the following code
93         \code
94         typedef typename cds::details::align_as<int, double>::type   aligned_int;
95         \endcode
96         declares type \p aligned_int which is \p int aligned like \p double.
97
98         See also: \ref aligned_type
99     */
100     template <typename T, typename AlignAs>
101     struct align_as {
102         /// Result of meta-algo: type \p T aligned like type \p AlignAs
103         typedef typename aligned_type<T, alignof(AlignAs)>::type type;
104     };
105
106 }}  // namespace cds::details
107
108 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_TYPE_H