Browse Source

feat: codec ffi support unsafe convert

legacy
Dnomd343 2 years ago
parent
commit
ebb57f185a
  1. 28
      src/klotski_core/ffi/codec.cc
  2. 9
      src/klotski_core/klotski.h
  3. 4
      src/klotski_core/raw_code/convert.cc

28
src/klotski_core/ffi/codec.cc

@ -106,6 +106,34 @@ bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code)
.to_common_code().to_short_code().unwrap();
}
uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) {
return ShortCode::unsafe_create(short_code)
.to_common_code().to_raw_code().unwrap();
}
uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code).to_common_code().unwrap();
}
uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) {
return CommonCode::unsafe_create(common_code).to_raw_code().unwrap();
}
uint64_t short_code_to_common_code_unsafe(uint32_t short_code) {
return ShortCode::unsafe_create(short_code).to_common_code().unwrap();
}
uint32_t common_code_to_short_code_unsafe(uint64_t common_code) {
return CommonCode::unsafe_create(common_code).to_short_code().unwrap();
}
////////////////////////////////////////////////////////////////////////////////
const uint32_t SHORT_CODE_STR_SIZE = 6; const uint32_t SHORT_CODE_STR_SIZE = 6;
bool short_code_to_string(uint32_t short_code, char short_code_str[]) { bool short_code_to_string(uint32_t short_code, char short_code_str[]) {

9
src/klotski_core/klotski.h

@ -74,7 +74,6 @@ extern "C" {
extern bool short_code_check(uint32_t short_code); extern bool short_code_check(uint32_t short_code);
extern bool common_code_check(uint64_t common_code); extern bool common_code_check(uint64_t common_code);
// TODO: allow unsafe convert
extern bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code); extern bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code);
extern bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code); extern bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code);
extern bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code); extern bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code);
@ -82,6 +81,14 @@ extern "C" {
extern bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code); extern bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code);
extern bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code); extern bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code);
// TODO: allow unsafe convert
extern uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code);
extern uint64_t short_code_to_raw_code_unsafe(uint32_t short_code);
extern uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code);
extern uint64_t common_code_to_raw_code_unsafe(uint64_t common_code);
extern uint64_t short_code_to_common_code_unsafe(uint32_t short_code);
extern uint32_t common_code_to_short_code_unsafe(uint64_t common_code);
extern const uint32_t SHORT_CODE_STR_SIZE; extern const uint32_t SHORT_CODE_STR_SIZE;
extern bool short_code_to_string(uint32_t short_code, char short_code_str[]); extern bool short_code_to_string(uint32_t short_code, char short_code_str[]);
extern bool short_code_from_string(const char short_code_str[], uint32_t *short_code); extern bool short_code_from_string(const char short_code_str[], uint32_t *short_code);

4
src/klotski_core/raw_code/convert.cc

@ -8,10 +8,6 @@ using klotski::RawCodeException;
/// RawCode to CommonCode /// RawCode to CommonCode
CommonCode RawCode::to_common_code() const { CommonCode RawCode::to_common_code() const {
if (!RawCode::check(code)) {
throw RawCodeException("raw code invalid");
}
/// pass raw code checker -> common code must valid
return CommonCode::unsafe_create(RawCode::compact(code)); return CommonCode::unsafe_create(RawCode::compact(code));
} }

Loading…
Cancel
Save