NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_tool.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_PUBLIC_H
2#define NE_STL_INCLUDENE_STL_PUBLIC_H
3
4#include "ne_stl_atomic.h"
5#include "ne_stl_build.h"
6#include "ne_stl_spinlock.h"
8
9namespace nstd {
11using handle_t = uint32_t;
12#ifndef INVALID_HANDLE
13#define INVALID_HANDLE ((nstd::handle_t)0)
14#endif
15namespace {
16struct def_max_handle_value : integral_constant<handle_t, 0x5FFFFFF7> {};
17struct def_base_handle_value : integral_constant<handle_t, 0x30ABCDEF> {};
18} // namespace
19template <typename TContext, handle_t _max_handle_value, handle_t _base_handle_value>
21 static handle_t generate() {
22 static handle_t _current_handle_value{_base_handle_value};
23 static nstd::spinlock _generat_handle_lock;
24 nstd::lock_guard __guard(_generat_handle_lock);
25 if (_current_handle_value >= _max_handle_value) {
26 _current_handle_value = _base_handle_value;
27 }
28 return ++_current_handle_value;
29 }
30};
33 template <typename TEnum>
34 static TEnum v(int32_t _i_v) {
35 union {
36 int32_t i_v;
37 TEnum e_v;
38 } u = {_i_v};
39 return u.e_v;
40 }
41 template <typename TEnum>
42 static int32_t v(TEnum _e_v) {
43 union {
44 TEnum e_v;
45 int32_t i_v;
46 } u = {_e_v};
47 return u.i_v;
48 }
49};
51template <typename TEnum, typename TIntegral, typename nstd::enable_if<std::is_integral<TIntegral>::value, std::nullptr_t>::type = nullptr>
52TEnum& enum_cast(const TIntegral& _i_v) {
53 union {
54 const TIntegral* i_v;
55 TEnum* e_v;
56 } u{&_i_v};
57 return *u.e_v;
58}
59template <typename TEnum>
60int32_t& enum_cast(TEnum& _e_v) {
61 union {
62 TEnum* e_v;
63 int32_t* i_v;
64 } u{&_e_v};
65 return *u.i_v;
66}
67
68template <typename T, size_t N>
69char (&ArraySizeHelper(T (&array)[N]))[N];
70#define arraysize(array) (sizeof(ArraySizeHelper(array)))
71} // namespace nstd
72
73#endif // !NE_STL_INCLUDENE_STL_PUBLIC_H
Definition ne_stl_spinlock.h:61
Definition ne_stl_spinlock.h:44
Definition ne_stl_any.h:7
char(& ArraySizeHelper(T(&array)[N]))[N]
TEnum & enum_cast(const TIntegral &_i_v)
Definition ne_stl_tool.h:52
uint32_t handle_t
Definition ne_stl_tool.h:11
_integral_constant< T, _value > integral_constant
Definition ne_stl_type_traits.h:15
Definition ne_stl_tool.h:32
static int32_t v(TEnum _e_v)
Definition ne_stl_tool.h:42
static TEnum v(int32_t _i_v)
Definition ne_stl_tool.h:34
Definition ne_stl_tool.h:10
Definition ne_stl_tool.h:20
static handle_t generate()
Definition ne_stl_tool.h:21