Skip to content

Commit

Permalink
imlement proper option cloning #12998
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Dec 20, 2024
1 parent c731a1b commit a946eb5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/utils/options/Option.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef std::vector<int> IntVector;
*/
typedef std::vector<std::string> StringVector;

#define CLONEABLE(Type) virtual Type* clone() const { return new Type(*this); }

/* -------------------------------------------------------------------------
* Option
* ----------------------------------------------------------------------- */
Expand Down Expand Up @@ -331,6 +333,12 @@ class Option {
*/
virtual const std::string& getTypeName() const;

/** @brief Returns a copy of this option
*
* @return The option copy
*/
virtual Option* clone() const = 0;

protected:
/** @brief Marks the information as set
*
Expand Down Expand Up @@ -427,6 +435,8 @@ class Option_Integer : public Option {
*/
bool isInteger() const;

CLONEABLE(Option_Integer)

private:
/// @brief the value, valid only when the base-classes "myAmSet"-member is true
int myValue;
Expand Down Expand Up @@ -472,6 +482,8 @@ class Option_String : public Option {
*/
bool set(const std::string& v, const std::string& orig, const bool append);

CLONEABLE(Option_String)

protected:
/// @brief the value, valid only when the base-classes "myAmSet"-member is true
std::string myValue;
Expand Down Expand Up @@ -523,6 +535,8 @@ class Option_Float : public Option {
*/
bool isFloat() const;

CLONEABLE(Option_Float)

private:
/// @brief the value, valid only when the base-classes "myAmSet"-member is true
double myValue;
Expand Down Expand Up @@ -561,6 +575,8 @@ class Option_Bool : public Option {
*/
bool isBool() const;

CLONEABLE(Option_Bool)

protected:
/// @brief the value, valid only when the base-classes "myAmSet"-member is true
bool myValue;
Expand All @@ -584,6 +600,8 @@ class Option_BoolExtended : public Option_Bool {

/// @brief sets the given value (converts it to bool)
bool set(const std::string& v, const std::string& orig, const bool append);

CLONEABLE(Option_BoolExtended)
};

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -625,6 +643,8 @@ class Option_IntVector : public Option {
*/
bool set(const std::string& v, const std::string& orig, const bool append);

CLONEABLE(Option_IntVector)

private:
/// @brief the value, valid only when the base-classes "myAmSet"-member is true
IntVector myValue;
Expand Down Expand Up @@ -670,6 +690,8 @@ class Option_StringVector : public Option {
*/
bool set(const std::string& v, const std::string& orig, const bool append);

CLONEABLE(Option_StringVector)

private:
/// @brief the value, valid only when the base-classes "myAmSet"-member is true
StringVector myValue;
Expand Down Expand Up @@ -708,6 +730,8 @@ class Option_FileName : public Option_StringVector {
* not in line with code style of the Options sub-system.
*/
std::string getString() const;

CLONEABLE(Option_FileName)
};

// -------------------------------------------------------------------------
Expand All @@ -730,6 +754,8 @@ class Option_Network : public Option_String {
* @return true
*/
bool isNetwork() const;

CLONEABLE(Option_Network)
};

// -------------------------------------------------------------------------
Expand All @@ -752,6 +778,8 @@ class Option_Additional : public Option_String {
* @return true
*/
bool isAdditional() const;

CLONEABLE(Option_Additional)
};

// -------------------------------------------------------------------------
Expand All @@ -774,6 +802,8 @@ class Option_Route : public Option_String {
* @return true
*/
bool isRoute() const;

CLONEABLE(Option_Route)
};

// -------------------------------------------------------------------------
Expand All @@ -796,6 +826,8 @@ class Option_Data : public Option_String {
* @return true
*/
bool isData() const;

CLONEABLE(Option_Data)
};

// -------------------------------------------------------------------------
Expand All @@ -818,6 +850,8 @@ class Option_SumoConfig : public Option_String {
* @return true
*/
bool isSumoConfig() const;

CLONEABLE(Option_SumoConfig)
};

// -------------------------------------------------------------------------
Expand All @@ -840,6 +874,8 @@ class Option_Edge : public Option_String {
* @return true
*/
bool isEdge() const;

CLONEABLE(Option_Edge)
};

// -------------------------------------------------------------------------
Expand All @@ -862,4 +898,6 @@ class Option_EdgeVector : public Option_String {
* @return true
*/
bool isEdgeVector() const;

CLONEABLE(Option_EdgeVector)
};
4 changes: 3 additions & 1 deletion src/utils/options/OptionsCont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,9 @@ OptionsCont::clone() const {
// (with the possibility of changing a few settings and not affecting the original)
OptionsCont* oc = new OptionsCont(*this);
oc->resetWritable();
oc->myAddresses.clear();
for (auto& addr : oc->myAddresses) {
addr.second = addr.second->clone();
}
return oc;
}

Expand Down

0 comments on commit a946eb5

Please sign in to comment.