issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / cds / details / aligned_type.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_DETAILS_ALIGNED_TYPE_H
4 #define CDSLIB_DETAILS_ALIGNED_TYPE_H
5
6 #include <cds/details/defs.h>
7
8 namespace cds { namespace details {
9
10     /// Aligned type
11     /**
12         This meta-algorithm solves compiler problem when you need to declare a type \p T with alignment
13         equal to another type alignment. For example, the following declaration produces an error in Microsoft Visual Studio 2008 compiler:
14         \code
15             typedef double  my_double;
16             typedef __declspec(align( __alignof(my_double) )) int   aligned_int;
17         \endcode
18         In MS VS, the __declspec(align(N)) construction requires that N must be a integer constant (1, 2, 4 and so on)
19         but not an integer constant expression.
20
21         The result of this meta-algo is a type \p aligned_type<T,Alignment>::type that is \p T aligned by \p Alignment.
22         For example, with \p aligned_type the prevoius example will not generate an error:
23         \code
24             typedef double  my_double;
25             typedef typename cds::details::aligned_type<int, __alignof(my_double)>::type   aligned_int;
26         \endcode
27         and result of this declaration is equivalent to
28         \code
29             typedef __declspec(align(8)) int   aligned_int;
30         \endcode
31
32         The \p Alignment template parameter must be a constant expression and its result must be power of two.
33         The maximum of its value is 1024.
34
35         See also \ref align_as
36     */
37     template <typename T, size_t Alignment>
38     struct aligned_type
39 #ifdef CDS_DOXYGEN_INVOKED
40         {}
41 #endif
42 ;
43
44     //@cond none
45 #   define CDS_ALIGNED_TYPE_impl(nAlign) template <typename T> struct aligned_type<T,nAlign> { typedef CDS_TYPE_ALIGNMENT(nAlign) T type; }
46     CDS_ALIGNED_TYPE_impl(1);
47     CDS_ALIGNED_TYPE_impl(2);
48     CDS_ALIGNED_TYPE_impl(4);
49     CDS_ALIGNED_TYPE_impl(8);
50     CDS_ALIGNED_TYPE_impl(16);
51     CDS_ALIGNED_TYPE_impl(32);
52     CDS_ALIGNED_TYPE_impl(64);
53     CDS_ALIGNED_TYPE_impl(128);
54     CDS_ALIGNED_TYPE_impl(256);
55     CDS_ALIGNED_TYPE_impl(512);
56     CDS_ALIGNED_TYPE_impl(1024);
57 #   undef CDS_ALIGNED_TYPE_impl
58     //@endcond
59
60     /** Alignment by example
61
62         This meta-algo is similar to \ref aligned_type <T, alignof(AlignAs)>.
63
64         For example, the following code
65         \code
66         typedef typename cds::details::align_as<int, double>::type   aligned_int;
67         \endcode
68         declares type \p aligned_int which is \p int aligned like \p double.
69
70         See also: \ref aligned_type
71     */
72     template <typename T, typename AlignAs>
73     struct align_as {
74         /// Result of meta-algo: type \p T aligned like type \p AlignAs
75         typedef typename aligned_type<T, alignof(AlignAs)>::type type;
76     };
77
78 }}  // namespace cds::details
79
80 #endif // #ifndef CDSLIB_DETAILS_ALIGNED_TYPE_H