diff --git a/qucs-core/src/object.cpp b/qucs-core/src/object.cpp index 0e7b2d0beb..18332115c2 100644 --- a/qucs-core/src/object.cpp +++ b/qucs-core/src/object.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "logging.h" #include "complex.h" @@ -45,7 +46,7 @@ object::object (const object & o) { name = o.name; next = o.next; prev = o.prev; - copyProperties (o.prop); + props = o.props; } // Destructor deletes an instance of the object class. @@ -57,43 +58,43 @@ object::~object () { // The function adds a complete property to the object property list. void object::addProperty (const std::string &n, property * const p) { // for now - (void) n; - p->setNext (prop); - prop = p; + props.insert({{n,*p}}); } /* This function adds a property consisting of a key and a string value to the object. */ void object::addProperty (const std::string &n, const char * const val, const bool def) { - property * p = new property (n, val); - p->setDefault(def); - this->addProperty (n, p); + property p; + p.set(val); + p.setDefault(def); + props.insert({{n,p}}); } /* This function sets the specified property consisting of a key and a string value in the object. */ void object::setProperty (const std::string &n, const char * const val) { - property * p = prop->findProperty (n); - if (p != NULL) - p->set (val); - else - addProperty (n, val); + auto it = props.find(n); + if(it != props.end()) + props[n].set(val); + else + addProperty (n, val); } /* This function adds a property consisting of a key and a double value to the object. */ void object::addProperty (const std::string &n, const nr_double_t val, const bool def) { - property * p = new property (n, val); - p->setDefault(def); - addProperty (n, p); + property p; + p.set(val); + p.setDefault(def); + props.insert({{n,p}}); } /* This function sets the specified property consisting of a key and a double value in the object. */ void object::setProperty (const std::string &n, const nr_double_t val) { - property * p = prop->findProperty (n); - if (p != NULL) - p->set (val); + auto it = props.find(n); + if(it != props.end()) + props[n].set(val); else addProperty (n, val); } @@ -108,48 +109,53 @@ void object::setScaledProperty (const std::string &n, const nr_double_t val) { /* This function adds a property consisting of a key and a variable value to the object. */ void object::addProperty (const std::string &n, variable * const val, const bool def) { - property * p = new property (n, val); - p->setDefault(def); - addProperty (n,p); + property p; + p.set(val); + p.setDefault(def); + props.insert({{n,p}}); } /* Returns the requested property value which has been previously added as its vector representation. If there is no such property the function returns NULL. */ qucs::vector * object::getPropertyVector (const std::string &n) const { - const property * p = prop->findProperty (n); - if (p != NULL) - return p->getVector (); - return NULL; + const auto &it = props.find(n); + if(it != props.end()) + return props.at(n).getVector(); + else + return NULL; } /* Returns the requested property value which has been previously added as its text representation. If there is no such property the function returns NULL. */ const char * object::getPropertyString (const std::string &n) const { - const property * p = prop->findProperty (n); - if (p != NULL) - return p->getString (); - return NULL; + const auto &it = props.find(n); + if(it != props.end()) + return props.at(n).getString(); + else + return NULL; } /* Returns the requested property reference variable name. If there is no such property the function returns NULL. */ const char * object::getPropertyReference (const std::string &n) const { - const property * p = prop->findProperty (n); - if (p != NULL) - return p->getReference (); - return NULL; + const auto &it = props.find(n); + if(it != props.end()) + return props.at(n).getReference(); + else + return NULL; } /* Returns the requested property value which has been previously added as its double representation. If there is no such property the function returns zero. */ nr_double_t object::getPropertyDouble (const std::string &n) const { - const property * p = prop->findProperty (n); - if (p != NULL) - return p->getDouble (); - return 0.0; + const auto &it = props.find(n); + if(it != props.end()) + return props.at(n).getDouble(); + else + return 0.0; } /* The functions returns the requested (scalability) property value @@ -157,79 +163,61 @@ nr_double_t object::getPropertyDouble (const std::string &n) const { property the function returns the standard property or zero. */ nr_double_t object::getScaledProperty (const std::string &n) const{ std::string txt = "Scaled:"+n; - // try to find scaled property - const property * p = prop->findProperty (txt); - if (p != NULL) - return p->getDouble (); - // default to standard property - return getPropertyDouble (n); + const auto &it = props.find(txt); + if(it != props.end()) + return props.at(n).getDouble(); + else + return this->getPropertyDouble(n); } /* Returns the requested property value which has been previously added as its integer representation. If there is no such property the function returns zero. */ int object::getPropertyInteger (const std::string &n) const { - const property * p = prop->findProperty (n); - if (p != NULL) - return p->getInteger (); - return 0; + const auto &it = props.find(n); + if(it != props.end()) + return props.at(n).getInteger(); + else + return 0; } /* The function checks whether the object has got a certain property value. If so it returns non-zero, otherwise it returns zero. */ bool object::hasProperty (const std::string &n) const { - return (prop && prop->findProperty (n)) ? true : false; + return props.find(n) != props.end(); } /* The function checks whether the object has got a certain property value and if this has its default value. If so it returns non-zero, otherwise it returns zero. */ bool object::isPropertyGiven (const std::string &n) const { - if (prop != NULL) { - const property * p = prop->findProperty (n); - if (p != NULL && !p->isDefault ()) - return true; - } - return false; + const auto &it = props.find(n); + if(it != props.end()) + return !props.at(n).isDefault(); + else + return false; } -/* This function copies all properties in the given property list into - an object. */ -void object::copyProperties (property * org) { - prop = NULL; - while (org != NULL) { - addProperty (org->getName(),new property (*org)); - org = org->getNext (); - } -} + // Deletes all properties of an object. void object::deleteProperties (void) { - property * n; - for (property * p = prop; p != NULL; p = n) { - n = p->getNext (); - delete p; - } - prop = NULL; + props.clear(); } // The function returns the number of properties in the object. int object::countProperties (void) const { - int res = 0; - for (property * p = prop; p != NULL; p = p->getNext ()) res++; - return res; + return props.size(); } // This function returns a text representation of the objects properties. const char * object::propertyList (void) const { std::string ptxt; - for (property * p = prop; p != NULL; p = p->getNext ()) { - std::string n = std::string(p->getName ()); - std::string val = std::string(p->toString ()); + for(auto it = props.cbegin(); it != props.cend(); ++it) { + std::string n = it->first; + std::string val = it->second.toString (); std::string text = n+"=\""+val+"\""; ptxt += text; - if (p->getNext () != NULL) - ptxt += " "; } return ptxt.c_str(); } diff --git a/qucs-core/src/object.h b/qucs-core/src/object.h index 4e08047eba..3be4fdf454 100644 --- a/qucs-core/src/object.h +++ b/qucs-core/src/object.h @@ -32,6 +32,7 @@ #define __OBJECT_H__ #include +#include "property.h" #define MCREATOR(val) \ val (); \ @@ -55,9 +56,9 @@ class object { public: //! Constructor creates an unnamed instance of the object class. - object () : name(), next(nullptr), prev(nullptr), prop(nullptr) {} ; + object () : name(), next(nullptr), prev(nullptr), props() {} ; //! This constructor creates a named instance of the object class. - object (const std::string &n) : name(n), next(nullptr), prev(nullptr), prop(nullptr) {} ; + object (const std::string &n) : name(n), next(nullptr), prev(nullptr), props() {} ; object (const object &); virtual ~object (); object * getNext (void) const { return this->next; } @@ -90,11 +91,10 @@ class object propertyList (void) const; private: - void copyProperties (property *); std::string name; object * next; object * prev; - property * prop; + properties props; }; } // namespace qucs diff --git a/qucs-core/src/property.cpp b/qucs-core/src/property.cpp index 265de809bc..a770c93a7f 100644 --- a/qucs-core/src/property.cpp +++ b/qucs-core/src/property.cpp @@ -43,78 +43,14 @@ using namespace eqn; // Constructor creates an unnamed instance of the property class. property::property () : - name(), str() { type = PROPERTY_UNKNOWN; value = 0.0; var = NULL; - next = NULL; def = false; } -// Constructor creates a named instance of the property class. -property::property (const std::string &n) : - name (n), - str () - { - type = PROPERTY_UNKNOWN; - value = 0.0; - var = NULL; - next = NULL; - def = false; -} - -/* This full qualified constructor creates an instance of the property - class containing both the key and the value of the property. */ -property::property (const std::string &n, const std::string &val) : - name (n), - str(val) -{ - type = PROPERTY_STR; - value = 0.0; - var = NULL; - next = NULL; - def = false; -} - -/* This full qualified constructor creates an instance of the property - class containing both the key and the value of the property. */ -property::property (const std::string &n, nr_double_t val) : - name (n), - str() -{ - type = PROPERTY_DOUBLE; - value = val; - var = NULL; - next = NULL; - def = false; -} - -/* This full qualified constructor creates an instance of the property - class containing both the key and the value of the property. */ -property::property (const std::string &n, variable * val) : - name (n), - str () -{ - type = PROPERTY_VAR; - var = val; - value = 0.0; - next = NULL; - def = false; -} - -/* The copy constructor creates a new instance of the property class - based on the given property object. */ -property::property (const property & p) { - type = p.type; - this->name = p.name; - this->str = p.str; - value = p.value; - next = p.next; - var = p.var; - def = p.def; -} // Destructor deletes the property object. property::~property () { @@ -129,16 +65,6 @@ property::~property () { #endif } -/* Goes through the chained list of the properties and looks for a - property matching the given key and returns its value if possible. - If there is no such property the function returns NULL. */ -property * property::findProperty (const std::string &n) { - const std::string tmp = std::string(n); - for (property * p = this; p != NULL; p = p->getNext ()) - if (p->getName() == n) - return p; - return NULL; -} // Short macro in order to obtain the correct constant value. #define D(con) ((constant *) (con))->d diff --git a/qucs-core/src/property.h b/qucs-core/src/property.h index 44dedd8ead..925d9e71cb 100644 --- a/qucs-core/src/property.h +++ b/qucs-core/src/property.h @@ -26,6 +26,8 @@ #define __PROPERTY_H__ #include +#include +#include namespace qucs { @@ -44,24 +46,7 @@ class property { public: property (); - property (const std::string &); - property (const std::string &, const std::string &); - property (const std::string &, nr_double_t); - property (const std::string &, variable *); - property (const property &); virtual ~property (); - property * getNext (void) const { return next; } - void setNext (property * p) { next = p; } - - //! Sets the name of the property. - void setName (const std::string &n) { - this->name = n; - }; - - //! Returns the name of the property. - std::string getName (void) const { - return this->name; - } qucs::vector * getVector (void) const; nr_double_t getDouble (void) const; @@ -72,7 +57,6 @@ class property void set (int); void set (const std::string &); void set (variable *); - property * findProperty (const std::string &n); std::string toString (void) const; bool isDefault (void) const { return def; } void setDefault (bool d) { def = d; } @@ -80,13 +64,13 @@ class property private: bool def; int type; - std::string name; std::string str; nr_double_t value; variable * var; - property * next; }; +typedef std::unordered_map properties; + } // namespace qucs #endif /* __PROPERTY_H__ */