From b615609b26fc856fa17f8a1a07590da995fea6a4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 22:40:26 +0800 Subject: [PATCH] feat: `to_short_code` trait in CommonCode --- src/common/CMakeLists.txt | 1 + src/common_code/common_code.cc | 4 ++++ src/common_code/common_code.h | 4 ++++ src/main.cc | 6 ++++-- src/short_code/short_code.cc | 1 + src/short_code/short_code.h | 2 ++ 6 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0ab3fea..73c0316 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,3 +1,4 @@ cmake_minimum_required(VERSION 3.0) add_library(common common.cc) +target_link_libraries(common short_code) diff --git a/src/common_code/common_code.cc b/src/common_code/common_code.cc index e70def5..e8b4be9 100644 --- a/src/common_code/common_code.cc +++ b/src/common_code/common_code.cc @@ -12,6 +12,10 @@ uint64_t CommonCode::unwrap() const { return code; // get raw uint64_t code } +ShortCode CommonCode::to_short_code() const { // convert to short code + return ShortCode(*this); +} + CommonCode::CommonCode(uint64_t common_code) { if (!CommonCode::check(common_code)) { // check input common code throw std::invalid_argument("invalid common code"); diff --git a/src/common_code/common_code.h b/src/common_code/common_code.h index 5c2badb..c8529d3 100644 --- a/src/common_code/common_code.h +++ b/src/common_code/common_code.h @@ -2,10 +2,14 @@ #include #include +#include "short_code.h" + +class ShortCode; class CommonCode { public: uint64_t unwrap() const; + ShortCode to_short_code() const; static bool check(uint64_t common_code); std::string to_string(bool shorten = false) const; diff --git a/src/main.cc b/src/main.cc index 13654c7..ffd86b9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -114,8 +114,10 @@ int main() { // std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl; // std::cout << ShortCode(CommonCode(0x6EC0F8800)).to_string() << std::endl; - std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::NORMAL).to_string() << std::endl; - std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::FAST).to_string() << std::endl; +// std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::NORMAL).to_string() << std::endl; +// std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::FAST).to_string() << std::endl; + + std::cout << CommonCode(0x6EC0F8800).to_short_code().to_string() << std::endl; return 0; } diff --git a/src/short_code/short_code.cc b/src/short_code/short_code.cc index 65422b8..9443665 100644 --- a/src/short_code/short_code.cc +++ b/src/short_code/short_code.cc @@ -1,3 +1,4 @@ +#include #include "short_code.h" #include "short_code_chars.h" diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index f40c5ec..c5b79c5 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -6,6 +6,8 @@ #include #include "common_code.h" +class CommonCode; + class ShortCode { public: enum Mode {