├── .gitignore
├── LICENSE
├── README.md
├── build
├── build.js
├── clean.bat
├── codecheck.vcxproj
├── codecheck.vcxproj.filters
├── nixy.sln
├── nixycore.vcxproj
└── nixycore.vcxproj.filters
├── codecheck
├── dummy.cpp
├── main.cpp
├── nedmalloc
│ ├── malloc.c.h
│ ├── nedmalloc.c
│ └── nedmalloc.h
├── test_algorithm.h
├── test_bugfix.h
├── test_delegate.h
├── test_finalizer.h
├── test_head.h
├── test_memory.h
├── test_preprocessor.h
├── test_random.h
├── test_stream.h
├── test_string.h
├── test_thread.h
├── test_time.h
├── test_typemanip.h
└── test_utility.h
├── nixycore
├── algorithm
│ ├── algorithm.h
│ ├── assign.h
│ ├── container.h
│ ├── foreach.h
│ └── series.h
├── bugfix
│ ├── assert.h
│ ├── assert_detail.h
│ ├── bugfix.h
│ ├── noexcept.h
│ ├── output.h
│ └── trace.h
├── container
│ ├── any.h
│ ├── array.h
│ ├── container.h
│ ├── deque.h
│ ├── forward_list.h
│ ├── hash_map.h
│ ├── hash_set.h
│ ├── list.h
│ ├── map.h
│ ├── priority.h
│ ├── queue.h
│ ├── set.h
│ ├── stack.h
│ └── vector.h
├── delegate
│ ├── bind.h
│ ├── delegate.h
│ ├── function_traits.h
│ ├── functor.h
│ └── signal.h
├── finalizer
│ ├── finalizer.h
│ ├── gc.h
│ ├── holder.h
│ ├── ref_counter.h
│ └── scope_guard.h
├── general
│ ├── detail
│ │ ├── plat_detect_cc.hxx
│ │ ├── plat_detect_os.hxx
│ │ ├── plat_detect_pc.hxx
│ │ └── plat_detect_sp.hxx
│ ├── general.h
│ ├── global_functions.h
│ └── plat_detect.h
├── memory
│ ├── alloc.h
│ ├── alloc_model.h
│ ├── cache_pool.h
│ ├── center_heap.h
│ ├── construct.h
│ ├── default_alloc.h
│ ├── detail
│ │ └── skip_array.h
│ ├── fixed_pool.h
│ ├── mem_alloc.h
│ ├── mem_guard.h
│ ├── mem_leak.h
│ ├── mem_pool.h
│ ├── memory.h
│ ├── object_pool.h
│ ├── pointer.h
│ ├── std_alloc.h
│ └── unfixed_pool.h
├── pattern
│ ├── iterator.h
│ ├── pattern.h
│ ├── prototype.h
│ ├── singleton.h
│ └── trackable.h
├── preprocessor
│ ├── pp_arg.h
│ ├── pp_count.h
│ ├── pp_macros.h
│ ├── pp_mult.h
│ ├── pp_nest.h
│ ├── pp_repeat.h
│ └── preprocessor.h
├── random
│ ├── rand_mt19937.h
│ ├── rand_std.h
│ └── random.h
├── stream
│ ├── detail
│ │ ├── stream_buffer_in.hxx
│ │ └── stream_buffer_out.hxx
│ ├── printf_format.h
│ ├── stream.h
│ ├── stream_buffer.h
│ ├── stream_detail.h
│ ├── stream_ops.h
│ └── stream_wrap.h
├── string
│ ├── detail
│ │ └── string_ops_define.hxx
│ ├── string.h
│ ├── string_detail.h
│ └── transform.h
├── thread
│ ├── async.h
│ ├── atomic.h
│ ├── barrier.h
│ ├── blocking_queue.h
│ ├── condition.h
│ ├── detail
│ │ ├── barrier_gnuc.hxx
│ │ ├── barrier_msvc.hxx
│ │ ├── condition_linux.hxx
│ │ ├── condition_win.hxx
│ │ ├── interlocked_def_msvc.hxx
│ │ ├── interlocked_gnuc.hxx
│ │ ├── interlocked_msvc.hxx
│ │ ├── mutex_linux.hxx
│ │ ├── mutex_win.hxx
│ │ ├── thread_ops_define.hxx
│ │ ├── thread_ops_linux.hxx
│ │ ├── thread_ops_std.hxx
│ │ ├── thread_ops_win.hxx
│ │ ├── tls_ptr_linux.hxx
│ │ └── tls_ptr_win.hxx
│ ├── future.h
│ ├── interlocked.h
│ ├── lock_guard.h
│ ├── mutex.h
│ ├── promise.h
│ ├── semaphore.h
│ ├── spin_lock.h
│ ├── task.h
│ ├── thread.h
│ ├── thread_detail.h
│ ├── thread_model.h
│ ├── thread_ops.h
│ ├── thread_pool.h
│ ├── tls_ptr.h
│ └── waiter.h
├── time
│ ├── stopwatch.h
│ ├── tickcount.h
│ └── time.h
├── typemanip
│ ├── detail
│ │ └── typeof_msvc.hxx
│ ├── typebehavior.h
│ ├── typeconcept.h
│ ├── typedecay.h
│ ├── typedefs.h
│ ├── typedetect.h
│ ├── typelist.h
│ ├── typemanip.h
│ ├── typeof.h
│ ├── typequalifier.h
│ ├── typerelation.h
│ ├── typetools.h
│ └── typetraits.h
└── utility
│ ├── addressof.h
│ ├── alignof.h
│ ├── countof.h
│ ├── final.h
│ ├── forward.h
│ ├── initialize.h
│ ├── limit_of.h
│ ├── max_min.h
│ ├── noncopyable.h
│ ├── operator.h
│ ├── refer.h
│ ├── rvalue.h
│ ├── safe_bool.h
│ ├── tuple.h
│ ├── type_cast.h
│ ├── utility.h
│ └── valid.h
└── nixyx
├── config.cpp
├── config.h
├── config_base.h
├── config_ini.h
├── config_js.h
├── main.cpp
├── nixyx.pro
├── platform.cpp
├── platform.h
├── platform_base.h
├── platform_gcc.h
├── platform_vc.h
├── policy_vc10.h
├── policy_vc11.h
├── policy_vc12.h
├── policy_vc8.h
├── policy_vc9.h
├── policy_vc_base.h
├── solution.h
└── tools.h
/.gitignore:
--------------------------------------------------------------------------------
1 | # Object files
2 | *.o
3 | *.ko
4 |
5 | # Libraries
6 | *.lib
7 | *.a
8 |
9 | # Shared objects (inc. Windows DLLs)
10 | *.dll
11 | *.so
12 | *.so.*
13 | *.dylib
14 |
15 | # Executables
16 | *.exe
17 | *.out
18 | *.app
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The Nixy Library - A Lightweight C++ Library
2 | Copyright (c) 2013-2014, mutouyun (http://orzz.org). All rights reserved.
3 | https://code.google.com/p/nixy/
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # The Nixy Library
2 |
3 | 这是一个小巧灵活的 跨平台/编译器的 C++基础库
4 |
5 | * 目前支持的OS: Windows(32/64/CE), Linux(Desktop/ARM)
6 | * 目前支持的CC: VC(8.0+), gcc, clang
7 |
8 | ## nixyx
9 |
10 | 1. Nixy的构建系统, 使用 Qt 编写
11 | 2. 脚本可以使用ini和js, 其中ini不支持条件判断和循环(当然了,这货只是个ini), 仅用于最简单的情况
12 | 3. 目前支持生成: Makefile, Visual Studio项目文件(Format Version 9.00 以上)
13 |
14 | ## nixycore
15 |
16 | 1. Nixy的核心部分, 基于泛型思想搭建的C++库
17 | 2. 目标: 代码轻巧, 功能灵活, 使用方便
18 |
19 | - [x] bugfix - 用于简化调试和排错
20 | - [x] container - 各种泛型容器
21 | - [x] delegate - 委托,包括泛化仿函数、绑定器等
22 | - [x] finalizer - 资源管理,包括通用rc和gc实现等
23 | - [x] memory - 内存管理,包括内存池、通用内存分配器、rc智能指针等
24 | - [x] preprocessor - 预处理器,用于对编译器的预处理阶段编程,一般用于自动代码生成
25 | - [x] stream - 输入输出流,包括简单的format功能
26 | - [x] string - 字符串相关,包括字符编码处理、字符串存储等
27 | - [x] typemanip - 类型操作,用于对编译器的编译期编程,实现各种泛型技巧
28 | - [x] thread - 线程管理,包括原子类、各种线程同步对象、线程类等
29 | - [x] utility - 各种实用小工具
30 | - [ ] general - 全库的通用部分
31 | - [ ] algorithm - 提供一些基本的算法
32 | - [ ] file - 文件处理
33 | - [ ] pattern - 模式,包括一部分可以通用化的设计模式
34 | - [ ] random - 随机数生成器
35 | - [ ] time - 时间管理
36 |
--------------------------------------------------------------------------------
/build/clean.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | cd ..
4 |
5 | for /r %%a in (.) do (
6 | set list=%%a
7 | setlocal enabledelayedexpansion
8 | if "!list:~-8,6!"=="-Debug" rd /q /s "!list:~0,-2!"
9 | if "!list:~-10,8!"=="-Release" rd /q /s "!list:~0,-2!"
10 | endlocal
11 | )
12 |
13 | rd /q /s "build/!bin"
14 | rd /q /s "build/!tmp"
15 | rd /q /s "build/debug"
16 | rd /q /s "build/release"
17 |
18 | del /f /q /s /a *.user *.ncb *.suo *.sdf *.i
--------------------------------------------------------------------------------
/build/codecheck.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/build/nixy.sln:
--------------------------------------------------------------------------------
1 | Microsoft Visual Studio Solution File, Format Version 12.00
2 | # Visual Studio 14
3 | VisualStudioVersion = 14.0.24720.0
4 | MinimumVisualStudioVersion = 10.0.40219.1
5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "codecheck", "codecheck.vcxproj", "{E7486F48-C460-4688-8BA9-6A9A39B26F94}"
6 | ProjectSection(ProjectDependencies) = postProject
7 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED} = {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}
8 | EndProjectSection
9 | EndProject
10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "nixycore", "nixycore.vcxproj", "{23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}"
11 | EndProject
12 | Global
13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
14 | Debug|x64 = Debug|x64
15 | Debug|x86 = Debug|x86
16 | Release|x64 = Release|x64
17 | Release|x86 = Release|x86
18 | EndGlobalSection
19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
20 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Debug|x64.ActiveCfg = Debug|x64
21 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Debug|x64.Build.0 = Debug|x64
22 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Debug|x86.ActiveCfg = Debug|Win32
23 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Debug|x86.Build.0 = Debug|Win32
24 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Release|x64.ActiveCfg = Release|x64
25 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Release|x64.Build.0 = Release|x64
26 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Release|x86.ActiveCfg = Release|Win32
27 | {E7486F48-C460-4688-8BA9-6A9A39B26F94}.Release|x86.Build.0 = Release|Win32
28 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Debug|x64.ActiveCfg = Debug|x64
29 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Debug|x64.Build.0 = Debug|x64
30 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Debug|x86.ActiveCfg = Debug|Win32
31 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Debug|x86.Build.0 = Debug|Win32
32 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Release|x64.ActiveCfg = Release|x64
33 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Release|x64.Build.0 = Release|x64
34 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Release|x86.ActiveCfg = Release|Win32
35 | {23C0BEBC-BB9D-4A10-86BF-1AA06909A1ED}.Release|x86.Build.0 = Release|Win32
36 | EndGlobalSection
37 | GlobalSection(SolutionProperties) = preSolution
38 | HideSolutionNode = FALSE
39 | EndGlobalSection
40 | EndGlobal
41 |
--------------------------------------------------------------------------------
/codecheck/dummy.cpp:
--------------------------------------------------------------------------------
1 | //#include "nixycore/time/time.h"
2 | //#include "nixycore/typemanip/typemanip.h"
3 | //#include "nixycore/memory/memory.h"
4 | //#include "nixycore/thread/thread.h"
5 | #include "nixycore/container/container.h"
6 | #include "nixycore/delegate/delegate.h"
7 |
8 | void dummy(void)
9 | {
10 | }
11 |
--------------------------------------------------------------------------------
/codecheck/main.cpp:
--------------------------------------------------------------------------------
1 |
2 | //////////////////////////////////////////////////////////////////////////
3 | //#define _WIN32_WINNT 0x0601
4 | #include "test_head.h"
5 |
6 | //////////////////////////////////////////////////////////////////////////
7 |
8 | void dummy(void);
9 |
10 | int main(void)
11 | {
12 | cout << __cplusplus << endl;
13 | dummy();
14 | testAll();
15 | return 0;
16 | }
17 |
--------------------------------------------------------------------------------
/codecheck/test_bugfix.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/bugfix/bugfix.h"
4 |
5 | //////////////////////////////////////////////////////////////////////////
6 |
7 | void testAssert(void) nx_noexcept
8 | {
9 | TEST_CASE();
10 |
11 | bool b = true;
12 | int i = 1, j = 2;
13 | char what[] = "What's up";
14 |
15 | try
16 | {
17 | nx_assert(b = false)(i)(j)(what).msg("blabla").except(b);
18 | strout << b << endl;
19 | }
20 | catch(const nx::assert_context&)
21 | {
22 | strout << "catched assert_context!" << endl;
23 | }
24 | catch(bool b)
25 | {
26 | strout << "catched bool: " << b << endl;
27 | }
28 | catch(...)
29 | {
30 | strout << "What?!" << endl;
31 | }
32 | }
33 |
34 | //////////////////////////////////////////////////////////////////////////
35 |
36 | void testTrace(void)
37 | {
38 | TEST_CASE();
39 |
40 | nx_trace("I %x seiko")("love") << nx::endl;
41 | nx_trace()("seiko ") << 123 << nx::endl<2>;
42 | }
43 |
44 | //////////////////////////////////////////////////////////////////////////
45 |
46 | void testBugFix(void)
47 | {
48 | TEST_FUNCTION();
49 |
50 | testAssert();
51 | testTrace();
52 | }
53 |
--------------------------------------------------------------------------------
/codecheck/test_finalizer.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/finalizer/finalizer.h"
4 |
5 | //////////////////////////////////////////////////////////////////////////
6 |
7 | namespace test_scopeguard
8 | {
9 | void onScopeExit(void)
10 | {
11 | strout << NX__FUNCTION__ << endl;
12 | }
13 |
14 | void onSoleExit(void)
15 | {
16 | strout << NX__FUNCTION__ << endl;
17 | }
18 | }
19 |
20 | void testScopeGuard(void)
21 | {
22 | TEST_CASE();
23 |
24 | using namespace test_scopeguard;
25 |
26 | {
27 | nx_guard_scope(onScopeExit);
28 | static nx_guard_scope(onSoleExit);
29 |
30 | # ifdef NX_SP_CXX11_LAMBDA
31 | nx_guard_scope([]()
32 | {
33 | strout << NX__FUNCTION__ << endl;
34 | });
35 | # endif
36 | }
37 |
38 | strout << __FUNCTION__ << " ending..." << endl;
39 | }
40 |
41 | //////////////////////////////////////////////////////////////////////////
42 |
43 | namespace test_gc
44 | {
45 | template
46 | void new_dest(T* p)
47 | {
48 | if (!p) return;
49 | strout << NX__FUNCTION__ << " ->: ";
50 | delete p;
51 | }
52 |
53 | class A
54 | {
55 | public:
56 | int a_;
57 |
58 | public:
59 | A(int a = 0) : a_(a)
60 | { strout << "cont A " << a_ << endl; }
61 | ~A(void)
62 | { strout << "dest A " << a_ << endl; }
63 | };
64 |
65 | class B
66 | {
67 | public:
68 | A* p_;
69 |
70 | public:
71 | B(int a = 0) : p_(nx::nulptr)
72 | {
73 | nx_gc_scope() = this;
74 | gc(p_)(new A(a), new_dest);
75 | strout << "cont B " << p_->a_ << endl;
76 | }
77 | ~B(void)
78 | {
79 | strout << "dest B " << p_->a_ << endl;
80 | }
81 | };
82 |
83 | void func_scope(B*(& pd))
84 | {
85 | nx_gc_scope();
86 | gc(pd)(new B(4), new_dest);
87 | }
88 | }
89 |
90 | void testGC(void)
91 | {
92 | TEST_CASE();
93 |
94 | using namespace test_gc;
95 | {
96 | nx_gc_scope();
97 |
98 | A* pa = nx::nulptr;
99 | A* pb = nx::nulptr;
100 |
101 | gc(pa)(new A(1), new_dest);
102 | gc(pb) = nx::alloc(2);
103 | gc(pb) = pa;
104 |
105 | B* pd = nx::nulptr;
106 | gc(pd) = nx::alloc(3);
107 |
108 | strout << "...scope in..." << endl;
109 | func_scope(pd);
110 | strout << "...scope out..." << endl;
111 |
112 | gc(pd) = nx::alloc(5);
113 |
114 | strout << "...start dest..." << endl;
115 | }
116 | strout << endl << "Finish Scope" << endl << endl;
117 | {
118 | nx_gc_scope();
119 | B* NX_UNUSED pb = gc(nx::alloc(2));
120 | A* NX_UNUSED pa = gc(new A(1), new_dest);
121 | }
122 | }
123 |
124 | //////////////////////////////////////////////////////////////////////////
125 |
126 | void testFinalizer(void)
127 | {
128 | TEST_FUNCTION();
129 |
130 | //testScopeGuard();
131 | testGC();
132 | }
133 |
--------------------------------------------------------------------------------
/codecheck/test_head.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | //////////////////////////////////////////////////////////////////////////
4 |
5 | #ifndef CODECHECK_CUSTOM_STROUT
6 |
7 | #include
8 |
9 | using std::cout;
10 | using std::endl;
11 |
12 | #define strout cout
13 |
14 | #endif/*CODECHECK_CUSTOM_STROUT*/
15 |
16 | //////////////////////////////////////////////////////////////////////////
17 |
18 | #define TEST_FUNCTION(...) \
19 | strout << endl \
20 | << "==================================" << endl \
21 | << "\t" __VA_ARGS__ << __FUNCTION__ << endl \
22 | << "==================================" << endl
23 |
24 | #define TEST_CASE(...) \
25 | strout << endl \
26 | << "----------------------------------" << endl \
27 | << "\t" __VA_ARGS__ << __FUNCTION__ << " ->: " << endl \
28 | << "----------------------------------" << endl
29 |
30 | //////////////////////////////////////////////////////////////////////////
31 |
32 | //#define NX_SINGLE_THREAD
33 | //#define NX_NO_CXX11_ATOMIC
34 | //#define NX_NO_CXX11_RVALUE_REF
35 | //#define NX_NO_CXX11_PERFECT_FWD
36 | //#define NX_NO_CXX11_AUTO
37 |
38 | #include "test_preprocessor.h"
39 | #include "test_bugfix.h"
40 | #include "test_typemanip.h"
41 | #include "test_algorithm.h"
42 | #include "test_utility.h"
43 | #include "test_time.h"
44 | #include "test_random.h"
45 | #include "test_delegate.h"
46 | #include "test_thread.h"
47 | #include "test_memory.h"
48 | #include "test_finalizer.h"
49 | #include "test_string.h"
50 | #include "test_stream.h"
51 |
52 | //////////////////////////////////////////////////////////////////////////
53 |
54 | void testAll(void)
55 | {
56 | //testPreprocessor();
57 | //testBugFix();
58 | //testTypeManip();
59 | //testAlgorithm();
60 | testUtility();
61 | //testTime();
62 | //testRandom();
63 | //testDelegate();
64 | //testFinalizer();
65 | //testThread();
66 | //testMemory();
67 | //testString();
68 | //testStream();
69 |
70 | TEST_FUNCTION(<< "Finished ");
71 | }
72 |
--------------------------------------------------------------------------------
/codecheck/test_preprocessor.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/general/general.h"
4 | #include "nixycore/preprocessor/preprocessor.h"
5 |
6 | //////////////////////////////////////////////////////////////////////////
7 |
8 | void testPreprocessor(void)
9 | {
10 | TEST_FUNCTION();
11 |
12 | strout << NX_PP_STR(NX_PP_REPEAT(5, f, d1, d2)) << endl;
13 | strout << NX_PP_STR(NX_PP_NEST(3, func, last, nest, xx)) << endl;
14 |
15 | strout << NX_PP_STR(NX_PP_CLONE(6, c, ,)) << endl;
16 |
17 | strout << NX_PP_STR(NX_PP_REV(1, 2, 3, 4, 5, 6, 7, 8, 9)) << endl;
18 | strout << NX_PP_STR(NX_PP_LIMIT(3, a, b, c, d, e, f)) << endl;
19 |
20 | strout << NX_PP_STR(NX_PP_INC(5)) << endl;
21 | strout << NX_PP_STR(NX_PP_DEC(5)) << endl;
22 | strout << NX_PP_STR(NX_PP_ADD(3, 4)) << endl;
23 | strout << NX_PP_STR(NX_PP_SUB(5, 3)) << endl;
24 |
25 | strout << NX_PP_STR(NX_PP_LESS(3, 4, Yes, No)) << endl;
26 | strout << NX_PP_STR(NX_PP_MORE(3, 4, Yes, No)) << endl;
27 | strout << NX_PP_STR(NX_PP_EQUA(4, 4, Yes, No)) << endl;
28 |
29 | strout << NX_PP_STR(NX_PP_RECUR(9, f, nul, c)) << endl;
30 | strout << NX_PP_STR(NX_PP_ORDER(f, 1, 2, 3, 4, 5, 6, 7, 8, 9)) << endl;
31 | strout << NX_PP_STR(NX_PP_CALL(func)) << endl;
32 |
33 | strout << NX_PP_STR(NX_PP_PARAM(par, int, char, long, double)) << endl;
34 |
35 | strout << NX_PP_STR(NX_PP_TYPE_1(3, T)) << endl;
36 | strout << NX_PP_STR(NX_PP_TYPE_2(3, P, * par, = NULL)) << endl;
37 | strout << NX_PP_STR(NX_PP_TYPE_1(1, typename P)) << endl;
38 |
39 | //#define NX_REPEAT_2(f1, f2, ...) f1(1, __VA_ARGS__) f2(2, __VA_ARGS__)
40 | //#define NX_MULT_2(f) f(1) f(2)
41 | //
42 | //#define NX_TYPE_1_1(n, t1, ...) NX_PP_VA(NX_PP_JOIN(t1, n) __VA_ARGS__)
43 | //#define NX_TYPE_1_2(n, t1, ...) NX_PP_VA(, NX_PP_JOIN(t1, n) __VA_ARGS__)
44 | //
45 | //#define NX_RP(n) NX_REPEAT_2(NX_TYPE_1_1, NX_TYPE_1_2, T)
46 | //
47 | // NX_MULT_2(NX_RP)
48 | }
49 |
--------------------------------------------------------------------------------
/codecheck/test_random.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/random/random.h"
4 | #include "nixycore/utility/countof.h"
5 |
6 | //////////////////////////////////////////////////////////////////////////
7 |
8 | void testRandMT19937(void)
9 | {
10 | TEST_CASE();
11 |
12 | nx::random<> rdm(0, 10000);
13 | int save[10];
14 | int save_index = 0, save_counter[nx_countof(save)] = {0};
15 | char snm[6];
16 |
17 | FILE* fp = fopen("test_rand_mt19937.txt", "wb");
18 | for (int i = 0; i < 1000000; ++i)
19 | {
20 | int x = rdm.roll();
21 | int n = 0;
22 | for (; n < save_index; ++n)
23 | {
24 | if (save[n] == x)
25 | {
26 | save_counter[n] += 1;
27 | break;
28 | }
29 | }
30 | if (n >= save_index && save_index < (int)nx_countof(save))
31 | {
32 | save_counter[save_index] += 1;
33 | save[save_index++] = x;
34 | }
35 |
36 | sprintf(snm, "%d", x);
37 | for (unsigned int m = 0; (m < nx_countof(snm)) && snm[m]; ++m)
38 | fwrite(snm + m, sizeof(char), 1, fp);
39 | fwrite("\r\n", sizeof(char), 2, fp);
40 | }
41 | fclose(fp);
42 |
43 | for (unsigned int n = 0; n < nx_countof(save); ++n)
44 | strout << save[n] << "\t->: " << save_counter[n] << endl;
45 | }
46 |
47 | //////////////////////////////////////////////////////////////////////////
48 |
49 | void testRandom(void)
50 | {
51 | TEST_FUNCTION();
52 |
53 | testRandMT19937();
54 | }
55 |
--------------------------------------------------------------------------------
/codecheck/test_stream.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/string/string.h"
4 | #include "nixycore/stream/stream.h"
5 |
6 | //////////////////////////////////////////////////////////////////////////
7 |
8 | void testStreamDetail(void)
9 | {
10 | TEST_CASE();
11 |
12 | nx::string text;
13 | {
14 | nx::format(&text, L"pi: %x%x\ntest bool: %x and %x") << 3.1415 << "926" << true << false;
15 | }
16 | strout << text.to_local().c_str() << endl << endl;
17 | {
18 | nx::io(&text) << 3.1415 << L"926" << true << false;
19 | }
20 | strout << text.to_local().c_str() << endl << endl;
21 | {
22 | text = L"I think is true && -3.1415926 && I-love-Sego";
23 | bool is_what = false;
24 | float pi = 0;
25 | nx::string tmp;
26 | nx::format(&text, L"I think is %x && %x && %x") >> is_what >> pi >> tmp;
27 | strout << is_what << " " << pi << " " << tmp.to_local().c_str() << endl;
28 | }
29 | }
30 |
31 | //////////////////////////////////////////////////////////////////////////
32 |
33 | void testStreamOps(void)
34 | {
35 | TEST_CASE();
36 |
37 | nx::string text;
38 | nx::io(&text) << L"pi is about: " << nx::fmt("%.2f", 3.1415) << nx::endl<2>;
39 | strout << text.to_local().c_str() << endl;
40 | }
41 |
42 | //////////////////////////////////////////////////////////////////////////
43 |
44 | void testStream(void)
45 | {
46 | TEST_FUNCTION();
47 |
48 | testStreamDetail();
49 | testStreamOps();
50 | }
51 |
--------------------------------------------------------------------------------
/codecheck/test_string.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/string/string.h"
4 |
5 | //////////////////////////////////////////////////////////////////////////
6 |
7 | namespace test_string
8 | {
9 | const char* c = "Hello world 你好世界 こんにちわ、世界";
10 | }
11 |
12 | //////////////////////////////////////////////////////////////////////////
13 |
14 | void testTransform(void)
15 | {
16 | TEST_CASE();
17 |
18 | using namespace test_string;
19 |
20 | size_t n = nx::transform::local_to_utf(c);
21 | wchar_t* s = new wchar_t[n];
22 | n = nx::transform::local_to_utf(c, s, n);
23 | FILE* fp = fopen("test_transform_s.txt", "wb");
24 | fwrite(s, sizeof(wchar_t), n - 1, fp);
25 | fclose(fp);
26 | strout << "local_to_utf size: " << n << endl;
27 |
28 | n = nx::transform::utf_to_local(s);
29 | char* z = new char[n];
30 | n = nx::transform::utf_to_local(s, z, n);
31 | fp = fopen("test_transform_z.txt", "wb");
32 | fwrite(z, sizeof(char), n - 1, fp);
33 | fclose(fp);
34 | strout << "utf_to_local size: " << n << endl;
35 |
36 | //wchar_t* s = new wchar_t[n];
37 | //size_t m = nx::transform::utf(c, s);
38 | //strout << n << " " << m << endl;
39 | /*
40 | wstring s;
41 | size_t n; nx::wchar w;
42 | while (!!(n = nx::transform::utf(c, w)))
43 | {
44 | s.push_back(w);
45 | c += n;
46 | }*/
47 | }
48 |
49 | //////////////////////////////////////////////////////////////////////////
50 |
51 | void testStringDetail(void)
52 | {
53 | TEST_CASE();
54 |
55 | using namespace test_string;
56 |
57 | nx::string str('c');
58 |
59 | str = c;
60 | str = str + "\r\ncstring: 123123\r\n"
61 | + L"wstring: 一二三一二三\r\n"
62 | + 'c'
63 | + "\r\n";
64 | str += L'夏';
65 |
66 | FILE* fp = fopen("test_string.txt", "wb");
67 | fwrite(str.c_str(), sizeof(wchar_t), str.length(), fp);
68 | fclose(fp);
69 |
70 | nx::vector arr = str.split();
71 | nx_foreach(s, arr)
72 | strout << s.to_local().c_str() << endl;
73 | }
74 |
75 | //////////////////////////////////////////////////////////////////////////
76 |
77 | void testString(void)
78 | {
79 | TEST_FUNCTION();
80 |
81 | //testTransform();
82 | testStringDetail();
83 | }
84 |
--------------------------------------------------------------------------------
/codecheck/test_time.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "nixycore/time/time.h"
4 | #include "nixycore/thread/thread_ops.h"
5 |
6 | #include
7 |
8 | //////////////////////////////////////////////////////////////////////////
9 |
10 | #ifndef STOPWATCH_CHECKER
11 | #define STOPWATCH_CHECKER(op) \
12 | strout << "stopwatch ->: " << #op << endl; \
13 | sw.op(); \
14 | for(int i = 0; i < 5; ++i) \
15 | { \
16 | nx::thread_ops::sleep(100); \
17 | strout << "stopwatch ->: " << sw.value() << endl; \
18 | }
19 | #endif
20 |
21 | void testStopwatch(void)
22 | {
23 | TEST_CASE();
24 |
25 | strout << std::setiosflags(std::ios_base::showpoint);
26 | nx::stopwatch<> sw;
27 | STOPWATCH_CHECKER(start)
28 | STOPWATCH_CHECKER(pause)
29 | STOPWATCH_CHECKER(start)
30 | STOPWATCH_CHECKER(stop)
31 | STOPWATCH_CHECKER(start)
32 | STOPWATCH_CHECKER(start)
33 | strout << std::resetiosflags(std::ios_base::showpoint);
34 | }
35 |
36 | #undef STOPWATCH_CHECKER
37 | #undef STOPWATCH_SLEEP
38 |
39 | //////////////////////////////////////////////////////////////////////////
40 |
41 | void testTime(void)
42 | {
43 | TEST_FUNCTION();
44 |
45 | testStopwatch();
46 | }
47 |
--------------------------------------------------------------------------------
/nixycore/algorithm/foreach.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/algorithm/container.h"
11 |
12 | #include "nixycore/typemanip/typetools.h"
13 | #include "nixycore/typemanip/typetraits.h"
14 | #include "nixycore/typemanip/typeof.h"
15 |
16 | #include "nixycore/general/general.h"
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | #ifdef NX_SP_CXX11_RANGEFOR
23 | #define nx_foreach(var, col, ...) \
24 | for(auto var : col) \
25 | for(bool once__ = true; once__; once__ = false,##__VA_ARGS__)
26 | #else
27 | namespace private_foreach
28 | {
29 | template struct storage { typedef const T & type_t; };
30 | template struct storage { typedef T & type_t; };
31 |
32 | template
33 | struct helper
34 | {
35 | typedef typename traits::type_t type_t;
36 | typedef typename container_traits::ite_t ite_t;
37 | typedef typename container_traits::val_t val_t;
38 | typedef typename traits::forward_t deref_t;
39 | typedef typename storage::type_t storage_t;
40 |
41 | T set_; ite_t cur_, end_;
42 | bool mark_;
43 |
44 | helper(storage_t s)
45 | : set_(s)
46 | , cur_(nx::begin(set_))
47 | , end_(nx::end(set_))
48 | , mark_(false)
49 | {}
50 |
51 | inline void next(void) { ++cur_; }
52 | inline bool is_inside(void) { return (cur_ != end_); }
53 | inline bool go_next(void) { return (mark_ = !mark_); }
54 | };
55 |
56 | template
57 | inline helper contain(T & t) // lvalue
58 | {
59 | return helper(t);
60 | }
61 |
62 | template
63 | inline helper contain(T const & t) // rvalue
64 | {
65 | return helper(t);
66 | }
67 |
68 | template
69 | inline typename enable_if::ite_t>::value,
70 | typename helper::deref_t>::type_t deref(helper& hp)
71 | {
72 | return hp.cur_;
73 | }
74 |
75 | template
76 | inline typename enable_if::ite_t>::value,
77 | typename helper::deref_t>::type_t deref(helper& hp)
78 | {
79 | return *(hp.cur_);
80 | }
81 | }
82 |
83 | #define nx_foreach(var, col, ...) \
84 | for(nx_auto(hp__, nx::private_foreach::contain(col)); hp__.is_inside(); hp__.next(),##__VA_ARGS__) \
85 | for(nx_auto(var , nx::private_foreach::deref(hp__)) ; hp__.go_next();)
86 |
87 | #endif/*NX_SP_CXX11_RANGEFOR*/
88 |
89 | #define nx_forever(...) \
90 | for(__VA_ARGS__;;)
91 |
92 | //////////////////////////////////////////////////////////////////////////
93 | NX_END
94 | //////////////////////////////////////////////////////////////////////////
95 |
--------------------------------------------------------------------------------
/nixycore/bugfix/bugfix.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | #include "nixycore/bugfix/noexcept.h"
13 | #include "nixycore/bugfix/output.h"
14 | #include "nixycore/bugfix/assert_detail.h"
15 | #include "nixycore/bugfix/assert.h"
16 | #include "nixycore/bugfix/trace.h"
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 |
--------------------------------------------------------------------------------
/nixycore/bugfix/noexcept.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #include "nixycore/general/general.h"
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 | NX_BEG
12 | //////////////////////////////////////////////////////////////////////////
13 |
14 | #ifdef NX_SP_CXX11_NOEXCEPT
15 | #define nx_noexcept noexcept
16 | #else
17 | #define nx_noexcept throw()
18 | #endif
19 |
20 | //////////////////////////////////////////////////////////////////////////
21 | NX_END
22 | //////////////////////////////////////////////////////////////////////////
23 |
--------------------------------------------------------------------------------
/nixycore/bugfix/output.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/string/transform.h"
11 | #include "nixycore/utility/countof.h"
12 |
13 | #include "nixycore/general/general.h"
14 |
15 | // std::cout, std::cerr, std::clog
16 | #include
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | namespace use
23 | {
24 | struct std_cout
25 | {
26 | static void out(const char* str)
27 | {
28 | std::cout << str;
29 | }
30 | };
31 |
32 | struct std_cerr
33 | {
34 | static void out(const char* str)
35 | {
36 | # if defined(NX_OS_WINCE)
37 | nx::wchar wcs[MAX_PATH];
38 | transform::local_to_utf(str, wcs, nx_countof(wcs));
39 | ::OutputDebugStringW(wcs);
40 | # else
41 | # if defined(NX_OS_WIN)
42 | ::OutputDebugStringA(str);
43 | # endif
44 | std::cerr << str;
45 | # endif
46 | }
47 | };
48 |
49 | struct std_clog
50 | {
51 | static void out(const char* str)
52 | {
53 | std::clog << str;
54 | }
55 | };
56 | }
57 |
58 | template
59 | bool output(const char* fmt, ...)
60 | {
61 | if (!fmt) return false;
62 | bool ret = true;
63 | va_list args;
64 | va_start(args, fmt);
65 | #ifdef NX_OS_WINCE
66 | char buf[MAX_PATH];
67 | if (::_vsnprintf(buf, nx_countof(buf), fmt, args) > 0)
68 | {
69 | PolicyT::out(buf);
70 | }
71 | else ret = false;
72 | #else
73 | char* buf = static_cast(::malloc(::vsnprintf(NULL, 0, fmt, args) + 1));
74 | if (buf)
75 | {
76 | if (::vsprintf(buf, fmt, args) > 0)
77 | {
78 | PolicyT::out(buf);
79 | }
80 | else ret = false;
81 | ::free(buf);
82 | }
83 | else ret = false;
84 | #endif
85 | va_end(args);
86 | return ret;
87 | }
88 |
89 | //////////////////////////////////////////////////////////////////////////
90 | NX_END
91 | //////////////////////////////////////////////////////////////////////////
92 |
--------------------------------------------------------------------------------
/nixycore/bugfix/trace.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/bugfix/output.h"
11 |
12 | #include "nixycore/general/general.h"
13 | #include "nixycore/stream/stream.h"
14 | #include "nixycore/string/string.h"
15 |
16 | //////////////////////////////////////////////////////////////////////////
17 | NX_BEG
18 | //////////////////////////////////////////////////////////////////////////
19 |
20 | class trace : public nx::stream
21 | {
22 | typedef nx::stream base_t;
23 |
24 | nx::string str_;
25 |
26 | public:
27 | trace(void)
28 | : base_t(&str_)
29 | {}
30 |
31 | template
32 | trace(const S& fs)
33 | : base_t(&str_, fs)
34 | {}
35 |
36 | ~trace(void)
37 | {
38 | base_t::flush();
39 | nx::output(str_.to_local().c_str());
40 | }
41 | };
42 |
43 | namespace private_trace
44 | {
45 | class maker
46 | {
47 | const nx::string::value_type* str_;
48 |
49 | public:
50 | maker(const nx::string::value_type* str)
51 | : str_(str)
52 | {}
53 |
54 | nx::trace operator()(void)
55 | {
56 | return nx::trace(str_);
57 | }
58 |
59 | template
60 | nx::trace operator()(const T& fmt)
61 | {
62 | return nx::trace(nx::string(str_) + fmt);
63 | }
64 | };
65 | }
66 |
67 | #ifndef NX_ENABLE_TRACE
68 | #ifndef NDEBUG
69 | # define NX_ENABLE_TRACE
70 | #endif
71 | #endif/*NX_ENABLE_TRACE*/
72 |
73 | #ifdef NX_ENABLE_TRACE
74 | # define nx_trace(...) \
75 | (nx::private_trace::maker(L"%x\n%x, L: %x ->: ")(__VA_ARGS__) << __FILE__ << NX__FUNCTION__ << __LINE__)
76 | #else /*NX_ENABLE_TRACE*/
77 | # define nx_trace(...) if (true) ; else nx::trace()
78 | #endif/*NX_ENABLE_TRACE*/
79 |
80 | //////////////////////////////////////////////////////////////////////////
81 | NX_END
82 | //////////////////////////////////////////////////////////////////////////
83 |
--------------------------------------------------------------------------------
/nixycore/container/container.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | #include "nixycore/container/any.h"
13 | #include "nixycore/container/array.h"
14 | #include "nixycore/container/deque.h"
15 | #include "nixycore/container/list.h"
16 | #include "nixycore/container/map.h"
17 | #include "nixycore/container/priority.h"
18 | #include "nixycore/container/queue.h"
19 | #include "nixycore/container/set.h"
20 | #include "nixycore/container/stack.h"
21 | #include "nixycore/container/vector.h"
22 | #include "nixycore/container/forward_list.h"
23 | #include "nixycore/container/hash_map.h"
24 | #include "nixycore/container/hash_set.h"
25 |
26 | //////////////////////////////////////////////////////////////////////////
27 |
--------------------------------------------------------------------------------
/nixycore/container/deque.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/default_alloc.h"
11 | #include "nixycore/utility/rvalue.h"
12 |
13 | #include "nixycore/general/general.h"
14 |
15 | #include // std::deque
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 | NX_BEG
19 | //////////////////////////////////////////////////////////////////////////
20 |
21 | #ifdef NX_SP_CXX11_ALIAS
22 | template
23 | using deque = std::deque::type_t>;
24 | #else /*NX_SP_CXX11_ALIAS*/
25 | template
26 | class deque : public std::deque::type_t>
27 | {
28 | typedef std::deque::type_t> base_t;
29 |
30 | public:
31 | #ifdef NX_SP_CXX11_INHERITING
32 | using base_t::deque;
33 | #else /*NX_SP_CXX11_INHERITING*/
34 | deque(void)
35 | : base_t()
36 | {}
37 |
38 | explicit deque(const typename base_t::allocator_type& a)
39 | : base_t(a)
40 | {}
41 |
42 | explicit deque(typename base_t::size_type n,
43 | const typename base_t::value_type& v = typename base_t::value_type(),
44 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
45 | : base_t(n, v, a)
46 | {}
47 |
48 | template
49 | deque(IteratorT f, IteratorT l,
50 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
51 | : base_t(f, l, a)
52 | {}
53 |
54 | deque(const deque& rhs)
55 | : base_t(rhs)
56 | {}
57 |
58 | deque(nx_rref(deque, true) rhs)
59 | : base_t()
60 | {
61 | base_t::swap(nx::moved(rhs));
62 | }
63 | #endif/*NX_SP_CXX11_INHERITING*/
64 |
65 | deque& operator=(deque rhs)
66 | {
67 | rhs.swap(*this);
68 | return (*this);
69 | }
70 | };
71 |
72 | /*
73 | Special swap algorithm
74 | */
75 |
76 | template
77 | inline void swap(deque& x, deque& y)
78 | {
79 | x.swap(y);
80 | }
81 | #endif/*NX_SP_CXX11_ALIAS*/
82 |
83 | //////////////////////////////////////////////////////////////////////////
84 | NX_END
85 | //////////////////////////////////////////////////////////////////////////
86 |
--------------------------------------------------------------------------------
/nixycore/container/forward_list.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/general/general.h"
11 |
12 | #ifdef NX_SP_CXX11_FORWARD_LIST
13 |
14 | #include "nixycore/memory/default_alloc.h"
15 | #include "nixycore/utility/rvalue.h"
16 |
17 | #include // std::forward_list
18 |
19 | //////////////////////////////////////////////////////////////////////////
20 | NX_BEG
21 | //////////////////////////////////////////////////////////////////////////
22 |
23 | #ifdef NX_SP_CXX11_ALIAS
24 | template
25 | using forward_list = std::forward_list::type_t>;
26 | #else /*NX_SP_CXX11_ALIAS*/
27 | template
28 | class forward_list : public std::forward_list::type_t>
29 | {
30 | typedef std::forward_list::type_t> base_t;
31 |
32 | public:
33 | #ifdef NX_SP_CXX11_INHERITING
34 | using base_t::forward_list;
35 | #else /*NX_SP_CXX11_INHERITING*/
36 | forward_list(void)
37 | : base_t()
38 | {}
39 |
40 | explicit forward_list(typename base_t::size_type count)
41 | : base_t(count)
42 | {}
43 |
44 | forward_list(typename base_t::size_type count, const T& val,
45 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
46 | : base_t(count, val, a)
47 | {}
48 |
49 | explicit forward_list(const typename base_t::allocator_type& a)
50 | : base_t(a)
51 | {}
52 |
53 | template
54 | forward_list(IteratorT f, IteratorT l,
55 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
56 | : base_t(f, l, a)
57 | {}
58 |
59 | forward_list(const forward_list& rhs)
60 | : base_t(rhs)
61 | {}
62 |
63 | forward_list(nx_rref(forward_list, true) rhs)
64 | : base_t()
65 | {
66 | base_t::swap(nx::moved(rhs));
67 | }
68 | #endif/*NX_SP_CXX11_INHERITING*/
69 |
70 | forward_list& operator=(forward_list rhs)
71 | {
72 | rhs.swap(*this);
73 | return (*this);
74 | }
75 | };
76 |
77 | /*
78 | Special swap algorithm
79 | */
80 |
81 | template
82 | inline void swap(forward_list& x, forward_list& y)
83 | {
84 | x.swap(y);
85 | }
86 | #endif/*NX_SP_CXX11_ALIAS*/
87 |
88 | //////////////////////////////////////////////////////////////////////////
89 | NX_END
90 | //////////////////////////////////////////////////////////////////////////
91 |
92 | #endif/*NX_SP_CXX11_FORWARD_LIST*/
93 |
--------------------------------------------------------------------------------
/nixycore/container/list.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/default_alloc.h"
11 | #include "nixycore/utility/rvalue.h"
12 |
13 | #include "nixycore/general/general.h"
14 |
15 | #include // std::list
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 | NX_BEG
19 | //////////////////////////////////////////////////////////////////////////
20 |
21 | #ifdef NX_SP_CXX11_ALIAS
22 | template
23 | using list = std::list::type_t>;
24 | #else /*NX_SP_CXX11_ALIAS*/
25 | template
26 | class list : public std::list::type_t>
27 | {
28 | typedef std::list::type_t> base_t;
29 |
30 | public:
31 | #ifdef NX_SP_CXX11_INHERITING
32 | using base_t::list;
33 | #else /*NX_SP_CXX11_INHERITING*/
34 | list(void)
35 | : base_t()
36 | {}
37 |
38 | explicit list(const typename base_t::allocator_type& a)
39 | : base_t(a)
40 | {}
41 |
42 | explicit list(typename base_t::size_type n,
43 | const typename base_t::value_type& v = typename base_t::value_type(),
44 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
45 | : base_t(n, v, a)
46 | {}
47 |
48 | template
49 | list(IteratorT f, IteratorT l,
50 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
51 | : base_t(f, l, a)
52 | {}
53 |
54 | list(const list& rhs)
55 | : base_t(rhs)
56 | {}
57 |
58 | list(nx_rref(list, true) rhs)
59 | : base_t()
60 | {
61 | base_t::swap(nx::moved(rhs));
62 | }
63 | #endif/*NX_SP_CXX11_INHERITING*/
64 |
65 | list& operator=(list rhs)
66 | {
67 | rhs.swap(*this);
68 | return (*this);
69 | }
70 | };
71 |
72 | /*
73 | Special swap algorithm
74 | */
75 |
76 | template
77 | inline void swap(list& x, list& y)
78 | {
79 | x.swap(y);
80 | }
81 | #endif/*NX_SP_CXX11_ALIAS*/
82 |
83 | //////////////////////////////////////////////////////////////////////////
84 | NX_END
85 | //////////////////////////////////////////////////////////////////////////
86 |
--------------------------------------------------------------------------------
/nixycore/container/priority.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/container/vector.h"
11 |
12 | #include "nixycore/utility/rvalue.h"
13 |
14 | #include "nixycore/general/general.h"
15 |
16 | #include // std::priority_queue
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | #ifdef NX_SP_CXX11_ALIAS
23 | template
24 | <
25 | typename T, typename SeqT = nx::vector,
26 | typename CompT = std::less
27 | >
28 | using priority = std::priority_queue;
29 | #else /*NX_SP_CXX11_ALIAS*/
30 | template
31 | <
32 | typename T, typename SeqT = nx::vector,
33 | typename CompT = std::less
34 | >
35 | class priority : public std::priority_queue
36 | {
37 | typedef std::priority_queue base_t;
38 |
39 | public:
40 | #ifdef NX_SP_CXX11_INHERITING
41 | using base_t::priority_queue;
42 | #else /*NX_SP_CXX11_INHERITING*/
43 | explicit priority(const CompT& c = CompT(), const SeqT& s = SeqT())
44 | : base_t(c, s)
45 | {}
46 |
47 | template
48 | priority(IteratorT f, IteratorT l,
49 | const CompT& c = CompT(), const SeqT& s = SeqT())
50 | : base_t(f, l, c, s)
51 | {}
52 |
53 | priority(const priority& rhs)
54 | : base_t(rhs)
55 | {}
56 |
57 | #ifdef NX_SP_CXX11_STACK_SWAP
58 | priority(nx_rref(priority, true) rhs)
59 | : base_t()
60 | {
61 | base_t::swap(nx::moved(rhs));
62 | }
63 | #endif
64 | #endif/*NX_SP_CXX11_INHERITING*/
65 |
66 | priority& operator=(priority rhs)
67 | {
68 | rhs.swap(*this);
69 | return (*this);
70 | }
71 | };
72 |
73 | /*
74 | Special swap algorithm
75 | */
76 |
77 | template
78 | inline void swap(priority& x, priority& y)
79 | {
80 | x.swap(y);
81 | }
82 | #endif/*NX_SP_CXX11_ALIAS*/
83 |
84 | /*
85 | Special assign algorithm
86 | */
87 |
88 | template
89 | inline void insert(std::priority_queue& set,
90 | typename std::priority_queue::iterator /*ite*/, const V& val)
91 | {
92 | set.push(val);
93 | }
94 |
95 | template
96 | inline void erase(std::priority_queue& set,
97 | typename std::priority_queue::iterator /*ite*/)
98 | {
99 | set.pop();
100 | }
101 |
102 | #ifndef NX_SP_CXX11_ALIAS
103 | template
104 | inline void insert(priority& set, typename priority::iterator /*ite*/, const V& val)
105 | {
106 | set.push(val);
107 | }
108 |
109 | template
110 | inline void erase(priority& set, typename priority::iterator /*ite*/)
111 | {
112 | set.pop();
113 | }
114 | #endif/*NX_SP_CXX11_ALIAS*/
115 |
116 | //////////////////////////////////////////////////////////////////////////
117 | NX_END
118 | //////////////////////////////////////////////////////////////////////////
119 |
--------------------------------------------------------------------------------
/nixycore/container/queue.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/container/deque.h"
11 |
12 | #include "nixycore/utility/rvalue.h"
13 |
14 | #include "nixycore/general/general.h"
15 |
16 | #include // std::queue
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | #ifdef NX_SP_CXX11_ALIAS
23 | template >
24 | using queue = std::queue;
25 | #else /*NX_SP_CXX11_ALIAS*/
26 | template >
27 | class queue : public std::queue
28 | {
29 | typedef std::queue base_t;
30 |
31 | public:
32 | #ifdef NX_SP_CXX11_INHERITING
33 | using base_t::queue;
34 | #else /*NX_SP_CXX11_INHERITING*/
35 | explicit queue(const SeqT& s = SeqT())
36 | : base_t(s)
37 | {}
38 |
39 | queue(const queue& rhs)
40 | : base_t(rhs)
41 | {}
42 |
43 | #ifdef NX_SP_CXX11_STACK_SWAP
44 | queue(nx_rref(queue, true) rhs)
45 | : base_t()
46 | {
47 | base_t::swap(nx::moved(rhs));
48 | }
49 | #endif
50 | #endif/*NX_SP_CXX11_INHERITING*/
51 |
52 | queue& operator=(queue rhs)
53 | {
54 | rhs.swap(*this);
55 | return (*this);
56 | }
57 | };
58 |
59 | /*
60 | Special swap algorithm
61 | */
62 |
63 | template
64 | inline void swap(queue& x, queue& y)
65 | {
66 | x.swap(y);
67 | }
68 | #endif/*NX_SP_CXX11_ALIAS*/
69 |
70 | /*
71 | Special assign algorithm
72 | */
73 |
74 | template
75 | inline void insert(std::queue& set, typename std::queue::iterator /*ite*/, const V& val)
76 | {
77 | set.push(val);
78 | }
79 |
80 | template
81 | inline void erase(std::queue& set, typename std::queue::iterator /*ite*/)
82 | {
83 | set.pop();
84 | }
85 |
86 | #ifndef NX_SP_CXX11_ALIAS
87 | template
88 | inline void insert(queue& set, typename queue::iterator /*ite*/, const V& val)
89 | {
90 | set.push(val);
91 | }
92 |
93 | template
94 | inline void erase(queue& set, typename queue::iterator /*ite*/)
95 | {
96 | set.pop();
97 | }
98 | #endif/*NX_SP_CXX11_ALIAS*/
99 |
100 | //////////////////////////////////////////////////////////////////////////
101 | NX_END
102 | //////////////////////////////////////////////////////////////////////////
103 |
--------------------------------------------------------------------------------
/nixycore/container/stack.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/container/deque.h"
11 |
12 | #include "nixycore/utility/rvalue.h"
13 |
14 | #include "nixycore/general/general.h"
15 |
16 | #include // std::stack
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | #ifdef NX_SP_CXX11_ALIAS
23 | template >
24 | using stack = std::stack;
25 | #else /*NX_SP_CXX11_ALIAS*/
26 | template >
27 | class stack : public std::stack
28 | {
29 | typedef std::stack base_t;
30 |
31 | public:
32 | #ifdef NX_SP_CXX11_INHERITING
33 | using base_t::stack;
34 | #else /*NX_SP_CXX11_INHERITING*/
35 | explicit stack(const SeqT& s = SeqT())
36 | : base_t(s)
37 | {}
38 |
39 | stack(const stack& rhs)
40 | : base_t(rhs)
41 | {}
42 |
43 | #ifdef NX_SP_CXX11_STACK_SWAP
44 | stack(nx_rref(stack, true) rhs)
45 | : base_t()
46 | {
47 | base_t::swap(nx::moved(rhs));
48 | }
49 | #endif
50 | #endif/*NX_SP_CXX11_INHERITING*/
51 |
52 | stack& operator=(stack rhs)
53 | {
54 | rhs.swap(*this);
55 | return (*this);
56 | }
57 | };
58 |
59 | /*
60 | Special swap algorithm
61 | */
62 |
63 | template
64 | inline void swap(stack& x, stack& y)
65 | {
66 | x.swap(y);
67 | }
68 | #endif/*NX_SP_CXX11_ALIAS*/
69 |
70 | /*
71 | Special assign algorithm
72 | */
73 |
74 | template
75 | inline void insert(std::stack& set, typename std::stack::iterator /*ite*/, const V& val)
76 | {
77 | set.push(val);
78 | }
79 |
80 | template
81 | inline void erase(std::stack& set, typename std::stack::iterator /*ite*/)
82 | {
83 | set.pop();
84 | }
85 |
86 | #ifndef NX_SP_CXX11_ALIAS
87 | template
88 | inline void insert(stack& set, typename stack::iterator /*ite*/, const V& val)
89 | {
90 | set.push(val);
91 | }
92 |
93 | template
94 | inline void erase(stack& set, typename stack::iterator /*ite*/)
95 | {
96 | set.pop();
97 | }
98 | #endif/*NX_SP_CXX11_ALIAS*/
99 |
100 | //////////////////////////////////////////////////////////////////////////
101 | NX_END
102 | //////////////////////////////////////////////////////////////////////////
103 |
--------------------------------------------------------------------------------
/nixycore/container/vector.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/default_alloc.h"
11 | #include "nixycore/utility/rvalue.h"
12 |
13 | #include "nixycore/general/general.h"
14 |
15 | #include // std::vector
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 | NX_BEG
19 | //////////////////////////////////////////////////////////////////////////
20 |
21 | #ifdef NX_SP_CXX11_ALIAS
22 | template
23 | using vector = std::vector::type_t>;
24 | #else /*NX_SP_CXX11_ALIAS*/
25 | template
26 | class vector : public std::vector::type_t>
27 | {
28 | typedef std::vector::type_t> base_t;
29 |
30 | public:
31 | #ifdef NX_SP_CXX11_INHERITING
32 | using base_t::vector;
33 | #else /*NX_SP_CXX11_INHERITING*/
34 | vector(void)
35 | : base_t()
36 | {}
37 |
38 | explicit vector(const typename base_t::allocator_type& a)
39 | : base_t(a)
40 | {}
41 |
42 | explicit vector(typename base_t::size_type n,
43 | const typename base_t::value_type& v = typename base_t::value_type(),
44 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
45 | : base_t(n, v, a)
46 | {}
47 |
48 | template
49 | vector(IteratorT f, IteratorT l,
50 | const typename base_t::allocator_type& a = typename base_t::allocator_type())
51 | : base_t(f, l, a)
52 | {}
53 |
54 | vector(const vector& rhs)
55 | : base_t(rhs)
56 | {}
57 |
58 | vector(nx_rref(vector, true) rhs)
59 | : base_t()
60 | {
61 | base_t::swap(nx::moved(rhs));
62 | }
63 | #endif/*NX_SP_CXX11_INHERITING*/
64 |
65 | vector& operator=(vector rhs)
66 | {
67 | rhs.swap(*this);
68 | return (*this);
69 | }
70 | };
71 |
72 | /*
73 | Special swap algorithm
74 | */
75 |
76 | template
77 | inline void swap(vector& x, vector& y)
78 | {
79 | x.swap(y);
80 | }
81 | #endif/*NX_SP_CXX11_ALIAS*/
82 |
83 | //////////////////////////////////////////////////////////////////////////
84 | NX_END
85 | //////////////////////////////////////////////////////////////////////////
86 |
--------------------------------------------------------------------------------
/nixycore/delegate/delegate.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | #include "nixycore/delegate/function_traits.h"
13 | #include "nixycore/delegate/functor.h"
14 | #include "nixycore/delegate/bind.h"
15 | #include "nixycore/delegate/signal.h"
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 |
--------------------------------------------------------------------------------
/nixycore/finalizer/finalizer.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | #include "nixycore/finalizer/scope_guard.h"
13 | #include "nixycore/finalizer/ref_counter.h"
14 | #include "nixycore/finalizer/holder.h"
15 | #include "nixycore/finalizer/gc.h"
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 |
--------------------------------------------------------------------------------
/nixycore/finalizer/scope_guard.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/delegate/functor.h"
11 |
12 | #include "nixycore/bugfix/noexcept.h"
13 |
14 | #include "nixycore/general/general.h"
15 | #include "nixycore/typemanip/typemanip.h"
16 | #include "nixycore/utility/utility.h"
17 | #include "nixycore/algorithm/algorithm.h"
18 |
19 | //////////////////////////////////////////////////////////////////////////
20 | NX_BEG
21 | //////////////////////////////////////////////////////////////////////////
22 |
23 | /*
24 | Execute guard function when the enclosing scope exits
25 | */
26 |
27 | template
28 | class scope_guard : noncopyable
29 | {
30 | F destructor_;
31 | mutable bool dismiss_;
32 |
33 | public:
34 | explicit scope_guard(const F& destructor)
35 | : destructor_(destructor)
36 | , dismiss_(false)
37 | {}
38 |
39 | explicit scope_guard(nx_rref(F) destructor)
40 | : destructor_(nx::move(destructor))
41 | , dismiss_(false)
42 | {}
43 |
44 | scope_guard(nx_rref(scope_guard, true) rhs)
45 | : destructor_(nx::move(rhs.destructor_))
46 | , dismiss_(true) // dismiss rhs
47 | {
48 | nx::swap(dismiss_, nx::moved(rhs).dismiss_);
49 | }
50 |
51 | ~scope_guard(void)
52 | {
53 | if (!dismiss_) try
54 | {
55 | destructor_();
56 | }
57 | /*
58 | In the realm of exceptions,
59 | it is fundamental that you can do nothing
60 | if your "undo/recover" action fails.
61 | */
62 | catch(...)
63 | {
64 | // Do nothing
65 | }
66 | }
67 |
68 | void dismiss() const nx_noexcept
69 | {
70 | dismiss_ = true;
71 | }
72 |
73 | nx_rval(functor, true) get(void) const
74 | {
75 | functor tmp(destructor_);
76 | return nx::move(tmp);
77 | }
78 |
79 | void swap(scope_guard& rhs)
80 | {
81 | nx::swap(destructor_, rhs.destructor_);
82 | nx::swap(dismiss_, rhs.dismiss_);
83 | }
84 | };
85 |
86 | /*
87 | -->
88 | void func(void)
89 | {
90 | int* p = new int(2);
91 | nx_guard_scope(nx::bind(new_dest, p));
92 | ...
93 | }
94 | */
95 |
96 | template
97 | inline nx_rval(scope_guard::type_t>, true)
98 | make_scope_guard(nx_fref(F) f)
99 | {
100 | return scope_guard::type_t>(nx_forward(F, f));
101 | }
102 |
103 | #define NX_GUARD_SCOPE_NAM_(nn) scope_guard_##nn##__
104 | #define NX_GUARD_SCOPE_(nn, call) \
105 | nx_auto(NX_UNUSED NX_GUARD_SCOPE_NAM_(nn), call)
106 |
107 | #define nx_guard_scope(...) NX_GUARD_SCOPE_(__LINE__, nx::make_scope_guard(__VA_ARGS__))
108 |
109 | //////////////////////////////////////////////////////////////////////////
110 | NX_END
111 | //////////////////////////////////////////////////////////////////////////
112 |
--------------------------------------------------------------------------------
/nixycore/general/detail/plat_detect_cc.hxx:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | //////////////////////////////////////////////////////////////////////////
9 | /*
10 | The compiler, must be one of: (NX_CC_XX)
11 |
12 | MSVC - Microsoft Visual C++
13 | GNUC - GNU C++
14 | CLANG - C++ front-end for the LLVM compiler
15 | */
16 | //////////////////////////////////////////////////////////////////////////
17 |
18 | #if defined(_MSC_VER) && (_MSC_VER >= 1400) /* >= 8.0 */
19 | # define NX_CC_MSVC _MSC_VER
20 | #elif defined(__GNUC__)
21 | # define NX_CC_GNUC
22 | # if defined(__clang__)
23 | # define NX_CC_CLANG // Clang also masquerades as GCC
24 | # else
25 | # define NX_CC_GCC
26 | # endif
27 | #else
28 | # error "This CC is unsupported."
29 | #endif
30 |
31 | #if defined(NX_CC_MSVC)
32 | # pragma warning(disable:4996) // This function or variable may be unsafe
33 | # pragma warning(disable:4101) // The local variable is never used
34 | # define NX_UNUSED __pragma(warning(suppress:4100))
35 | # ifndef NX__FUNCTION__
36 | # define NX__FUNCTION__ __FUNCSIG__
37 | # endif
38 | #elif defined(NX_CC_GNUC)
39 | # define NX_UNUSED __attribute__((__unused__))
40 | # ifndef NX__FUNCTION__
41 | # define NX__FUNCTION__ __PRETTY_FUNCTION__
42 | # endif
43 | #endif
44 |
45 | //////////////////////////////////////////////////////////////////////////
46 |
--------------------------------------------------------------------------------
/nixycore/general/detail/plat_detect_pc.hxx:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | //////////////////////////////////////////////////////////////////////////
9 | /*
10 | The processor, must be one of: (NX_PC_XX)
11 |
12 | X86_32 - x86 32-bit processors
13 | X86_64 - x86 64-bit processors
14 | IA_64 - Itanium Processor Family 64-bit processors
15 | ARM - ARM processors
16 | */
17 | //////////////////////////////////////////////////////////////////////////
18 |
19 | #if defined(__i386__) || defined(_M_IX86)
20 | # define NX_PC_X86_32
21 | #elif defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
22 | # define NX_PC_X86_64
23 | #elif defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
24 | # define NX_PC_IA_64
25 | #elif defined(__arm__) || defined(_ARM_) || defined(_M_ARM)
26 | # define NX_PC_ARM
27 | #else
28 | # error "This processor is unsupported."
29 | #endif
30 |
31 | #if defined(NX_PC_X86_32) || defined(NX_PC_X86_64)
32 | # define NX_PC_X86
33 | #endif
34 |
35 | #if defined(NX_PC_X86_32)
36 | # define NX_PC_X32
37 | #endif
38 |
39 | #if defined(NX_PC_X86_64) || defined(NX_PC_IA_64)
40 | # define NX_PC_X64
41 | #endif
42 |
43 | #if defined(NX_CC_GNUC)
44 | # if defined(__MMX__)
45 | # define NX_PC_MMX
46 | # endif
47 | # if defined(__SSE__)
48 | # define NX_PC_SSE
49 | # endif
50 | # if defined(__SSE2__)
51 | # define NX_PC_SSE2
52 | # endif
53 | #elif defined(NX_CC_MSVC) && defined(NX_PC_X86)
54 | # if !defined(NX_PC_X86_64)
55 | # define NX_PC_MMX
56 | # endif
57 | # if defined(_M_IX86_FP) && (_M_IX86_FP >= 1)
58 | # define NX_PC_SSE
59 | # endif
60 | # if defined(_M_IX86_FP) && (_M_IX86_FP >= 2)
61 | # define NX_PC_SSE2
62 | # endif
63 | #endif
64 |
65 | //////////////////////////////////////////////////////////////////////////
66 |
--------------------------------------------------------------------------------
/nixycore/general/general.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | /*
13 | Standard Librarys
14 | */
15 |
16 | // size_t, ptrdiff_t, ...
17 | #include
18 | // printf, vsnprintf, vsprintf, ...
19 | #include
20 | // wprintf, vswprintf, ...
21 | #include
22 | // abort, rand, srand, ...
23 | #include
24 | // va_list, va_start, va_end
25 | #include
26 | // memcpy, memset, ...
27 | #include
28 |
29 | /*
30 | Detect platforms
31 | */
32 |
33 | // NX_OS, NX_CC, NDEBUG, ...
34 | #include "nixycore/general/plat_detect.h"
35 |
36 | //////////////////////////////////////////////////////////////////////////
37 |
38 | // namespace define
39 |
40 | #define NX_BEG namespace nx {
41 | #define NX_END }
42 |
43 | //////////////////////////////////////////////////////////////////////////
44 | NX_BEG
45 | //////////////////////////////////////////////////////////////////////////
46 |
47 | // global types
48 |
49 | typedef unsigned char uchar;
50 | typedef unsigned short ushort;
51 | typedef unsigned int uint;
52 | typedef unsigned long ulong;
53 | typedef long long llong;
54 | typedef unsigned long long ullong;
55 |
56 | typedef wchar_t wchar;
57 | typedef long double ldouble;
58 |
59 | typedef char* pchar;
60 | typedef const char* cpchar;
61 | typedef wchar_t* pwchar;
62 | typedef const wchar_t* cpwchar;
63 | typedef void* pvoid;
64 | typedef const void* cpvoid;
65 |
66 | typedef unsigned char byte;
67 |
68 | typedef ::size_t size_t;
69 | typedef ::ptrdiff_t ptrdiff_t;
70 |
71 | typedef char sint8;
72 | typedef uchar uint8;
73 | typedef short sint16;
74 | typedef ushort uint16;
75 | typedef int sint32;
76 | typedef uint uint32;
77 | typedef llong sint64;
78 | typedef ullong uint64;
79 |
80 | //////////////////////////////////////////////////////////////////////////
81 | NX_END
82 | //////////////////////////////////////////////////////////////////////////
83 |
--------------------------------------------------------------------------------
/nixycore/general/global_functions.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #if defined(NX_OS_LINUX)
9 | #include
10 | #endif
11 |
12 | //////////////////////////////////////////////////////////////////////////
13 | NX_BEG
14 | //////////////////////////////////////////////////////////////////////////
15 |
16 | /*
17 | Detect the numbers of the CPU cores
18 | */
19 | inline long detect_cpu_count(void)
20 | {
21 | static long cpu_count = 0;
22 | if (cpu_count > 0) return cpu_count;
23 | #if defined(NX_OS_WIN)
24 | SYSTEM_INFO si;
25 | GetSystemInfo(&si);
26 | cpu_count = static_cast(si.dwNumberOfProcessors);
27 | #elif defined(NX_OS_LINUX)
28 | cpu_count = static_cast(get_nprocs());
29 | #endif
30 | if (cpu_count < 1) cpu_count = 1; // fail-safe
31 | return cpu_count;
32 | }
33 |
34 | //////////////////////////////////////////////////////////////////////////
35 | NX_END
36 | //////////////////////////////////////////////////////////////////////////
37 |
--------------------------------------------------------------------------------
/nixycore/general/plat_detect.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | //////////////////////////////////////////////////////////////////////////
9 |
10 | // The compiler, must be one of: (NX_CC_XX)
11 | #include "detail/plat_detect_cc.hxx"
12 |
13 | // C++ features support, must be one of: (NX_SP_XX)
14 | #include "detail/plat_detect_sp.hxx"
15 |
16 | // The processor, must be one of: (NX_PC_XX)
17 | #include "detail/plat_detect_pc.hxx"
18 |
19 | // The operating system, must be one of: (NX_OS_XX)
20 | #include "detail/plat_detect_os.hxx"
21 |
22 | // The other defines
23 |
24 | #ifndef NDEBUG
25 | # ifndef _DEBUG
26 | # define _DEBUG
27 | # endif
28 | # ifndef DEBUG
29 | # define DEBUG
30 | # endif
31 | #endif
32 |
33 | #ifndef CHAR_BIT
34 | # define CHAR_BIT 8
35 | #endif
36 |
37 | //////////////////////////////////////////////////////////////////////////
38 |
--------------------------------------------------------------------------------
/nixycore/memory/alloc_model.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/construct.h"
11 | #include "nixycore/memory/alloc.h"
12 |
13 | #include "nixycore/utility/limit_of.h"
14 |
15 | #include "nixycore/general/general.h"
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 | NX_BEG
19 | //////////////////////////////////////////////////////////////////////////
20 |
21 | /*
22 | a adapter allocator for stl
23 | */
24 |
25 | template
26 | struct std_allocator
27 | {
28 | public:
29 | // type definitions
30 | typedef T value_type;
31 | typedef T* pointer;
32 | typedef const T* const_pointer;
33 | typedef T& reference;
34 | typedef const T& const_reference;
35 | typedef size_t size_type;
36 | typedef ptrdiff_t difference_type;
37 |
38 | // the other type of std_allocator
39 | template
40 | struct rebind { typedef std_allocator other; };
41 |
42 | public:
43 | pointer address(reference val) const
44 | { return &val; }
45 | const_pointer address(const_reference val) const
46 | { return &val; }
47 |
48 | size_type max_size() const
49 | { return (nx::limit_of::upper / sizeof(T)); }
50 |
51 | public:
52 | std_allocator(void) {}
53 | std_allocator(const std_allocator&) {}
54 | template
55 | std_allocator(const std_allocator&) {}
56 |
57 | std_allocator& operator=(const std_allocator&) { return (*this); }
58 | template
59 | std_allocator& operator=(const std_allocator&) { return (*this); }
60 |
61 | template
62 | bool operator==(const std_allocator&) const { return true; }
63 | /* vector(609): compiler error C2593 */
64 | template
65 | bool operator!=(const std_allocator&) const { return false; }
66 |
67 | public:
68 | pointer allocate(size_type count, const pvoid = NULL)
69 | {
70 | if (count > this->max_size()) throw std::bad_alloc();
71 | pvoid p = nx::alloc(count * sizeof(T));
72 | if (!p) throw std::bad_alloc();
73 | return static_cast(p);
74 | }
75 |
76 | void deallocate(pvoid p, size_type count)
77 | {
78 | nx::free(p, count * sizeof(T));
79 | }
80 |
81 | static void construct(pointer p, const T& val)
82 | {
83 | nx_construct(p, value_type, (val));
84 | }
85 |
86 | static void destroy(pointer p)
87 | {
88 | nx_destruct(p, value_type);
89 | }
90 | };
91 |
92 | /*
93 | alloc policy base
94 | */
95 |
96 | template
97 | struct alloc_model : ModelT
98 | {
99 | /* the stl allocator type */
100 | template
101 | struct std_allocator { typedef nx::std_allocator type_t; };
102 | };
103 |
104 | /*
105 | check alloc policy
106 | */
107 |
108 | template
109 | struct is_alloc
110 | : type_if
111 | {};
112 |
113 | template
114 | struct is_alloc >
115 | : type_if
116 | {};
117 |
118 | //////////////////////////////////////////////////////////////////////////
119 | NX_END
120 | //////////////////////////////////////////////////////////////////////////
121 |
--------------------------------------------------------------------------------
/nixycore/memory/center_heap.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/alloc.h"
11 | #include "nixycore/memory/std_alloc.h"
12 | #include "nixycore/memory/cache_pool.h"
13 | #include "nixycore/memory/mem_pool.h"
14 |
15 | #include "nixycore/bugfix/assert.h"
16 | #include "nixycore/typemanip/typedefs.h"
17 | #include "nixycore/thread/thread_model.h"
18 | #include "nixycore/pattern/singleton.h"
19 |
20 | #include "nixycore/general/general.h"
21 |
22 | //////////////////////////////////////////////////////////////////////////
23 | NX_BEG
24 | //////////////////////////////////////////////////////////////////////////
25 |
26 | /*
27 | center heap alloc policy model
28 | */
29 |
30 | template
31 | struct center_heap_model
32 | {
33 | typedef cache_pool cache_pool_t;
34 | typedef mem_pool mem_pool_t;
35 |
36 | static mem_pool_t& instance(void)
37 | {
38 | static mem_pool_t& cache = nx::singleton();
39 | return cache;
40 | }
41 |
42 | static pvoid alloc(size_t size)
43 | {
44 | return instance().alloc(size);
45 | }
46 |
47 | static void free(pvoid p, size_t size)
48 | {
49 | instance().free(p, size);
50 | }
51 |
52 | static pvoid realloc(pvoid p, size_t old_size, size_t new_size)
53 | {
54 | return instance().realloc(p, old_size, new_size);
55 | }
56 | };
57 |
58 | namespace use
59 | {
60 | typedef alloc_model > alloc_center_heap;
61 | }
62 |
63 | //////////////////////////////////////////////////////////////////////////
64 | NX_END
65 | //////////////////////////////////////////////////////////////////////////
66 |
--------------------------------------------------------------------------------
/nixycore/memory/construct.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/general/general.h"
11 |
12 | // placement new, std::bad_alloc
13 | #include
14 |
15 | //////////////////////////////////////////////////////////////////////////
16 | NX_BEG
17 | //////////////////////////////////////////////////////////////////////////
18 |
19 | /*
20 | construct/destruct
21 | */
22 |
23 | #define nx_construct(p, T, ...) \
24 | (T*)::new ((void*)(p)) T __VA_ARGS__
25 |
26 | #define nx_construct_arr(p, T, N, ...) \
27 | do \
28 | { \
29 | for(size_t i = 0; i < N; ++i) \
30 | nx_construct(&(((T*)(p))[i]), T, __VA_ARGS__); \
31 | } while(false)
32 |
33 | #define nx_destruct(p, T) \
34 | ((T*)(p))->~T()
35 |
36 | #define nx_destruct_arr(p, T, N) \
37 | do \
38 | { \
39 | for(size_t i = 0; i < N; ++i) \
40 | nx_destruct(&(((T*)(p))[i]), T); \
41 | } while(false)
42 |
43 | //////////////////////////////////////////////////////////////////////////
44 | NX_END
45 | //////////////////////////////////////////////////////////////////////////
46 |
--------------------------------------------------------------------------------
/nixycore/memory/default_alloc.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/alloc.h"
11 | #include "nixycore/memory/std_alloc.h"
12 | #include "nixycore/memory/mem_alloc.h"
13 |
14 | #include "nixycore/general/general.h"
15 | #include "nixycore/preprocessor/preprocessor.h"
16 | #include "nixycore/utility/utility.h"
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | #ifndef NX_DEFAULT_ALLOC
23 | #define NX_DEFAULT_ALLOC nx::use::alloc_pool
24 | #endif
25 |
26 | /*
27 | construct alloc
28 | */
29 |
30 | inline pvoid alloc(size_t size)
31 | {
32 | return nx::alloc(size);
33 | }
34 |
35 | #ifdef NX_SP_CXX11_TEMPLATES
36 | template
37 | inline typename nx::enable_if::value,
38 | T*>::type_t alloc(nx_fref(P)... par)
39 | {
40 | return nx::alloc(nx_forward(P, par)...);
41 | }
42 | #else /*NX_SP_CXX11_TEMPLATES*/
43 | template
44 | inline T* alloc(void)
45 | {
46 | return nx::alloc();
47 | }
48 |
49 | #define NX_ALLOC_(n) \
50 | template \
51 | inline typename nx::enable_if::value, \
52 | T*>::type_t alloc(NX_PP_TYPE_2(n, P, NX_PP_FREF(par))) \
53 | { \
54 | return nx::alloc(NX_PP_FORWARD(n, P, par)); \
55 | }
56 | NX_PP_MULT_MAX(NX_ALLOC_)
57 | #undef NX_ALLOC_
58 | #endif/*NX_SP_CXX11_TEMPLATES*/
59 |
60 | /*
61 | destruct free
62 | */
63 |
64 | inline void free(pvoid p, size_t size)
65 | {
66 | nx::free(p, size);
67 | }
68 |
69 | template
70 | inline void free(T* p)
71 | {
72 | nx::free(p);
73 | }
74 |
75 | /*
76 | realloc
77 | */
78 |
79 | inline pvoid realloc(pvoid p, size_t old_size, size_t new_size)
80 | {
81 | return nx::realloc(p, old_size, new_size);
82 | }
83 |
84 | template
85 | inline pvoid realloc(T* p, size_t size)
86 | {
87 | return nx::realloc(p, size);
88 | }
89 |
90 | //////////////////////////////////////////////////////////////////////////
91 | NX_END
92 | //////////////////////////////////////////////////////////////////////////
93 |
--------------------------------------------------------------------------------
/nixycore/memory/mem_alloc.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/alloc.h"
11 | #include "nixycore/memory/std_alloc.h"
12 | #include "nixycore/memory/cache_pool.h"
13 | #include "nixycore/memory/mem_pool.h"
14 | #include "nixycore/memory/center_heap.h"
15 |
16 | #include "nixycore/thread/tls_ptr.h"
17 | #include "nixycore/thread/thread_model.h"
18 | #include "nixycore/pattern/singleton.h"
19 | #include "nixycore/algorithm/series.h"
20 |
21 | #include "nixycore/general/general.h"
22 | #include "nixycore/preprocessor/preprocessor.h"
23 | #include "nixycore/utility/utility.h"
24 |
25 | //////////////////////////////////////////////////////////////////////////
26 | NX_BEG
27 | //////////////////////////////////////////////////////////////////////////
28 |
29 | /*
30 | The thread local storage singleton
31 | */
32 |
33 | template
34 | class TLSSingleton
35 | {
36 | typedef tls_ptr tls_t;
37 |
38 | static void destroy(pvoid p)
39 | {
40 | nx::free(static_cast(p));
41 | }
42 |
43 | public:
44 | #ifdef NX_SP_CXX11_TEMPLATES
45 | template
46 | static T& instance(nx_fref(P)... par)
47 | {
48 | T* p = singleton(destroy);
49 | if (p) return (*p);
50 | singleton() = p = nx::alloc(nx_forward(P, par)...);
51 | return (*p);
52 | }
53 | #else /*NX_SP_CXX11_TEMPLATES*/
54 | static T& instance(void)
55 | {
56 | T* p = singleton(destroy);
57 | if (p) return (*p);
58 | singleton() = p = nx::alloc();
59 | return (*p);
60 | }
61 | #define NX_INSTANCE_(n) \
62 | template \
63 | static T& instance(NX_PP_TYPE_2(n, P, NX_PP_FREF(par))) \
64 | { \
65 | T* p = singleton(destroy); \
66 | if (p) return (*p); \
67 | singleton() = p = nx::alloc(NX_PP_FORWARD(n, P, par)); \
68 | return (*p); \
69 | }
70 | NX_PP_MULT_MAX(NX_INSTANCE_)
71 | #undef NX_INSTANCE_
72 | #endif/*NX_SP_CXX11_TEMPLATES*/
73 | };
74 |
75 | /*
76 | memory alloc policy model
77 | */
78 |
79 | struct pool_alloc_model
80 | {
81 | typedef cache_pool
82 | <
83 | use::alloc_std,
84 | use::thread_single,
85 | use::alloc_center_heap
86 | > cache_pool_t;
87 |
88 | typedef mem_pool mem_pool_t;
89 |
90 | static mem_pool_t& instance(void)
91 | {
92 | use::alloc_center_heap::instance(); // Make center heap run first
93 | return TLSSingleton::instance();
94 | }
95 |
96 | static pvoid alloc(size_t size)
97 | {
98 | return instance().alloc(size);
99 | }
100 |
101 | static void free(pvoid p, size_t size)
102 | {
103 | instance().free(p, size);
104 | }
105 |
106 | static pvoid realloc(pvoid p, size_t old_size, size_t new_size)
107 | {
108 | return instance().realloc(p, old_size, new_size);
109 | }
110 | };
111 |
112 | namespace use
113 | {
114 | typedef alloc_model alloc_pool;
115 | }
116 |
117 | //////////////////////////////////////////////////////////////////////////
118 | NX_END
119 | //////////////////////////////////////////////////////////////////////////
120 |
--------------------------------------------------------------------------------
/nixycore/memory/mem_guard.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/default_alloc.h"
11 |
12 | #include "nixycore/delegate/functor.h"
13 | #include "nixycore/finalizer/scope_guard.h"
14 | #include "nixycore/utility/rvalue.h"
15 |
16 | #include "nixycore/general/general.h"
17 | #include "nixycore/typemanip/typemanip.h"
18 |
19 | //////////////////////////////////////////////////////////////////////////
20 | NX_BEG
21 | //////////////////////////////////////////////////////////////////////////
22 |
23 | /*
24 | memory scope guard
25 | */
26 |
27 | namespace private_mem_guard
28 | {
29 | template
30 | struct free_fr
31 | {
32 | T p_;
33 | free_fr(T p) : p_(p) {}
34 | void operator()(void) { nx::free(p_); }
35 | };
36 |
37 | template <>
38 | struct free_fr
39 | {
40 | pvoid p_;
41 | size_t s_;
42 | free_fr(pvoid p, size_t s) : p_(p), s_(s) {}
43 | void operator()(void) { nx::free(p_, s_); }
44 | };
45 | }
46 |
47 | template
48 | inline private_mem_guard::free_fr make_destructor(T* r)
49 | {
50 | return private_mem_guard::free_fr(r);
51 | }
52 |
53 | template
54 | inline typename nx::enable_if::value,
55 | private_mem_guard::free_fr >::type_t make_destructor(pvoid r, SizeT s)
56 | {
57 | return private_mem_guard::free_fr(r, s);
58 | }
59 |
60 | #define nx_memory_scope(...) \
61 | nx_guard_scope(nx::make_destructor(__VA_ARGS__))
62 |
63 | //////////////////////////////////////////////////////////////////////////
64 | NX_END
65 | //////////////////////////////////////////////////////////////////////////
66 |
--------------------------------------------------------------------------------
/nixycore/memory/memory.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | #include "nixycore/memory/construct.h"
13 | #include "nixycore/memory/alloc.h"
14 | #include "nixycore/memory/std_alloc.h"
15 | #include "nixycore/memory/fixed_pool.h"
16 | #include "nixycore/memory/cache_pool.h"
17 | #include "nixycore/memory/mem_pool.h"
18 | #include "nixycore/memory/center_heap.h"
19 | #include "nixycore/memory/mem_alloc.h"
20 | #include "nixycore/memory/default_alloc.h"
21 | #include "nixycore/memory/mem_leak.h"
22 | #include "nixycore/memory/unfixed_pool.h"
23 | #include "nixycore/memory/object_pool.h"
24 | #include "nixycore/memory/mem_guard.h"
25 | #include "nixycore/memory/pointer.h"
26 |
27 | //////////////////////////////////////////////////////////////////////////
28 |
--------------------------------------------------------------------------------
/nixycore/memory/std_alloc.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/construct.h"
11 | #include "nixycore/memory/alloc_model.h"
12 | #include "nixycore/memory/alloc.h"
13 |
14 | #include "nixycore/general/general.h"
15 |
16 | //////////////////////////////////////////////////////////////////////////
17 | NX_BEG
18 | //////////////////////////////////////////////////////////////////////////
19 |
20 | /*
21 | standard alloc policy model
22 | */
23 |
24 | struct std_alloc_model
25 | {
26 | static pvoid alloc(size_t size)
27 | { return (size ? ::malloc(size) : NULL); }
28 | static void free(pvoid p, size_t /*size*/)
29 | { if (p) ::free(p); }
30 | static pvoid realloc(pvoid p, size_t old_size, size_t new_size)
31 | { return (((p && old_size) || new_size) ? ::realloc(p, new_size) : NULL); }
32 | };
33 |
34 | namespace use
35 | {
36 | typedef alloc_model alloc_std;
37 | }
38 |
39 | //////////////////////////////////////////////////////////////////////////
40 | NX_END
41 | //////////////////////////////////////////////////////////////////////////
42 |
--------------------------------------------------------------------------------
/nixycore/pattern/iterator.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/general/general.h"
11 | #include "nixycore/utility/utility.h"
12 |
13 | //////////////////////////////////////////////////////////////////////////
14 | NX_BEG
15 | //////////////////////////////////////////////////////////////////////////
16 |
17 | template
18 | struct iterator : public ModelT, nx_operator(iterator, unequal, operable)
19 | {
20 | typedef typename ModelT::type_t type_t;
21 |
22 | iterator(void)
23 | : ModelT(0)
24 | {}
25 |
26 | template
27 | iterator(T v)
28 | : ModelT(v)
29 | {}
30 |
31 | iterator& operator+=(int v)
32 | {
33 | ModelT::operator()(v);
34 | return (*this);
35 | }
36 |
37 | bool operator==(const iterator& y) const
38 | {
39 | return ModelT::operator==(y);
40 | }
41 |
42 | type_t value(void) const
43 | {
44 | return ModelT::value();
45 | }
46 |
47 | type_t operator*(void) const { return value(); }
48 | };
49 |
50 | //////////////////////////////////////////////////////////////////////////
51 | NX_END
52 | //////////////////////////////////////////////////////////////////////////
53 |
--------------------------------------------------------------------------------
/nixycore/pattern/pattern.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | #include "nixycore/pattern/prototype.h"
13 | #include "nixycore/pattern/singleton.h"
14 | #include "nixycore/pattern/iterator.h"
15 | #include "nixycore/pattern/trackable.h"
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 |
--------------------------------------------------------------------------------
/nixycore/pattern/prototype.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/memory/default_alloc.h"
11 |
12 | #include "nixycore/general/general.h"
13 | #include "nixycore/typemanip/typemanip.h"
14 | #include "nixycore/utility/utility.h"
15 |
16 | //////////////////////////////////////////////////////////////////////////
17 | NX_BEG
18 | //////////////////////////////////////////////////////////////////////////
19 |
20 | NX_CONCEPT(clone, clone, C*(C::*)(void))
21 |
22 | template
23 | inline typename enable_if::value,
24 | T*>::type_t clone(T* ob)
25 | {
26 | if (!ob) return nx::nulptr;
27 | return static_cast(ob->clone());
28 | }
29 |
30 | template
31 | inline typename enable_if::value,
32 | T*>::type_t clone(T* ob)
33 | {
34 | if (!ob) return nx::nulptr;
35 | return nx::alloc(nx::ref(*ob));
36 | }
37 |
38 | template
39 | T* clone(T* ob)
40 | {
41 | if (!ob) return nx::nulptr;
42 | return nx::alloc(nx::ref(*ob));
43 | }
44 |
45 | //////////////////////////////////////////////////////////////////////////
46 | NX_END
47 | //////////////////////////////////////////////////////////////////////////
48 |
--------------------------------------------------------------------------------
/nixycore/pattern/trackable.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/pattern/singleton.h"
11 |
12 | #include "nixycore/bugfix/assert.h"
13 |
14 | #include "nixycore/general/general.h"
15 | #include "nixycore/typemanip/typemanip.h"
16 |
17 | //////////////////////////////////////////////////////////////////////////
18 | NX_BEG
19 | //////////////////////////////////////////////////////////////////////////
20 |
21 | template class SingleT = Singleton>
22 | class trackable
23 | {
24 | public:
25 | T * prev_, * next_;
26 |
27 | private:
28 | static T *(& get_head_ptr(void))
29 | {
30 | return SingleT::instance(nx::nulptr);
31 | }
32 |
33 | void init(void)
34 | {
35 | T*(& head) = get_head_ptr();
36 | // check and push self to list
37 | if (head)
38 | {
39 | this->next_ = head;
40 | head->prev_ = static_cast(this);
41 | }
42 | head = static_cast(this);
43 | }
44 |
45 | void dest(void)
46 | {
47 | T*(& head) = get_head_ptr();
48 | // check and pop self from list
49 | if (!head) return;
50 | if (this->prev_)
51 | {
52 | this->prev_->next_ = this->next_;
53 | if (this->next_)
54 | this->next_->prev_ = this->prev_;
55 | }
56 | else
57 | {
58 | nx_assert(this == head);
59 | head = head->next_;
60 | if (head) head->prev_ = nx::nulptr;
61 | }
62 | }
63 |
64 | public:
65 | trackable(void)
66 | : prev_(nx::nulptr)
67 | , next_(nx::nulptr) { init(); }
68 |
69 | trackable(const trackable&)
70 | : prev_(nx::nulptr)
71 | , next_(nx::nulptr) { init(); }
72 |
73 | ~trackable(void) { dest(); }
74 |
75 | trackable& operator=(const trackable&) { return (*this); }
76 |
77 | public:
78 | static T* track(void) { return get_head_ptr(); }
79 | };
80 |
81 | //////////////////////////////////////////////////////////////////////////
82 | NX_END
83 | //////////////////////////////////////////////////////////////////////////
84 |
--------------------------------------------------------------------------------
/nixycore/preprocessor/pp_arg.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | // preprocessor macros
11 | #include "nixycore/preprocessor/pp_macros.h"
12 |
13 | //////////////////////////////////////////////////////////////////////////
14 |
15 | /*
16 | NX_PP_B2(a, b, c, d, e)
17 | -->
18 | c, d, e
19 | */
20 |
21 | #define NX_PP_B_H(a, ...) __VA_ARGS__
22 |
23 | #define NX_PP_B0(...) NX_PP_VA(__VA_ARGS__)
24 | #define NX_PP_B1(...) NX_PP_VA(NX_PP_B_H(__VA_ARGS__))
25 | #define NX_PP_B2(...) NX_PP_B1(NX_PP_B1(__VA_ARGS__))
26 | #define NX_PP_B3(...) NX_PP_B1(NX_PP_B2(__VA_ARGS__))
27 | #define NX_PP_B4(...) NX_PP_B1(NX_PP_B3(__VA_ARGS__))
28 | #define NX_PP_B5(...) NX_PP_B1(NX_PP_B4(__VA_ARGS__))
29 | #define NX_PP_B6(...) NX_PP_B1(NX_PP_B5(__VA_ARGS__))
30 | #define NX_PP_B7(...) NX_PP_B1(NX_PP_B6(__VA_ARGS__))
31 | #define NX_PP_B8(...) NX_PP_B1(NX_PP_B7(__VA_ARGS__))
32 | #define NX_PP_B9(...) NX_PP_B1(NX_PP_B8(__VA_ARGS__))
33 |
34 | #define NX_PP_B10(...) NX_PP_B1(NX_PP_B9(__VA_ARGS__))
35 | #define NX_PP_B11(...) NX_PP_B1(NX_PP_B10(__VA_ARGS__))
36 | #define NX_PP_B12(...) NX_PP_B1(NX_PP_B11(__VA_ARGS__))
37 | #define NX_PP_B13(...) NX_PP_B1(NX_PP_B12(__VA_ARGS__))
38 | #define NX_PP_B14(...) NX_PP_B1(NX_PP_B13(__VA_ARGS__))
39 | #define NX_PP_B15(...) NX_PP_B1(NX_PP_B14(__VA_ARGS__))
40 | #define NX_PP_B16(...) NX_PP_B1(NX_PP_B15(__VA_ARGS__))
41 | #define NX_PP_B17(...) NX_PP_B1(NX_PP_B16(__VA_ARGS__))
42 | #define NX_PP_B18(...) NX_PP_B1(NX_PP_B17(__VA_ARGS__))
43 | #define NX_PP_B19(...) NX_PP_B1(NX_PP_B18(__VA_ARGS__))
44 |
45 | #define NX_PP_B20(...) NX_PP_B1(NX_PP_B19(__VA_ARGS__))
46 |
47 | #define NX_PP_B_P(F, ...) NX_PP_VA(F(__VA_ARGS__))
48 | #define NX_PP_B(n, ...) NX_PP_B_P(NX_PP_JOIN(NX_PP_B, n), __VA_ARGS__)
49 |
50 | //////////////////////////////////////////////////////////////////////////
51 |
52 | /*
53 | NX_PP_A3(a, b, c, d, e)
54 | -->
55 | c
56 | */
57 |
58 | #define NX_PP_A_H(a, ...) a
59 |
60 | #define NX_PP_A0(...)
61 | #define NX_PP_A1(...) NX_PP_VA(NX_PP_A_H(__VA_ARGS__))
62 | #define NX_PP_A2(...) NX_PP_A1(NX_PP_B1(__VA_ARGS__))
63 | #define NX_PP_A3(...) NX_PP_A1(NX_PP_B2(__VA_ARGS__))
64 | #define NX_PP_A4(...) NX_PP_A1(NX_PP_B3(__VA_ARGS__))
65 | #define NX_PP_A5(...) NX_PP_A1(NX_PP_B4(__VA_ARGS__))
66 | #define NX_PP_A6(...) NX_PP_A1(NX_PP_B5(__VA_ARGS__))
67 | #define NX_PP_A7(...) NX_PP_A1(NX_PP_B6(__VA_ARGS__))
68 | #define NX_PP_A8(...) NX_PP_A1(NX_PP_B7(__VA_ARGS__))
69 | #define NX_PP_A9(...) NX_PP_A1(NX_PP_B8(__VA_ARGS__))
70 |
71 | #define NX_PP_A10(...) NX_PP_A1(NX_PP_B9(__VA_ARGS__))
72 | #define NX_PP_A11(...) NX_PP_A1(NX_PP_B10(__VA_ARGS__))
73 | #define NX_PP_A12(...) NX_PP_A1(NX_PP_B11(__VA_ARGS__))
74 | #define NX_PP_A13(...) NX_PP_A1(NX_PP_B12(__VA_ARGS__))
75 | #define NX_PP_A14(...) NX_PP_A1(NX_PP_B13(__VA_ARGS__))
76 | #define NX_PP_A15(...) NX_PP_A1(NX_PP_B14(__VA_ARGS__))
77 | #define NX_PP_A16(...) NX_PP_A1(NX_PP_B15(__VA_ARGS__))
78 | #define NX_PP_A17(...) NX_PP_A1(NX_PP_B16(__VA_ARGS__))
79 | #define NX_PP_A18(...) NX_PP_A1(NX_PP_B17(__VA_ARGS__))
80 | #define NX_PP_A19(...) NX_PP_A1(NX_PP_B18(__VA_ARGS__))
81 |
82 | #define NX_PP_A20(...) NX_PP_A1(NX_PP_B19(__VA_ARGS__))
83 |
84 | #define NX_PP_A_P(F, ...) NX_PP_VA(F(__VA_ARGS__))
85 | #define NX_PP_A(n, ...) NX_PP_A_P(NX_PP_JOIN(NX_PP_A, n), __VA_ARGS__)
86 |
--------------------------------------------------------------------------------
/nixycore/preprocessor/pp_count.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | // preprocessor macros
11 | #include "nixycore/preprocessor/pp_macros.h"
12 |
13 | //////////////////////////////////////////////////////////////////////////
14 |
15 | #define NX_PP_MAX 20
16 |
17 | #define NX_PP_FILTER_( _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
18 | _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
19 | _N, ...) _N
20 |
21 | #define NX_PP_NUMBER_() 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
22 | 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
23 |
24 | /*
25 | Get count of args from __VA_ARGS__
26 |
27 | NX_PP_COUNT(a, b, c, d)
28 | -->
29 | 4
30 | */
31 |
32 | #define NX_PP_HELPER(...) NX_PP_VA(NX_PP_FILTER_(__VA_ARGS__))
33 | #define NX_PP_COUNT(...) NX_PP_HELPER(__VA_ARGS__, NX_PP_NUMBER_())
34 |
--------------------------------------------------------------------------------
/nixycore/preprocessor/pp_macros.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | //////////////////////////////////////////////////////////////////////////
11 |
12 | /*
13 | Get a string of macro
14 | */
15 |
16 | #define NX_PP_SOL(...) #__VA_ARGS__
17 | #define NX_PP_STR_(...) NX_PP_SOL(__VA_ARGS__)
18 | #define NX_PP_STR(...) NX_PP_STR_(__VA_ARGS__)
19 |
20 | /*
21 | Connect two args together
22 | */
23 |
24 | #define NX_PP_CAT(x, ...) x##__VA_ARGS__
25 | #define NX_PP_JOIN_(x, ...) NX_PP_CAT(x, __VA_ARGS__)
26 | #define NX_PP_JOIN(x, ...) NX_PP_JOIN_(x, __VA_ARGS__)
27 |
28 | /*
29 | Circumvent MSVC __VA_ARGS__ BUG
30 | */
31 |
32 | #define NX_PP_VA(...) __VA_ARGS__ /* Try to expand __VA_ARGS__ */
33 | #define NX_PP_PROXY(F, ...) NX_PP_VA(F(__VA_ARGS__))
34 |
--------------------------------------------------------------------------------
/nixycore/preprocessor/pp_mult.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | // preprocessor macros
11 | #include "nixycore/preprocessor/pp_macros.h"
12 |
13 | //////////////////////////////////////////////////////////////////////////
14 |
15 | #define NX_PP_MULT_0(f)
16 |
17 | #define NX_PP_MULT_1(f) f(1)
18 | #define NX_PP_MULT_2(f) NX_PP_MULT_1(f) f(2)
19 | #define NX_PP_MULT_3(f) NX_PP_MULT_2(f) f(3)
20 | #define NX_PP_MULT_4(f) NX_PP_MULT_3(f) f(4)
21 | #define NX_PP_MULT_5(f) NX_PP_MULT_4(f) f(5)
22 | #define NX_PP_MULT_6(f) NX_PP_MULT_5(f) f(6)
23 | #define NX_PP_MULT_7(f) NX_PP_MULT_6(f) f(7)
24 | #define NX_PP_MULT_8(f) NX_PP_MULT_7(f) f(8)
25 | #define NX_PP_MULT_9(f) NX_PP_MULT_8(f) f(9)
26 | #define NX_PP_MULT_10(f) NX_PP_MULT_9(f) f(10)
27 |
28 | #define NX_PP_MULT_11(f) NX_PP_MULT_10(f) f(11)
29 | #define NX_PP_MULT_12(f) NX_PP_MULT_11(f) f(12)
30 | #define NX_PP_MULT_13(f) NX_PP_MULT_12(f) f(13)
31 | #define NX_PP_MULT_14(f) NX_PP_MULT_13(f) f(14)
32 | #define NX_PP_MULT_15(f) NX_PP_MULT_14(f) f(15)
33 | #define NX_PP_MULT_16(f) NX_PP_MULT_15(f) f(16)
34 | #define NX_PP_MULT_17(f) NX_PP_MULT_16(f) f(17)
35 | #define NX_PP_MULT_18(f) NX_PP_MULT_17(f) f(18)
36 | #define NX_PP_MULT_19(f) NX_PP_MULT_18(f) f(19)
37 | #define NX_PP_MULT_20(f) NX_PP_MULT_19(f) f(20)
38 |
39 | /*
40 | NX_PP_MULT(10, f)
41 | -->
42 | f(1) f(2) f(3) f(4) f(5) f(6) f(7) f(8) f(9) f(10)
43 | */
44 |
45 | #define NX_PP_MULT_P(F, ...) NX_PP_VA(F(__VA_ARGS__))
46 | #define NX_PP_MULT(n, f) NX_PP_MULT_P(NX_PP_JOIN(NX_PP_MULT_, n), f)
47 | #define NX_PP_MULT_MAX(f) NX_PP_MULT(NX_PP_MAX, f)
48 |
--------------------------------------------------------------------------------
/nixycore/preprocessor/pp_nest.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | // NX_PP_COUNT
11 | #include "nixycore/preprocessor/pp_count.h"
12 |
13 | //////////////////////////////////////////////////////////////////////////
14 |
15 | #define NX_PP_NEST_0(f, l, n, ...)
16 |
17 | #define NX_PP_NEST_1(f, l, n, ...) NX_PP_VA(l(NX_PP_VA(__VA_ARGS__)))
18 | #define NX_PP_NEST_2(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_1(f, l, n, n(__VA_ARGS__))))
19 | #define NX_PP_NEST_3(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_2(f, l, n, n(__VA_ARGS__))))
20 | #define NX_PP_NEST_4(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_3(f, l, n, n(__VA_ARGS__))))
21 | #define NX_PP_NEST_5(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_4(f, l, n, n(__VA_ARGS__))))
22 | #define NX_PP_NEST_6(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_5(f, l, n, n(__VA_ARGS__))))
23 | #define NX_PP_NEST_7(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_6(f, l, n, n(__VA_ARGS__))))
24 | #define NX_PP_NEST_8(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_7(f, l, n, n(__VA_ARGS__))))
25 | #define NX_PP_NEST_9(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_8(f, l, n, n(__VA_ARGS__))))
26 | #define NX_PP_NEST_10(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_9(f, l, n, n(__VA_ARGS__))))
27 |
28 | #define NX_PP_NEST_11(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_10(f, l, n, n(__VA_ARGS__))))
29 | #define NX_PP_NEST_12(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_11(f, l, n, n(__VA_ARGS__))))
30 | #define NX_PP_NEST_13(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_12(f, l, n, n(__VA_ARGS__))))
31 | #define NX_PP_NEST_14(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_13(f, l, n, n(__VA_ARGS__))))
32 | #define NX_PP_NEST_15(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_14(f, l, n, n(__VA_ARGS__))))
33 | #define NX_PP_NEST_16(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_15(f, l, n, n(__VA_ARGS__))))
34 | #define NX_PP_NEST_17(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_16(f, l, n, n(__VA_ARGS__))))
35 | #define NX_PP_NEST_18(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_17(f, l, n, n(__VA_ARGS__))))
36 | #define NX_PP_NEST_19(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_18(f, l, n, n(__VA_ARGS__))))
37 | #define NX_PP_NEST_20(f, l, n, ...) NX_PP_VA(f(NX_PP_VA(__VA_ARGS__), NX_PP_NEST_19(f, l, n, n(__VA_ARGS__))))
38 |
39 | /*
40 | NX_PP_NEST(3, func, last, nest, xx)
41 | -->
42 | func(xx, func(nest(xx), last(nest(nest(xx)))))
43 | */
44 |
45 | #define NX_PP_NEST_P(F, ...) NX_PP_VA(F(__VA_ARGS__))
46 | #define NX_PP_NEST(n, func, last, nest, ...) NX_PP_NEST_P(NX_PP_JOIN(NX_PP_NEST_, n), func, last, nest, __VA_ARGS__)
47 | #define NX_PP_NEST_MAX(func, last, nest, ...) NX_PP_NEST(NX_PP_MAX, func, last, nest, __VA_ARGS__)
48 |
--------------------------------------------------------------------------------
/nixycore/preprocessor/pp_repeat.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | // preprocessor macros
11 | #include "nixycore/preprocessor/pp_macros.h"
12 |
13 | //////////////////////////////////////////////////////////////////////////
14 |
15 | #define NX_PP_REPEAT_0(f1, f2, ...)
16 |
17 | #define NX_PP_REPEAT_1(f1, f2, ...) NX_PP_VA(f1(1, __VA_ARGS__))
18 | #define NX_PP_REPEAT_2(f1, f2, ...) NX_PP_REPEAT_1(f1, f2, __VA_ARGS__) NX_PP_VA(f2(2, __VA_ARGS__))
19 | #define NX_PP_REPEAT_3(f1, f2, ...) NX_PP_REPEAT_2(f1, f2, __VA_ARGS__) NX_PP_VA(f2(3, __VA_ARGS__))
20 | #define NX_PP_REPEAT_4(f1, f2, ...) NX_PP_REPEAT_3(f1, f2, __VA_ARGS__) NX_PP_VA(f2(4, __VA_ARGS__))
21 | #define NX_PP_REPEAT_5(f1, f2, ...) NX_PP_REPEAT_4(f1, f2, __VA_ARGS__) NX_PP_VA(f2(5, __VA_ARGS__))
22 | #define NX_PP_REPEAT_6(f1, f2, ...) NX_PP_REPEAT_5(f1, f2, __VA_ARGS__) NX_PP_VA(f2(6, __VA_ARGS__))
23 | #define NX_PP_REPEAT_7(f1, f2, ...) NX_PP_REPEAT_6(f1, f2, __VA_ARGS__) NX_PP_VA(f2(7, __VA_ARGS__))
24 | #define NX_PP_REPEAT_8(f1, f2, ...) NX_PP_REPEAT_7(f1, f2, __VA_ARGS__) NX_PP_VA(f2(8, __VA_ARGS__))
25 | #define NX_PP_REPEAT_9(f1, f2, ...) NX_PP_REPEAT_8(f1, f2, __VA_ARGS__) NX_PP_VA(f2(9, __VA_ARGS__))
26 | #define NX_PP_REPEAT_10(f1, f2, ...) NX_PP_REPEAT_9(f1, f2, __VA_ARGS__) NX_PP_VA(f2(10, __VA_ARGS__))
27 |
28 | #define NX_PP_REPEAT_11(f1, f2, ...) NX_PP_REPEAT_10(f1, f2, __VA_ARGS__) NX_PP_VA(f2(11, __VA_ARGS__))
29 | #define NX_PP_REPEAT_12(f1, f2, ...) NX_PP_REPEAT_11(f1, f2, __VA_ARGS__) NX_PP_VA(f2(12, __VA_ARGS__))
30 | #define NX_PP_REPEAT_13(f1, f2, ...) NX_PP_REPEAT_12(f1, f2, __VA_ARGS__) NX_PP_VA(f2(13, __VA_ARGS__))
31 | #define NX_PP_REPEAT_14(f1, f2, ...) NX_PP_REPEAT_13(f1, f2, __VA_ARGS__) NX_PP_VA(f2(14, __VA_ARGS__))
32 | #define NX_PP_REPEAT_15(f1, f2, ...) NX_PP_REPEAT_14(f1, f2, __VA_ARGS__) NX_PP_VA(f2(15, __VA_ARGS__))
33 | #define NX_PP_REPEAT_16(f1, f2, ...) NX_PP_REPEAT_15(f1, f2, __VA_ARGS__) NX_PP_VA(f2(16, __VA_ARGS__))
34 | #define NX_PP_REPEAT_17(f1, f2, ...) NX_PP_REPEAT_16(f1, f2, __VA_ARGS__) NX_PP_VA(f2(17, __VA_ARGS__))
35 | #define NX_PP_REPEAT_18(f1, f2, ...) NX_PP_REPEAT_17(f1, f2, __VA_ARGS__) NX_PP_VA(f2(18, __VA_ARGS__))
36 | #define NX_PP_REPEAT_19(f1, f2, ...) NX_PP_REPEAT_18(f1, f2, __VA_ARGS__) NX_PP_VA(f2(19, __VA_ARGS__))
37 | #define NX_PP_REPEAT_20(f1, f2, ...) NX_PP_REPEAT_19(f1, f2, __VA_ARGS__) NX_PP_VA(f2(20, __VA_ARGS__))
38 |
39 | /*
40 | NX_PP_REPEAT(5, f, data)
41 | -->
42 | f(1, data) f(2, data) f(3, data) f(4, data) f(5, data)
43 | */
44 |
45 | #define NX_PP_REPEAT_P(F, ...) NX_PP_VA(F(__VA_ARGS__))
46 |
47 | #define NX_PP_REPEATEX(n, f1, f2, ...) NX_PP_REPEAT_P(NX_PP_JOIN(NX_PP_REPEAT_, n), f1, f2, __VA_ARGS__)
48 | #define NX_PP_REPEATEX_MAX(f1, f2, ...) NX_PP_REPEATEX(NX_PP_MAX, f1, f2, __VA_ARGS__)
49 |
50 | #define NX_PP_REPEAT(n, f, ...) NX_PP_REPEATEX(n, f, f, __VA_ARGS__)
51 | #define NX_PP_REPEAT_MAX(f, ...) NX_PP_REPEATEX_MAX(f, f, __VA_ARGS__)
52 |
--------------------------------------------------------------------------------
/nixycore/random/rand_std.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/typemanip/typedefs.h"
11 |
12 | #include "nixycore/general/general.h"
13 |
14 | //////////////////////////////////////////////////////////////////////////
15 | NX_BEG namespace use {
16 | //////////////////////////////////////////////////////////////////////////
17 |
18 | /*
19 | Standard random policy
20 | */
21 |
22 | struct rand_std
23 | {
24 | typedef int rand_t;
25 |
26 | NX_STATIC_PROPERTY(rand_t, MAX, RAND_MAX);
27 |
28 | void srand(nx::uint sd)
29 | {
30 | ::srand(sd);
31 | }
32 |
33 | rand_t rand(void)
34 | {
35 | return ::rand();
36 | }
37 | };
38 |
39 | //////////////////////////////////////////////////////////////////////////
40 | } NX_END
41 | //////////////////////////////////////////////////////////////////////////
42 |
--------------------------------------------------------------------------------
/nixycore/random/random.h:
--------------------------------------------------------------------------------
1 | /*
2 | The Nixy Library
3 | Code covered by the MIT License
4 |
5 | Author: mutouyun (http://orzz.org)
6 | */
7 |
8 | #pragma once
9 |
10 | #include "nixycore/random/rand_std.h"
11 | #include "nixycore/random/rand_mt19937.h"
12 |
13 | #include "nixycore/time/tickcount.h"
14 |
15 | #include "nixycore/general/general.h"
16 | #include "nixycore/typemanip/typemanip.h"
17 |
18 | //////////////////////////////////////////////////////////////////////////
19 | NX_BEG
20 | //////////////////////////////////////////////////////////////////////////
21 |
22 | #ifndef NX_DEFAULT_RAND
23 | #define NX_DEFAULT_RAND nx::use::rand_mt19937
24 | #endif
25 |
26 | template
27 | class random : public ModelT
28 | {
29 | protected:
30 | int lower_, upper_;
31 |
32 | double roll(void)
33 | {
34 | return (ModelT::rand() + 0.5) / (ModelT::MAX + 1.0);
35 | }
36 |
37 | public:
38 | random(int lower = 0, int upper = ModelT::MAX)
39 | : lower_(0), upper_(0)
40 | {
41 | range(lower, upper);
42 | srand();
43 | }
44 |
45 | void range(int lower = 0, int upper = ModelT::MAX)
46 | {
47 | lower_ = lower;
48 | upper_ = upper;
49 | }
50 |
51 | void srand(nx::uint seed = 0)
52 | {
53 | ModelT::srand(seed ? seed : static_cast(tickcount()));
54 | }
55 |
56 | template