Skip to content

Commit

Permalink
Use a map for object properties
Browse files Browse the repository at this point in the history
Simplify code robustify
  • Loading branch information
bastien-roucaries committed Jan 5, 2015
1 parent 08c2bd6 commit d695f3b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 175 deletions.
142 changes: 65 additions & 77 deletions qucs-core/src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <utility>

#include "logging.h"
#include "complex.h"
Expand All @@ -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.
Expand All @@ -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);
}
Expand All @@ -108,128 +109,115 @@ 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
which has been previously added. If there is no such scaled
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();
}
Expand Down
8 changes: 4 additions & 4 deletions qucs-core/src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define __OBJECT_H__

#include <string>
#include "property.h"

#define MCREATOR(val) \
val (); \
Expand All @@ -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; }
Expand Down Expand Up @@ -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
Expand Down
74 changes: 0 additions & 74 deletions qucs-core/src/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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
Expand Down
Loading

0 comments on commit d695f3b

Please sign in to comment.