2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_DETAILS_ALIGNED_TYPE_H
32 #define CDSLIB_DETAILS_ALIGNED_TYPE_H
34 #include <cds/details/defs.h>
36 namespace cds { namespace details {
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:
43 typedef double my_double;
44 typedef __declspec(align( __alignof(my_double))) int aligned_int;
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.
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:
52 typedef double my_double;
53 typedef typename cds::details::aligned_type<int, __alignof(my_double)>::type aligned_int;
55 and result of this declaration is equivalent to
57 typedef __declspec(align(8)) int aligned_int;
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.
63 See also \ref align_as
65 template <typename T, size_t Alignment>
67 #ifdef CDS_DOXYGEN_INVOKED
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
88 /** Alignment by example
90 This meta-algo is similar to \ref aligned_type <T, alignof(AlignAs)>.
92 For example, the following code
94 typedef typename cds::details::align_as<int, double>::type aligned_int;
96 declares type \p aligned_int which is \p int aligned like \p double.
98 See also: \ref aligned_type
100 template <typename T, typename AlignAs>
102 /// Result of meta-algo: type \p T aligned like type \p AlignAs
103 typedef typename aligned_type<T, alignof(AlignAs)>::type type;
106 }} // namespace cds::details
108 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_TYPE_H