remove \r
[extl.git] / extl / utility / element_num.h
blobad83e5925b4f1df2a60d6c495a6229b1405e9d8d
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: element_num.h
4 * Created: 08.02.02
5 * Updated: 08.04.14
7 * Brief: Gets the number of elements in the array
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_UTILITY_ELEMENT_NUM_H
13 #define EXTL_UTILITY_ELEMENT_NUM_H
15 /*!\file element_num.h
16 * \brief Gets the number of elements in the array
19 /* ///////////////////////////////////////////////////////////////////////
20 * Includes
22 #include "static_assert.h"
23 #include "../type/typedef.h"
25 /* ///////////////////////////////////////////////////////////////////////
26 * ::extl namespace
28 EXTL_BEGIN_NAMESPACE
30 /* //////////////////////////////////////////////////////////////////// */
31 #if defined(__cplusplus) && \
32 defined(EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT)
34 # if defined(EXTL_COMPILER_IS_GCC)
36 # pragma pack(push, 1)
37 template < e_uint_t N >
38 struct extl_array_size
40 e_byte_t c[N];
42 # pragma pack(pop)
44 template < typename_param_k T, e_uint_t N >
45 extl_array_size<N> const& extl_static_array_size(T (&)[N]);
47 /// EXTL_ELEMENT_NUM
48 # define EXTL_ELEMENT_NUM(ar) sizeof(EXTL_NS(extl_static_array_size)(ar))
49 # else
51 template < e_uint_t N >
52 struct extl_array_size
54 e_byte_t c[N];
57 /* Passing array by reference will not cause pointer decay */
58 template < typename_param_k T, e_uint_t N >
59 extl_array_size<N> const& extl_static_array_size(T (&)[N]);
61 /// EXTL_ELEMENT_NUM
62 # define EXTL_ELEMENT_NUM(ar) sizeof(EXTL_NS(extl_static_array_size)(ar).c)
63 # endif /* EXTL_COMPILER_IS_GCC */
64 #else
65 /*!\brief EXTL_ELEMENT_NUM does'nt support array-to-pointer decay
67 * int ar[10];
68 * int *p=ar; //or int *p=&ar[0];
69 * EXTL_ELEMENT_NUM(ar) == 10
70 * EXTL_ELEMENT_NUM(p) ?= 1
72 # define EXTL_ELEMENT_NUM(ar) (sizeof(ar) / sizeof(0[(ar)]))
73 #endif /* defined(__cplusplus) && \
74 defined(EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT) */
76 /* ///////////////////////////////////////////////////////////////////////
77 * Unit-testing
79 #ifdef EXTL_UTILITY_ELEMENT_NUM_TEST_ENABLE
80 # include "unit_test/element_num_test.h"
81 #endif
82 /* ///////////////////////////////////////////////////////////////////////
83 * ::extl namespace
85 EXTL_END_NAMESPACE
87 /* //////////////////////////////////////////////////////////////////// */
88 #endif /* EXTL_UTILITY_ELEMENT_NUM_H */
89 /* //////////////////////////////////////////////////////////////////// */