Refactor GlobalModel class in the XML API.

PiperOrigin-RevId: 601761425
Change-Id: I9c24f670c1e9766867ec75aab56cec1c0b4f5703
This commit is contained in:
Kyle Bayes
2024-01-26 07:32:51 -08:00
committed by Copybara-Service
parent 669ee0ee48
commit 3b0a680fa7
+56 -65
View File
@@ -14,14 +14,17 @@
#include "xml/xml_api.h" #include "xml/xml_api.h"
#include <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <memory>
#include <mutex> #include <mutex>
#include <random> #include <optional>
#include <sstream>
#include <string>
#include <type_traits>
#include "engine/engine_resource.h" #include <mujoco/mjmodel.h>
#include "engine/engine_vfs.h"
#include "user/user_model.h" #include "user/user_model.h"
#include "xml/xml.h" #include "xml/xml.h"
#include "xml/xml_native_reader.h" #include "xml/xml_native_reader.h"
@@ -32,41 +35,51 @@
// global user model class // global user model class
class GlobalModel { class GlobalModel {
public: public:
GlobalModel(); // deletes current model and takes ownership of model
~GlobalModel(); void Set(mjCModel* model = nullptr);
void Clear(void);
mjCModel* model; // writes XML to string
std::optional<std::string> ToXML(const mjModel* m, char* error,
int error_sz);
private:
// using raw pointers as GlobalModel needs to be trivially destructible
std::mutex* mutex_ = new std::mutex();
mjCModel* model_ = nullptr;
}; };
std::optional<std::string> GlobalModel::ToXML(const mjModel* m, char* error,
GlobalModel::GlobalModel() { int error_sz) {
// clear pointers std::lock_guard<std::mutex> lock(*mutex_);
model = 0; if (!model_) {
} mjCopyError(error, "No XML model loaded", error_sz);
return std::nullopt;
GlobalModel::~GlobalModel() {
Clear();
}
void GlobalModel::Clear() {
// de-allocate models
if (model) {
delete model;
} }
model_->CopyBack(m);
std::string result = mjWriteXML(model_, error, error_sz);
if (result.empty()) {
return std::nullopt;
}
return result;
}
// clear pointers void GlobalModel::Set(mjCModel* model) {
model = 0; std::lock_guard<std::mutex> lock(*mutex_);
if (model_ != nullptr) {
delete model_;
}
model_ = model;
} }
// single instance of global model, protected with mutex // returns a single instance of the global model
static GlobalModel themodel; GlobalModel& GetGlobalModel() {
static std::mutex themutex; static GlobalModel global_model;
// global variables must be trivially destructible
static_assert(std::is_trivially_destructible_v<decltype(global_model)>);
return global_model;
}
//---------------------------------- Functions ----------------------------------------------------- //---------------------------------- Functions -----------------------------------------------------
@@ -76,34 +89,28 @@ static std::mutex themutex;
mjModel* mj_loadXML(const char* filename, const mjVFS* vfs, mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
char* error, int error_sz) { char* error, int error_sz) {
// serialize access to themodel
std::lock_guard<std::mutex> lock(themutex);
// parse new model // parse new model
mjCModel* newmodel = mjParseXML(filename, vfs, error, error_sz); std::unique_ptr<mjCModel> model(mjParseXML(filename, vfs, error, error_sz));
if (!newmodel) { if (!model) {
return nullptr; return nullptr;
} }
// compile new model // compile new model
mjModel* m = newmodel->Compile(vfs); mjModel* m = model->Compile(vfs);
if (!m) { if (!m) {
mjCopyError(error, newmodel->GetError().message, error_sz); mjCopyError(error, model->GetError().message, error_sz);
delete newmodel;
return nullptr; return nullptr;
} }
// clear old and assign new
themodel.Clear();
themodel.model = newmodel;
// handle compile warning // handle compile warning
if (themodel.model->GetError().warning) { if (model->GetError().warning) {
mjCopyError(error, themodel.model->GetError().message, error_sz); mjCopyError(error, model->GetError().message, error_sz);
} else if (error) { } else if (error) {
error[0] = '\0'; error[0] = '\0';
} }
// clear old and assign new
GetGlobalModel().Set(model.release());
return m; return m;
} }
@@ -113,15 +120,7 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
// returns 1 if successful, 0 otherwise // returns 1 if successful, 0 otherwise
// error can be NULL; otherwise assumed to have size error_sz // error can be NULL; otherwise assumed to have size error_sz
int mj_saveLastXML(const char* filename, const mjModel* m, char* error, int error_sz) { int mj_saveLastXML(const char* filename, const mjModel* m, char* error, int error_sz) {
// serialize access to themodel
std::lock_guard<std::mutex> lock(themutex);
FILE *fp = stdout; FILE *fp = stdout;
if (!themodel.model) {
mjCopyError(error, "No XML model loaded", error_sz);
return 0;
}
if (filename != nullptr && filename[0] != '\0') { if (filename != nullptr && filename[0] != '\0') {
fp = fopen(filename, "w"); fp = fopen(filename, "w");
if (!fp) { if (!fp) {
@@ -130,28 +129,23 @@ int mj_saveLastXML(const char* filename, const mjModel* m, char* error, int erro
} }
} }
themodel.model->CopyBack(m); auto result = GetGlobalModel().ToXML(m, error, error_sz);
std::string result = mjWriteXML(themodel.model, error, error_sz); if (result.has_value()) {
fprintf(fp, "%s", result->c_str());
if (!result.empty()) {
fprintf(fp, "%s", result.c_str());
} }
if (fp != stdout) { if (fp != stdout) {
fclose(fp); fclose(fp);
} }
return !result.empty(); return result.has_value();
} }
// free last XML // free last XML
void mj_freeLastXML(void) { void mj_freeLastXML(void) {
// serialize access to themodel GetGlobalModel().Set();
std::lock_guard<std::mutex> lock(themutex);
themodel.Clear();
} }
@@ -159,9 +153,6 @@ void mj_freeLastXML(void) {
// print internal XML schema as plain text or HTML, with style-padding or &nbsp; // print internal XML schema as plain text or HTML, with style-padding or &nbsp;
int mj_printSchema(const char* filename, char* buffer, int buffer_sz, int flg_html, int flg_pad) { int mj_printSchema(const char* filename, char* buffer, int buffer_sz, int flg_html, int flg_pad) {
// serialize access, even though it is not necessary
std::lock_guard<std::mutex> lock(themutex);
// print to stringstream // print to stringstream
mjXReader reader; mjXReader reader;
std::stringstream str; std::stringstream str;