Store the mjsCompiler -> appended mjSpec map when appending an mjSpec.

Previously, we stored the source `mjSpec` during a copy as a hack for having access to the compiler options, but this is not robust since we cannot guarantee that 1) the source `mjSpec` is not destroyed before we need to look up the compiler options nor 2) that the `mjSpec` was appended without a copy.

While 2) could be solved by simply handling an additional case in `mjCModel::FindSpec`, using a map also solves 1) and it is easier to understand.

PiperOrigin-RevId: 741504264
Change-Id: Iab1bfd9e61299a94fa8caf3a244c067b09d54384
This commit is contained in:
Alessio Quaglino
2025-03-28 06:26:30 -07:00
committed by Copybara-Service
parent 6e19035aab
commit 01d4c46753
4 changed files with 69 additions and 25 deletions
+5 -8
View File
@@ -215,7 +215,9 @@ class mjCModel : public mjCModel_, private mjSpec {
mjCTuple* AddTuple();
mjCKey* AddKey();
mjCPlugin* AddPlugin();
void AppendSpec(mjSpec* spec);
// append spec to this model, optionally map compiler options to the appended spec
void AppendSpec(mjSpec* spec, const mjsCompiler* compiler = nullptr);
// delete elements marked as discard=true
template <class T> void Delete(std::vector<T*>& elements,
@@ -248,7 +250,7 @@ class mjCModel : public mjCModel_, private mjSpec {
mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name
mjCBase* FindTree(mjCBody* body, mjtObj type, std::string name); // find tree object given name
mjSpec* FindSpec(std::string name) const; // find spec given name
mjSpec* FindSpec(const mjsCompiler* compiler_) const; // find spec given mjsCompiler
mjSpec* FindSpec(const mjsCompiler* compiler_); // find spec given mjsCompiler
void ActivatePlugin(const mjpPlugin* plugin, int slot); // activate plugin
// accessors
@@ -316,9 +318,6 @@ class mjCModel : public mjCModel_, private mjSpec {
// map from default class name to default class pointer
std::unordered_map<std::string, mjCDef*> def_map;
// get the spec from which this model was created
mjSpec* GetSourceSpec() const;
// set deepcopy flag
void SetDeepCopy(bool deepcopy) { deepcopy_ = deepcopy; }
@@ -332,9 +331,6 @@ class mjCModel : public mjCModel_, private mjSpec {
// settings for each defaults class
std::vector<mjCDef*> defaults_;
// spec from which this model was created in copy constructor
mjSpec* source_spec_;
// list of active plugins
std::vector<std::pair<const mjpPlugin*, int>> active_plugins_;
@@ -453,5 +449,6 @@ class mjCModel : public mjCModel_, private mjSpec {
bool deepcopy_; // copy objects when attaching
bool attached_ = false; // true if model is attached to a parent model
int uid_count_ = 0; // unique id count for all objects
std::unordered_map<const mjsCompiler*, mjSpec*> compiler2spec_; // map from compiler to spec
};
#endif // MUJOCO_SRC_USER_USER_MODEL_H_