Browse Source

feat: `to_short_code` trait in CommonCode

legacy
Dnomd343 2 years ago
parent
commit
b615609b26
  1. 1
      src/common/CMakeLists.txt
  2. 4
      src/common_code/common_code.cc
  3. 4
      src/common_code/common_code.h
  4. 6
      src/main.cc
  5. 1
      src/short_code/short_code.cc
  6. 2
      src/short_code/short_code.h

1
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)

4
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");

4
src/common_code/common_code.h

@ -2,10 +2,14 @@
#include <string>
#include <cstdint>
#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;

6
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;
}

1
src/short_code/short_code.cc

@ -1,3 +1,4 @@
#include <stdexcept>
#include "short_code.h"
#include "short_code_chars.h"

2
src/short_code/short_code.h

@ -6,6 +6,8 @@
#include <unordered_map>
#include "common_code.h"
class CommonCode;
class ShortCode {
public:
enum Mode {

Loading…
Cancel
Save