NX 1.0.3
niu2x's base c++ helper library
载入中...
搜索中...
未找到
type.h
浏览该文件的文档.
1#pragma once
2
3#include <string.h>
4#include <stdarg.h>
5
6#include <cstddef>
7#include <cstdint>
8#include <vector>
9#include <queue>
10#include <map>
11#include <type_traits>
12#include <unordered_map>
13#include <algorithm>
14#include <functional>
15#include <memory>
16#include <optional>
17#include <variant>
18#include <regex>
19#include <list>
20#include <chrono>
21#include <string>
22#include <stdexcept>
23#include <variant>
24#include <chrono>
25#include <nx/api.h>
26
27#define NX_PLATFORM_WINDOW 1
28#define NX_PLATFORM_LINUX 2
29
30#if defined(WIN32)
31 #define NX_PLATFORM NX_PLATFORM_WINDOW
32#else
33 #define NX_PLATFORM NX_PLATFORM_LINUX
34#endif
35
36namespace nx {
37
41using ByteBuffer = std::vector<uint8_t>;
42
43template <class T>
44using Optional = std::optional<T>;
45
46template <class T>
47using Vector = std::vector<T>;
48
49template <class T>
50using Queue = std::queue<T>;
51
52using String = std::string;
53
54template <class... T>
55using Variant = std::variant<T...>;
56
57template <class T>
58using UniquePtr = std::unique_ptr<T>;
59
60template <class T>
61using SharedPtr = std::shared_ptr<T>;
62
63template <class K, class V>
64using Map = std::map<K, V>;
65
66template <class T>
67using Function = std::function<T>;
68
69template <class T>
70using List = std::list<T>;
71
75class NX_API Uncopyable {
76public:
77 Uncopyable() = default;
78 ~Uncopyable() = default;
79
80 Uncopyable(const Uncopyable&) = delete;
81 Uncopyable& operator=(const Uncopyable&) = delete;
82
83 Uncopyable(Uncopyable&&) = default;
85};
86
87
88NX_API void panic_fmt(const char* fmt, ...);
89
90constexpr size_t operator""_kb(unsigned long long int n) { return n * 1024; }
91
95enum class OpenMode {
96 WRITE,
97 READ,
98};
99
104 size_t bytes;
105};
106
110struct EndOfFile {
111};
112
116enum class IO_Error {
117 NOT_OPEN,
118 IO_FAIL,
119};
120
126
127class NX_API Read {
128public:
129 virtual ~Read() = 0;
130 virtual ReadResult read(void* buffer, size_t bytes) = 0;
131 bool read_exact(void* buffer, size_t bytes);
133};
134
139
140class NX_API Write {
141public:
142 virtual ~Write() = 0;
143 virtual WriteResult write(const void* buffer, size_t bytes) = 0;
144 virtual bool write_all(const void* buffer, size_t bytes);
145};
146
155NX_API bool pipe(Read& reader, Write& writer);
156NX_API bool pipe(Read* reader, Write* writer);
157
158class NX_API MemoryFile : public Read, private Uncopyable {
159public:
160 MemoryFile(const uint8_t* buffer, size_t buf_len);
161 ReadResult read(void* buffer, size_t bytes) override;
162
163private:
164 const uint8_t* buffer_;
165 size_t buf_len_;
166 size_t read_pos_;
167};
168
169using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
170
174using TimeDuration = int64_t;
175
181inline TimePoint time_now() { return std::chrono::system_clock::now(); }
182
191inline TimeDuration time_diff(const TimePoint& t_old, const TimePoint& t_new)
192{
193 using std_ms = std::chrono::milliseconds;
194 return std::chrono::duration_cast<std_ms>(t_new - t_old).count();
195}
196
198{
199 using std_ms = std::chrono::milliseconds;
200 return std::chrono::duration_cast<std_ms>(t.time_since_epoch()).count();
201}
202
203inline uint8_t ceil_pow2(uint8_t n)
204{
205 n -= 1;
206 n |= n >> 1;
207 n |= n >> 2;
208 n |= n >> 4;
209 return n + 1;
210}
211
212inline uint16_t ceil_pow2(uint16_t n)
213{
214 n -= 1;
215 n |= n >> 1;
216 n |= n >> 2;
217 n |= n >> 4;
218 n |= n >> 8;
219 return n + 1;
220}
221
222inline uint32_t ceil_pow2(uint32_t n)
223{
224 n -= 1;
225 n |= n >> 1;
226 n |= n >> 2;
227 n |= n >> 4;
228 n |= n >> 8;
229 n |= n >> 16;
230 return n + 1;
231}
232
233inline uint64_t ceil_pow2(uint64_t n)
234{
235 n -= 1;
236 n |= n >> 1;
237 n |= n >> 2;
238 n |= n >> 4;
239 n |= n >> 8;
240 n |= n >> 16;
241 n |= n >> 32;
242 return n + 1;
243}
244
245template<class T>
246inline bool is_pow2(T x) { return ((x - 1) & x) == 0; }
247
248// using PrintLike = void (*)(const char* fmt, ...);
249// NX_API void set_error_log(PrintLike fn);
250// NX_API void set_no_error_log();
251
252NX_API ByteBuffer zlib_compress(const uint8_t* buf, size_t len);
253NX_API ByteBuffer zlib_uncompress(const uint8_t* buf, size_t len);
254
255} // namespace nx
256
262#define NX_PANIC(msg, ...) nx::panic_fmt(msg, ##__VA_ARGS__)
263
264#define NX_ASSERT(cond, msg, ...) \
265 if (!(cond)) { \
266 NX_PANIC(msg " file:%s line:%d", ##__VA_ARGS__, __FILE__, __LINE__); \
267 }
268
277#define NX_GET_BIT(x, bit) (((x) >> (bit)) & 1)
278#define NX_SET_BIT(x, bit) ((x) | (1 << (bit)))
279#define NX_CLEAR_BIT(x, bit) ((x) & (~(1 << (bit))))
280
289#define NX_GET_BIT_BOOL(x, bit) (NX_GET_BIT((x), (bit)) == 1)
290#define NX_SET_BIT_BOOL(x, bit, value) \
291 ((value) ? NX_SET_BIT((x), (bit)) : NX_CLEAR_BIT((x), (bit)))
Definition type.h:158
MemoryFile(const uint8_t *buffer, size_t buf_len)
ReadResult read(void *buffer, size_t bytes) override
Definition type.h:127
virtual ReadResult read(void *buffer, size_t bytes)=0
ReadAllResult read_all()
bool read_exact(void *buffer, size_t bytes)
virtual ~Read()=0
用于被private继承, 子类将不可被Copy
Definition type.h:75
Uncopyable & operator=(Uncopyable &&)=default
Uncopyable(const Uncopyable &)=delete
Uncopyable(Uncopyable &&)=default
Uncopyable & operator=(const Uncopyable &)=delete
Uncopyable()=default
~Uncopyable()=default
Definition type.h:140
virtual WriteResult write(const void *buffer, size_t bytes)=0
virtual bool write_all(const void *buffer, size_t bytes)
virtual ~Write()=0
root namespace
Definition alias.h:9
uint8_t ceil_pow2(uint8_t n)
Definition type.h:203
std::list< T > List
Definition type.h:70
OpenMode
Open Mode
Definition type.h:95
TimeDuration time_diff(const TimePoint &t_old, const TimePoint &t_new)
calculate time duration before old and now
Definition type.h:191
std::optional< T > Optional
Definition type.h:44
NX_API ByteBuffer zlib_compress(const uint8_t *buf, size_t len)
std::vector< uint8_t > ByteBuffer
byte array
Definition type.h:41
std::queue< T > Queue
Definition type.h:50
Variant< IO_Error, ByteBuffer > ReadAllResult
Definition type.h:125
IO_Error
IO Error
Definition type.h:116
NX_API bool pipe(Read &reader, Write &writer)
pipe data from reader to writer
std::string String
Definition type.h:52
int64_t TimeDuration
milliseconds
Definition type.h:174
NX_API void panic_fmt(const char *fmt,...)
std::variant< T... > Variant
Definition type.h:55
NX_API ByteBuffer zlib_uncompress(const uint8_t *buf, size_t len)
std::map< K, V > Map
Definition type.h:64
TimePoint time_now()
get current clock time
Definition type.h:181
std::chrono::time_point< std::chrono::system_clock > TimePoint
Definition type.h:169
std::unique_ptr< T > UniquePtr
Definition type.h:58
TimeDuration time_diff_epoch(const TimePoint &t)
Definition type.h:197
std::function< T > Function
Definition type.h:67
Variant< IO_Error, EndOfFile, IO_Success > ReadResult
Result of a read operation
Definition type.h:124
bool is_pow2(T x)
Definition type.h:246
Variant< IO_Error, IO_Success > WriteResult
Result of a write operation
Definition type.h:138
std::vector< T > Vector
Definition type.h:47
std::shared_ptr< T > SharedPtr
Definition type.h:61
End of file, a Unit struct.
Definition type.h:110
Result of successful io operation
Definition type.h:103
size_t bytes
Definition type.h:104