From 2d46f9e20925535aae2602a02621e559756b31c3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 17 Jan 2023 02:34:54 +0800 Subject: [PATCH] update: interface of CommonCode --- src/common_code/common_code.cc | 32 ++++++++++++++++++++++---------- src/common_code/common_code.h | 15 ++++++++------- src/common_code/serialize.cc | 4 ++++ src/main.cc | 6 ++++-- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/common_code/common_code.cc b/src/common_code/common_code.cc index f83d43b..237a10c 100644 --- a/src/common_code/common_code.cc +++ b/src/common_code/common_code.cc @@ -2,22 +2,34 @@ #include "common.h" #include "common_code.h" -uint64_t CommonCode::unwrap() const { // get raw uint64_t code - return code; +uint64_t CommonCode::unwrap() const { + return code; // raw uint64_t code } -RawCode CommonCode::to_raw_code() const { // convert to raw code - return RawCode(*this); +RawCode CommonCode::to_raw_code() const { + return RawCode(*this); // convert to raw code } -ShortCode CommonCode::to_short_code() const { // convert to short code - return ShortCode(*this); +ShortCode CommonCode::to_short_code() const { + return ShortCode(*this); // convert to short code } -CommonCode CommonCode::unsafe_create(uint64_t code) { // create CommonCode without check - auto common_code = CommonCode(); // init directly - common_code.code = code; - return common_code; +CommonCode CommonCode::create(uint64_t common_code) { + return CommonCode(common_code); // create from uint64_t +} + +CommonCode CommonCode::from_raw_code(const RawCode &raw_code) { + return raw_code.to_common_code(); // create from raw code +} + +CommonCode CommonCode::from_short_code(const ShortCode &short_code) { + return short_code.to_common_code(); // create from short code +} + +CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check + auto common = CommonCode(); // init directly + common.code = common_code; + return common; } CommonCode::CommonCode(uint64_t common_code) { diff --git a/src/common_code/common_code.h b/src/common_code/common_code.h index 87b2881..65f1cdc 100644 --- a/src/common_code/common_code.h +++ b/src/common_code/common_code.h @@ -11,22 +11,23 @@ class ShortCode; class CommonCode { public: uint64_t unwrap() const; - static bool check(uint64_t common_code); - static CommonCode unsafe_create(uint64_t code); - RawCode to_raw_code() const; ShortCode to_short_code() const; std::string to_string(bool shorten = false) const; + static bool check(uint64_t common_code); + // TODO: std::cout << CommonCode(...) + explicit CommonCode(uint64_t common_code); explicit CommonCode(const RawCode &raw_code); explicit CommonCode(const ShortCode &short_code); explicit CommonCode(const std::string &common_code); - // TODO: std::cout << CommonCode(...) - - // TODO: CommonCode::create(...) / CommonCode::from_string(...) - // TODO: CommonCode::from_short_code(...) / CommonCode::from_raw_code(...) + static CommonCode create(uint64_t common_code); + static CommonCode unsafe_create(uint64_t common_code); + static CommonCode from_raw_code(const RawCode &raw_code); + static CommonCode from_string(const std::string &common_code); + static CommonCode from_short_code(const ShortCode &short_code); private: uint64_t code; diff --git a/src/common_code/serialize.cc b/src/common_code/serialize.cc index e6a146c..9ce269e 100644 --- a/src/common_code/serialize.cc +++ b/src/common_code/serialize.cc @@ -15,6 +15,10 @@ inline uint32_t last_zero_num(uint32_t bin) { // get last zero number return binary_count(bin >> 1); } +CommonCode CommonCode::from_string(const std::string &common_code) { + return CommonCode(common_code); // load from string +} + std::string CommonCode::to_string(bool shorten) const { // convert uint64_t code to string char result[10]; // max length 9-bits sprintf(result, "%09lX", code); diff --git a/src/main.cc b/src/main.cc index b197170..21f44bc 100644 --- a/src/main.cc +++ b/src/main.cc @@ -356,8 +356,10 @@ int main() { // std::cout << CommonCode::check_demo(0x0000011CE) << std::endl; // std::cout << CommonCode::check_demo(0x02ED67000) << std::endl; - std::cout << CommonCode("1a9bf0C0").to_string() << std::endl; - std::cout << CommonCode(0x4FEA13400).to_string() << std::endl; +// std::cout << CommonCode("1a9bf0C0").to_string() << std::endl; +// std::cout << CommonCode(0x4FEA13400).to_string() << std::endl; + + printf("%09lX\n", CommonCode::from_string("1a9bf0c").unwrap()); // std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;