Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Util.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
22#pragma once
23
24#include "sese/Config.h"
25#include "sese/io/InputStream.h"
27
28#include <chrono>
29#include <thread>
30
31using namespace std::chrono_literals;
32
33namespace sese {
34
35// 使用预声明减少头文件引用
36enum class Level;
37
42int64_t toInteger(const std::string &string, int radix = 10);
43
47std::string getClassName(const std::type_info *type);
48
52struct StrCmp {
53 int operator()(char const *lv, char const *rv) const;
54 int operator()(const std::string &lv, const std::string &rv) const;
55};
56
60struct StrCmpI {
61 int operator()(char const *lv, char const *rv) const;
62 int operator()(const std::string &lv, const std::string &rv) const;
63};
64
71bool strcmp(char const *lv, char const *rv) noexcept;
72
79bool strcmpDoNotCase(char const *lv, char const *rv) noexcept;
80
86bool isSpace(char ch) noexcept;
87
93const char *getLevelString(Level level) noexcept;
94
101int32_t findFirstAt(const char *str, char ch);
102
107void sleep(uint32_t second);
108
112template<class REP, class PERIOD>
113void sleep(const std::chrono::duration<REP, PERIOD> &duration) {
114 std::this_thread::sleep_for(duration);
115}
116
122std::string getErrorString(int64_t error = errno);
123
128int32_t getErrorCode();
129
135size_t streamMove(sese::io::OutputStream *out, sese::io::InputStream *in, size_t size) noexcept;
136
143template<class T>
144size_t number2StringLength(T num, size_t radix = 10) {
145 static_assert(!std::is_same_v<T, double>, "Must be an integer");
146 static_assert(!std::is_same_v<T, float>, "Must be an integer");
147 size_t length = 0;
148 if (num == 0) return 1;
149 if (num < 0) {
150 length += 1;
151 num *= -1;
152 }
153 do {
154 num /= static_cast<T>(radix);
155 length += 1;
156 } while (num > 0);
157 return length;
158}
159
166template<class T>
167size_t floating2StringLength(T num, uint16_t placeholder) {
168 static_assert(std::is_same_v<T, double> || std::is_same_v<T, float>, "Must be a floating number");
169 size_t length = number2StringLength(static_cast<int64_t>(num));
170 if (placeholder > 0) {
171 length += 1 + placeholder;
172 }
173 return length;
174}
175
176// GCOVR_EXCL_START
182template<typename TP>
183std::time_t to_time_t(TP tp) {
184 using namespace std::chrono;
185 auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now());
186 return system_clock::to_time_t(sctp);
187}
188// GCOVR_EXCL_STOP
189
191template <typename T>
193 if constexpr (std::is_unsigned_v<T>) {
194 return a > std::numeric_limits<T>::max() - b;
195 } else {
196 if ((b > 0 && a > std::numeric_limits<T>::max() - b) ||
197 (b < 0 && a < std::numeric_limits<T>::min() - b)) {
198 return true;
199 }
200 }
201 return false;
202}
203
205template <typename T>
207 if constexpr (std::is_unsigned<T>::value) {
208 return a < b;
209 } else {
210 if ((b < 0 && a > std::numeric_limits<T>::max() + b) ||
211 (b > 0 && a < std::numeric_limits<T>::min() + b)) {
212 return true;
213 }
214 }
215 return false;
216}
217
218} // namespace sese
219
224extern "C" const char *getSpecificVersion();
225
226#ifdef _WIN32
227#pragma warning(disable : 4996)
228#endif
229
233#define CheckRange(x, max) ((unsigned int) (x) < (max))
234
238#define CheckRangeBetween(x, min, max) (((x) - (min)) | ((max) - (x)) >= 0)