Files
sword_rs/src/cxx/versekey.cc
T
Greg Hellings 0ec0a0c7ac Builds, runs, and tests succeed*
*Tests only succeed if you run them in a single-threaded instance. With
cargo this means invoking them as `cargo run -- --test-threads=1`. There
is an instance of global state in the Sword engine with the
VersificationMgr class that can easily cause a race condition. Since
VersificationMgr is intended to be a global singleton, when it is first
being instantiated the pointer is null. Sometimes the tests get into
that method at the same time and overwrite one another with the instance
of the manager, thus causing some of the VerseKeys that get instantiated
to be invalid.

TODO: On the Rust side of the code, create an instance of the
VersificationMgr before allowing any VerseKey objects to be created.
Probably also necessary to run it before allowing SWMgr to be
instantiated, as well, just to be safe.
2025-06-26 02:19:26 -05:00

226 lines
6.0 KiB
C++

#include "versekey.h"
#include <sword/versekey.h>
namespace sword_rs {
// VerseKey methods - all include null pointer checks for safety
uint8_t VerseKey::get_error() const {
return ptr ? ptr->getError() : 0;
}
rust::String VerseKey::get_locale() const {
return ptr ? rust::String(ptr->getLocale()) : rust::String("");
}
rust::String VerseKey::get_range_text() const {
return ptr ? rust::String(ptr->getRangeText()) : rust::String("");
}
rust::String VerseKey::get_short_text() const {
return ptr ? rust::String(ptr->getShortText()) : rust::String("");
}
rust::String VerseKey::get_text() const {
return ptr ? rust::String(ptr->getText()) : rust::String("");
}
void VerseKey::normalize(bool autocheck) {
if (ptr) {
ptr->normalize(autocheck);
}
}
uint8_t VerseKey::pop_error() {
return ptr ? ptr->popError() : 0;
}
void VerseKey::set_locale(const rust::String &locale) {
if (ptr) {
ptr->setLocale(std::string(locale).c_str());
}
}
void VerseKey::set_text(const rust::String &text) {
if (ptr) {
ptr->setText(std::string(text).c_str());
}
}
rust::String VerseKey::get_book_name() const {
return ptr ? rust::String(ptr->getBookName()) : rust::String("");
}
rust::String VerseKey::get_book_abbrev() const {
return ptr ? rust::String(ptr->getBookAbbrev()) : rust::String("");
}
rust::String VerseKey::get_osis_book_name() const {
return ptr ? rust::String(ptr->getOSISBookName()) : rust::String("");
}
rust::String VerseKey::get_osis_ref() const {
return ptr ? rust::String(ptr->getOSISRef()) : rust::String("");
}
rust::String VerseKey::get_osis_ref_range_text() const {
return ptr ? rust::String(ptr->getOSISRefRangeText()) : rust::String("");
}
rust::String VerseKey::get_short_range_text() const {
return ptr ? rust::String(ptr->getShortRangeText()) : rust::String("");
}
uint8_t VerseKey::get_testament() const {
return ptr ? ptr->getTestament() : 0;
}
uint8_t VerseKey::get_testament_max() const {
return ptr ? ptr->getTestamentMax() : 0;
}
uint8_t VerseKey::get_book() const {
return ptr ? ptr->getBook() : 0;
}
uint8_t VerseKey::get_book_max() const {
return ptr ? ptr->getBookMax() : 0;
}
uint8_t VerseKey::get_chapter() const {
return ptr ? ptr->getChapter() : 0;
}
uint8_t VerseKey::get_chapter_max() const {
return ptr ? ptr->getChapterMax() : 0;
}
uint8_t VerseKey::get_verse() const {
return ptr ? ptr->getVerse() : 0;
}
uint8_t VerseKey::get_verse_max() const {
return ptr ? ptr->getVerseMax() : 0;
}
bool VerseKey::is_autonormalize() const {
return ptr ? ptr->isAutoNormalize() : false;
}
bool VerseKey::is_intros() const {
return ptr ? ptr->isIntros() : false;
}
void VerseKey::set_autonormalize(bool autoNormalize) {
if (ptr) {
ptr->setAutoNormalize(autoNormalize);
}
}
void VerseKey::set_book_name(const rust::String &bookName) {
if (ptr) {
ptr->setBookName(std::string(bookName).c_str());
}
}
void VerseKey::set_intros(bool intros) {
if (ptr) {
ptr->setIntros(intros);
}
}
void VerseKey::set_testament(uint8_t testament) {
if (ptr) {
ptr->setTestament(testament);
}
}
void VerseKey::set_book(uint8_t book) {
if (ptr) {
ptr->setBook(book);
}
}
void VerseKey::set_chapter(uint8_t chapter) {
if (ptr) {
ptr->setChapter(chapter);
}
}
void VerseKey::set_verse(uint8_t verse) {
if (ptr) {
ptr->setVerse(verse);
}
}
bool VerseKey::is_valid() const {
return ptr != nullptr;
}
// Factory function for self-managing VerseKey
VerseKey* new_verse_key() {
try {
auto sword_key = new sword::VerseKey();
auto wrapper = new VerseKey();
wrapper->ptr = sword_key;
wrapper->managed = false; // self-managing
return wrapper;
} catch (...) {
return nullptr;
}
}
// Factory function for self-managing VerseKey from text
VerseKey* new_verse_key_from_text(const rust::String text) {
try {
auto sword_key = new sword::VerseKey(std::string(text).c_str());
auto wrapper = new VerseKey();
wrapper->ptr = sword_key;
wrapper->managed = false; // self-managing
return wrapper;
} catch (...) {
return nullptr;
}
}
// Creates a managed VerseKey wrapper from module's sword::VerseKey
VerseKey* get_verse_key_wrapper(SwordVerseKey* sword_key) {
if (!sword_key) {
return nullptr;
}
try {
auto wrapper = new VerseKey();
wrapper->ptr = sword_key;
wrapper->managed = true; // managed by external code
return wrapper;
} catch (...) {
return nullptr;
}
}
// Clone function - always returns self-managing copy using copy constructor
VerseKey* clone_verse_key(const VerseKey& source) {
if (!source.ptr) {
return nullptr;
}
try {
// Create a new sword::VerseKey using copy constructor
auto sword_key = new sword::VerseKey(*source.ptr);
auto wrapper = new VerseKey();
wrapper->ptr = sword_key;
wrapper->managed = false; // clones are always self-managing
return wrapper;
} catch (...) {
return nullptr;
}
}
// Delete function for VerseKey wrappers
void delete_verse_key(VerseKey* verse_key) {
if (verse_key) {
verse_key->cleanup();
delete verse_key;
}
}
}