feat: upgrade wfassoc_exec
This commit is contained in:
17
wfassoc_cdylib/Cargo.toml
Normal file
17
wfassoc_cdylib/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "wfassoc_cdylib"
|
||||
version = "0.1.0"
|
||||
authors = ["yyc12345"]
|
||||
edition = "2024"
|
||||
description = "The dynamic wfassoc library exposed for C/C++ and other languages users."
|
||||
license = "SPDX:MIT"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
thiserror = { workspace = true }
|
||||
wfassoc = { path="../wfassoc" }
|
||||
|
||||
[build-dependencies]
|
||||
cbindgen = "0.29.0"
|
||||
5
wfassoc_cdylib/README.md
Normal file
5
wfassoc_cdylib/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Wfassoc C/C++ Library
|
||||
|
||||
For how to utilize this library, please see our example located in `example/ppic`. It is a Qt project built with CMake and demonstrate basically all usage of exposed functions in this dynamic library.
|
||||
|
||||
All crucial spots are written in this example as the code comment so I don't write them in there again.
|
||||
58
wfassoc_cdylib/src/last_error.rs
Normal file
58
wfassoc_cdylib/src/last_error.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
//! When calling function with dynamic library,
|
||||
//! function return value indicates whether function has been successfully executed.
|
||||
//! When function return `false`, programmer may want to know which error occurs.
|
||||
//!
|
||||
//! This module provide **thread independent** error message string storage,
|
||||
//! which is more like Win32 `GetLastError()` but return error message instead of error code.
|
||||
//! These module provided functions will be called when executing main module functions.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::{CString, c_char};
|
||||
|
||||
struct LastError {
|
||||
msg: CString,
|
||||
}
|
||||
|
||||
impl LastError {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
msg: CString::new("").expect("empty string must be valid for CString"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_msg(&mut self, msg: &str) {
|
||||
self.msg = CString::new(msg).expect("unexpected blank in error message output");
|
||||
}
|
||||
|
||||
pub fn get_msg(&self) -> *const c_char {
|
||||
self.msg.as_ptr()
|
||||
}
|
||||
|
||||
pub fn clear_msg(&mut self) {
|
||||
self.msg = CString::new("").expect("empty string must be valid for CString");
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static LAST_ERROR: RefCell<LastError> = RefCell::new(LastError::new());
|
||||
}
|
||||
|
||||
/// Set thread local error message.
|
||||
pub fn set_last_error(msg: &str) {
|
||||
LAST_ERROR.with(|e| {
|
||||
e.borrow_mut().set_msg(msg);
|
||||
});
|
||||
}
|
||||
|
||||
/// Get const pointer to thread local error message string.
|
||||
/// If there is no error, return pointer will point to empty string.
|
||||
pub fn get_last_error() -> *const c_char {
|
||||
LAST_ERROR.with(|e| e.borrow().get_msg())
|
||||
}
|
||||
|
||||
/// Clear thread local error message (reset to empty string).
|
||||
pub fn clear_last_error() {
|
||||
LAST_ERROR.with(|e| {
|
||||
e.borrow_mut().clear_msg();
|
||||
});
|
||||
}
|
||||
57
wfassoc_cdylib/src/lib.rs
Normal file
57
wfassoc_cdylib/src/lib.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::{CString, c_char};
|
||||
use thiserror::Error as TeError;
|
||||
|
||||
mod last_error;
|
||||
mod string_cache;
|
||||
|
||||
// region: Error
|
||||
|
||||
/// Error occurs in this crate.
|
||||
#[derive(Debug, TeError)]
|
||||
enum Error {
|
||||
/// Error occurs in wfassoc core.
|
||||
#[error("{0}")]
|
||||
Core(#[from] wfassoc::Error),
|
||||
|
||||
#[error("error occurs when parsing into C/C++ string")]
|
||||
CastIntoCStr(#[from] std::ffi::NulError),
|
||||
#[error("error occurs when parsing from C/C++ string")]
|
||||
CastFromCStr(#[from] std::ffi::IntoStringError),
|
||||
}
|
||||
|
||||
/// Result type used in this crate.
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
// endregion
|
||||
|
||||
// region: Macros
|
||||
|
||||
|
||||
// endregion
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn WFStartup() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn WFShutdown() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn WFGetLastError() -> *const c_char {
|
||||
last_error::get_last_error()
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn WFHasPrivilege() -> bool {
|
||||
wfassoc::utilities::has_privilege()
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn WFAdd(left: u32, right: u32, rv: *mut u32) -> bool {
|
||||
unsafe { *rv = left + right; }
|
||||
return true;
|
||||
}
|
||||
52
wfassoc_cdylib/src/string_cache.rs
Normal file
52
wfassoc_cdylib/src/string_cache.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
//! When calling this dynamic library with outside programs,
|
||||
//! outer programs may usually need to fetch string resource produced by Rust code.
|
||||
//! However it is impossible pass Rust string directly to outer program.
|
||||
//!
|
||||
//! This module provide **thread independent** string cache for resolving this issue.
|
||||
//! When we need pass string to outer programs, we push that string into this string as C-like format,
|
||||
//! then return its pointer to outer program.
|
||||
//! So that outside program can utilize it like calling C/C++ library.
|
||||
//! The only thing that outer programs should note is that this string is volatile,
|
||||
//! once they get it, they must dupliate it immediately before any futher calling to this dynamic library.
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::{CString, c_char};
|
||||
|
||||
struct StringCache {
|
||||
msg: CString,
|
||||
}
|
||||
|
||||
impl StringCache {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
msg: CString::new("").expect("empty string must be valid for CString"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_msg(&mut self, msg: &str) {
|
||||
self.msg = CString::new(msg).expect("unexpected blank in string push into string cache");
|
||||
}
|
||||
|
||||
pub fn get_msg(&self) -> *const c_char {
|
||||
self.msg.as_ptr()
|
||||
}
|
||||
|
||||
pub fn clear_msg(&mut self) {
|
||||
self.msg = CString::new("").expect("empty string must be valid for CString");
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static STRING_CACHE: RefCell<StringCache> = RefCell::new(StringCache::new());
|
||||
}
|
||||
|
||||
/// Set thread local string cache.
|
||||
pub fn set_string_cache(msg: &str) {
|
||||
STRING_CACHE.with(|e| {
|
||||
e.borrow_mut().set_msg(msg);
|
||||
});
|
||||
}
|
||||
|
||||
/// Get const pointer to thread local string cache for outside program visiting.
|
||||
pub fn get_string_cache() -> *const c_char {
|
||||
STRING_CACHE.with(|e| e.borrow().get_msg())
|
||||
}
|
||||
Reference in New Issue
Block a user