From 109da0a80973132a931031f866400166d1ec423d Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 21 Oct 2026 09:38:05 +0800 Subject: [PATCH] refactor(sdk)!: replace legacy utility macros with standard calls Use std::to_string for existing data and endpoint conversions and std::size for bounded lookup arrays, with direct standard-library includes. Preserve argument types, numeric formatting, lookup storage types, enum mappings and range checks. Remove the unused static-assert and byte-clearing aliases, and document source migration in English and Chinese without changing the remaining macro contracts. BREAKING CHANGE: ARK_TO_STRING, ARRAY_LENTGH, ARK_SRATIC_ASSERT, ARRAY_CLEAR, MEMORY_CLEAR and MEMORY_CLEAR_POINTER are no longer defined. Use std::to_string, std::size or the appropriate type-extent query, and static_assert directly. Downstream uses of the clearing helpers must select a reset operation suitable for their storage contract; MEMORY_CLEAR_POINTER contained an invalid 0xx token. --- docs/code-style.md | 18 ++++++++++++++++++ docs/code-style_CN.md | 16 ++++++++++++++++ include/ark/kernel/data_impl.hpp | 16 +++++++++------- include/ark/kernel/data_list_impl.hpp | 16 +++++++++------- include/ark/macros.hpp | 8 -------- include/ark/misc.hpp | 4 +++- include/ark/socket_func.hpp | 11 +++++++---- .../net/net_service_manager_module_impl.cpp | 2 +- 8 files changed, 63 insertions(+), 28 deletions(-) diff --git a/docs/code-style.md b/docs/code-style.md index 5203711f2..c551bd466 100644 --- a/docs/code-style.md +++ b/docs/code-style.md @@ -87,3 +87,21 @@ and argument deduction remain unchanged: a const integer lvalue can deduce the unqualified type through `const T&`, while explicitly checking a const-qualified trait remains false. Constant types and values, including UID bit allocation and network timeouts, remain unchanged. + +## Standard utility migration + +**Breaking source change:** `ARK_TO_STRING`, `ARRAY_LENTGH`, `ARK_SRATIC_ASSERT`, +`ARRAY_CLEAR`, `MEMORY_CLEAR` and `MEMORY_CLEAR_POINTER` are no longer defined. + +| Removed name | Migration | +| --- | --- | +| `ARK_TO_STRING(value)` | Include `` and call `std::to_string(value)`. This is the same standard function previously used by the alias, including its C++20 floating-point formatting. | +| `ARRAY_LENTGH(array)` | Include `` and use `std::size(array)` for bounded arrays. If code needs a type's first array extent, include `` and use `std::extent_v`; `std::size` does not accept a raw pointer. | +| `ARK_SRATIC_ASSERT` | Write `static_assert` directly. | +| `ARRAY_CLEAR`, `MEMORY_CLEAR`, `MEMORY_CLEAR_POINTER` | These unused helpers are removed without a replacement alias. Choose value initialization, typed assignment or an explicit byte operation according to the object's storage contract; there is no universal safe object-clearing operation. `MEMORY_CLEAR_POINTER` also contained the invalid token `0xx`. | + +The existing data and endpoint conversions still call `std::to_string` with the +same argument types. Numeric output and precision remain unchanged. Array-count +call sites keep their existing `int` variables, lookup order, enum mappings and +range handling. Remaining allocation, ownership, runtime-assertion, platform, +plugin-registration and byte-order macros retain their existing behavior. diff --git a/docs/code-style_CN.md b/docs/code-style_CN.md index 94a25abd2..760b4be52 100644 --- a/docs/code-style_CN.md +++ b/docs/code-style_CN.md @@ -77,3 +77,19 @@ clang-tidy 应与 CMake 配置使用同一套编译器工具链,以正确找 `check_buildin_type` 的签名、异常契约、参数推导保持不变:const 整数左值可通过 `const T&` 推导出未限定的类型,但显式检查 const 限定的 trait 仍返回 false。 常量类型和值保持不变,包括 UID 位分配和网络超时。 + +## 标准工具迁移 + +**源码不兼容变更:** 不再定义 `ARK_TO_STRING`、`ARRAY_LENTGH`、 +`ARK_SRATIC_ASSERT`、`ARRAY_CLEAR`、`MEMORY_CLEAR` 和 `MEMORY_CLEAR_POINTER`。 + +| 移除的名称 | 迁移方式 | +| --- | --- | +| `ARK_TO_STRING(value)` | 包含 ``,调用 `std::to_string(value)`。它与原别名调用的标准函数相同,包括 C++20 的浮点格式化行为。 | +| `ARRAY_LENTGH(array)` | 对有界数组,包含 `` 并使用 `std::size(array)`。若需要查询类型的首维数组长度,包含 `` 并使用 `std::extent_v`;`std::size` 不接受裸指针。 | +| `ARK_SRATIC_ASSERT` | 直接使用 `static_assert`。 | +| `ARRAY_CLEAR`、`MEMORY_CLEAR`、`MEMORY_CLEAR_POINTER` | 移除未使用的工具,不提供替代别名。根据对象的存储契约选择值初始化、类型明确的赋值或显式字节操作;不存在对所有对象都安全的清零操作。`MEMORY_CLEAR_POINTER` 还包含非法 token `0xx`。 | + +现有数据和端点转换仍以相同的参数类型调用 `std::to_string`,数值输出和精度不变。 +数组长度调用点保留原有的 `int` 变量、查找顺序、枚举映射和范围处理。 +其余分配、所有权、运行时断言、平台、插件注册和字节序宏保持原有行为。 diff --git a/include/ark/kernel/data_impl.hpp b/include/ark/kernel/data_impl.hpp index 70fc2c4e6..b6c1e47ba 100644 --- a/include/ark/kernel/data_impl.hpp +++ b/include/ark/kernel/data_impl.hpp @@ -20,6 +20,8 @@ #pragma once +#include + #include "ark/core_def.hpp" #include "ark/kernel/data.hpp" #include "ark/misc.hpp" @@ -546,25 +548,25 @@ class data_base final : public data switch (get_type()) { case data_type::dt_boolean: - return ARK_TO_STRING(this->bool_value_); + return std::to_string(this->bool_value_); break; case data_type::dt_int32: - return ARK_TO_STRING(this->int_value_); + return std::to_string(this->int_value_); break; case data_type::dt_int64: - return ARK_TO_STRING(this->int64_value_); + return std::to_string(this->int64_value_); break; case data_type::dt_uint32: - return ARK_TO_STRING(this->uint32_value_); + return std::to_string(this->uint32_value_); break; case data_type::dt_uint64: - return ARK_TO_STRING(this->uint64_value_); + return std::to_string(this->uint64_value_); break; case data_type::dt_float: - return ARK_TO_STRING(this->float_value_); + return std::to_string(this->float_value_); break; case data_type::dt_double: - return ARK_TO_STRING(this->double_value_); + return std::to_string(this->double_value_); break; case data_type::dt_string: return this->string_value_; diff --git a/include/ark/kernel/data_list_impl.hpp b/include/ark/kernel/data_list_impl.hpp index 6b137ce99..909a94fc9 100644 --- a/include/ark/kernel/data_list_impl.hpp +++ b/include/ark/kernel/data_list_impl.hpp @@ -20,6 +20,8 @@ #pragma once +#include + #include "ark/kernel/data.hpp" #include "ark/kernel/data_list.hpp" #include "ark/misc.hpp" @@ -739,25 +741,25 @@ class base_data_list final : public data_list switch (data_[index].type) { case data_type::dt_boolean: - data = ARK_TO_STRING(data_[index].bool_value); + data = std::to_string(data_[index].bool_value); break; case data_type::dt_int32: - data = ARK_TO_STRING(data_[index].int_value); + data = std::to_string(data_[index].int_value); break; case data_type::dt_int64: - data = ARK_TO_STRING(data_[index].int64_value); + data = std::to_string(data_[index].int64_value); break; case data_type::dt_uint32: - data = ARK_TO_STRING(data_[index].uint32_value); + data = std::to_string(data_[index].uint32_value); break; case data_type::dt_uint64: - data = ARK_TO_STRING(data_[index].uint64_value); + data = std::to_string(data_[index].uint64_value); break; case data_type::dt_float: - data = ARK_TO_STRING(data_[index].float_value); + data = std::to_string(data_[index].float_value); break; case data_type::dt_double: - data = ARK_TO_STRING(data_[index].double_value); + data = std::to_string(data_[index].double_value); break; case data_type::dt_string: data = string(index); diff --git a/include/ark/macros.hpp b/include/ark/macros.hpp index 2a42c588e..3331fe9cd 100644 --- a/include/ark/macros.hpp +++ b/include/ark/macros.hpp @@ -39,11 +39,6 @@ #define INOUT #endif -#define ARRAY_CLEAR(v) memset((v), 0x0, sizeof((v))) -#define MEMORY_CLEAR(v) memset(&(v), 0x0, sizeof((v))) -#define MEMORY_CLEAR_POINTER(v) memset((v), 0xx, sizeof(*(v))) -#define ARRAY_LENTGH(v) std::extent::value - #define MAX_NAME 256 #define MAX_BUF 256 @@ -326,8 +321,6 @@ typedef struct HINSTANCE__* library_instance; } while (false); #endif -#define ARK_TO_STRING(value) std::to_string(value) - /// math: is zero #define IS_FLOAT_ZERO(type, value) (std::abs(value) < std::numeric_limits::epsilon()) @@ -337,7 +330,6 @@ typedef struct HINSTANCE__* library_instance; #define ARK_FORMAT_FUNCTION(my_fmt, ...) \ fmt::format(fmt::runtime(std::string("[{}:{}]") + (my_fmt)), ARK_FUNCTION_LINE, ##__VA_ARGS__); -#define ARK_SRATIC_ASSERT static_assert #define GET_CLASS_NAME(class_name) (typeid(class_name).name()) ////////////////////////////////////////////////////////////////////////// // Plugin macros diff --git a/include/ark/misc.hpp b/include/ark/misc.hpp index ecd412eb3..759d05d90 100644 --- a/include/ark/misc.hpp +++ b/include/ark/misc.hpp @@ -20,6 +20,8 @@ #pragma once +#include + #include "ark/bus.hpp" #include "ark/enum.hpp" #include "ark/macros.hpp" @@ -110,7 +112,7 @@ class misc // If there are more types, please add type name in here. }; - static int data_type_len = ARRAY_LENTGH(data_type_names); + static int data_type_len = std::size(data_type_names); for (int i = 0; i < data_type_len; ++i) { if (type_name == data_type_names[i]) diff --git a/include/ark/socket_func.hpp b/include/ark/socket_func.hpp index e9acfea8f..e6ce531a9 100644 --- a/include/ark/socket_func.hpp +++ b/include/ark/socket_func.hpp @@ -20,6 +20,9 @@ #pragma once +#include +#include + #include "ark/dep.hpp" #include "ark/error.hpp" #include "ark/macros.hpp" @@ -63,7 +66,7 @@ class endpoint final { public: static std::string make_url(const std::string& protocol, const std::string& hostname, const uint16_t& port) { - std::string domain = protocol + "://" + hostname + ":" + ARK_TO_STRING(port); + std::string domain = protocol + "://" + hostname + ":" + std::to_string(port); return domain; } @@ -79,7 +82,7 @@ class endpoint final "zk", }; - static int n = ARRAY_LENTGH(proto_type_s); + static int n = std::size(proto_type_s); for (int i = 0; i < n; ++i) { if (ARK_STRICMP(s.c_str(), proto_type_s[i]) == 0) @@ -103,7 +106,7 @@ class endpoint final "zk", }; - static int n = ARRAY_LENTGH(proto_type_s); + static int n = std::size(proto_type_s); if ((int)proto >= n) { return proto_type_s[0]; @@ -185,7 +188,7 @@ class endpoint final url += socket::proto2str(proto()) + "://"; } - url += get_ip() + ":" + ARK_TO_STRING(get_port()) + get_path(); + url += get_ip() + ":" + std::to_string(get_port()) + get_path(); return url; } diff --git a/src/plugins/net/net_service_manager_module_impl.cpp b/src/plugins/net/net_service_manager_module_impl.cpp index fbcea2381..1a865f747 100644 --- a/src/plugins/net/net_service_manager_module_impl.cpp +++ b/src/plugins/net/net_service_manager_module_impl.cpp @@ -300,7 +300,7 @@ std::shared_ptr net_service_manager_module_impl::get_client_ // auto check = service.mutable_check(); // check->set_id(app_whole_name + "_check"); // check->set_name(app_whole_name); -// check->set_tcp(self_proc.intranet_ep.GetIP() + ":" + ARK_TO_STRING(self_proc.intranet_ep.GetPort())); +// check->set_tcp(self_proc.intranet_ep.GetIP() + ":" + std::to_string(self_proc.intranet_ep.GetPort())); // check->set_interval(reg_center.check_interval); // check->set_timeout(reg_center.check_timeout); //