Browse Source

feat: operators reload of ShortCode and CommonCode

legacy
Dnomd343 2 years ago
parent
commit
0c8944683b
  1. 8
      src/common_code/common_code.cc
  2. 4
      src/common_code/common_code.h
  3. 8
      src/short_code/short_code.cc
  4. 4
      src/short_code/short_code.h

8
src/common_code/common_code.cc

@ -6,6 +6,10 @@ uint64_t CommonCode::unwrap() const {
return code; // raw uint64_t code return code; // raw uint64_t code
} }
bool CommonCode::valid() const {
return CommonCode::check(code);
}
CommonCode CommonCode::create(uint64_t common_code) { CommonCode CommonCode::create(uint64_t common_code) {
return CommonCode(common_code); // create from uint64_t return CommonCode(common_code); // create from uint64_t
} }
@ -23,6 +27,10 @@ CommonCode::CommonCode(uint64_t common_code) {
code = common_code; code = common_code;
} }
bool CommonCode::operator==(const CommonCode &common_code) const {
return this->code == common_code.code;
}
std::ostream& operator<<(std::ostream &out, const CommonCode &self) { std::ostream& operator<<(std::ostream &out, const CommonCode &self) {
char str[10]; char str[10];
sprintf(str, "%09lX", self.code); sprintf(str, "%09lX", self.code);

4
src/common_code/common_code.h

@ -11,8 +11,12 @@ class ShortCode;
class CommonCode { class CommonCode {
public: public:
bool valid() const;
static bool check(uint64_t common_code); static bool check(uint64_t common_code);
/// Operators of CommonCode
explicit operator uint64_t() const { return code; } explicit operator uint64_t() const { return code; }
bool operator==(const CommonCode &common_code) const;
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self); friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
/// Export functions /// Export functions

8
src/short_code/short_code.cc

@ -5,6 +5,10 @@ uint32_t ShortCode::unwrap() const {
return code; // raw uint32_t code return code; // raw uint32_t code
} }
bool ShortCode::valid() const {
return ShortCode::check(code);
}
ShortCode ShortCode::create(uint32_t short_code) { ShortCode ShortCode::create(uint32_t short_code) {
return ShortCode(short_code); return ShortCode(short_code);
} }
@ -20,6 +24,10 @@ bool ShortCode::check(uint32_t short_code) {
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
} }
bool ShortCode::operator==(const ShortCode &short_code) const {
return this->code == short_code.code;
}
std::ostream& operator<<(std::ostream &out, const ShortCode &self) { std::ostream& operator<<(std::ostream &out, const ShortCode &self) {
out << self.to_string() << "(" << self.code << ")"; // short code info out << self.to_string() << "(" << self.code << ")"; // short code info
return out; return out;

4
src/short_code/short_code.h

@ -13,9 +13,13 @@ class ShortCode {
public: public:
enum Mode {NORMAL, FAST}; enum Mode {NORMAL, FAST};
bool valid() const;
static void speed_up(enum Mode mode); static void speed_up(enum Mode mode);
static bool check(uint32_t short_code); static bool check(uint32_t short_code);
/// Operators of ShortCode
explicit operator uint32_t() const { return code; } explicit operator uint32_t() const { return code; }
bool operator==(const ShortCode &short_code) const;
friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); friend std::ostream& operator<<(std::ostream &out, const ShortCode &self);
/// Export functions /// Export functions

Loading…
Cancel
Save