diff --git a/Makefile b/Makefile index 7e1ca31..024a693 100644 --- a/Makefile +++ b/Makefile @@ -22,4 +22,4 @@ $(EMULATOR_LIB): src/hls4ml/emulator.cc $(CXX) $(CXXFLAGS) $(INCLUDES) -shared $^ -o $@ clean: - rm $(EMULATOR_LIB) + rm -f $(EMULATOR_LIB) diff --git a/include/hls4ml/emulator.h b/include/hls4ml/emulator.h index 6d7e9cb..dd854c3 100644 --- a/include/hls4ml/emulator.h +++ b/include/hls4ml/emulator.h @@ -26,7 +26,7 @@ namespace hls4mlEmulator void* model_lib_; public: - ModelLoader(std::string model_name); + ModelLoader(std::string const& model_name = ""); ModelLoader(ModelLoader const&) = delete; ModelLoader& operator=(ModelLoader const&) = delete; //prevent move constructor/assignment @@ -35,9 +35,12 @@ namespace hls4mlEmulator ~ModelLoader(); + std::string const& model_name() const { return model_name_; } + + void reset(std::string const& model_name = ""); + std::shared_ptr load_model(); }; } #endif - diff --git a/src/hls4ml/emulator.cc b/src/hls4ml/emulator.cc index 97e1c9f..861cbef 100644 --- a/src/hls4ml/emulator.cc +++ b/src/hls4ml/emulator.cc @@ -2,23 +2,30 @@ using namespace hls4mlEmulator; -ModelLoader::ModelLoader(std::string model_name) -{ - model_lib_ = nullptr; // Set to null for defined destructor behavior in case of failed load - model_name_ = std::move(model_name); - model_name_.append(".so"); +ModelLoader::ModelLoader(std::string const& model_name) + : model_lib_{nullptr}, + model_name_{model_name} {} + +ModelLoader::~ModelLoader() { + reset(); } -ModelLoader::~ModelLoader(){ - if (model_lib_ != nullptr) - dlclose(model_lib_); +void ModelLoader::reset(std::string const& model_name) { + model_name_ = model_name; + if (model_lib_ != nullptr) { + dlclose(model_lib_); + model_lib_ = nullptr; + } } +std::shared_ptr ModelLoader::load_model() { + if (model_name_.empty()) { + throw std::runtime_error("uninitialised model name: failed to load hls4ml emulator model !"); + } -std::shared_ptr ModelLoader::load_model() -{ //Open the model .so - model_lib_ = dlopen(model_name_.c_str(), RTLD_LAZY | RTLD_LOCAL); + std::string const model_lib_name = model_name_ + ".so"; + model_lib_ = dlopen(model_lib_name.c_str(), RTLD_LAZY | RTLD_LOCAL); if (!model_lib_) { std::cerr << "Cannot load library: " << dlerror() << std::endl; throw std::runtime_error("hls4ml emulator model library dlopen failure!");