Skip to content

Commit

Permalink
Use stdd:string instead of char* for name
Browse files Browse the repository at this point in the history
Less error prone. better object code
  • Loading branch information
bastien-roucaries committed Jan 5, 2015
1 parent 92bcc66 commit 08c2bd6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
47 changes: 23 additions & 24 deletions qucs-core/src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ object::~object () {


// The function adds a complete property to the object property list.
void object::addProperty (property * const p) {
void object::addProperty (const std::string &n, property * const p) {
// for now
(void) n;
p->setNext (prop);
prop = p;
}

/* This function adds a property consisting of a key and a string
value to the object. */
void object::addProperty (const char * const n, const char * const val, const bool def) {
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 (p);
this->addProperty (n, p);
}

/* This function sets the specified property consisting of a key and a
string value in the object. */
void object::setProperty (const char * const n, const char * const val) {
void object::setProperty (const std::string &n, const char * const val) {
property * p = prop->findProperty (n);
if (p != NULL)
p->set (val);
Expand All @@ -80,15 +82,15 @@ void object::setProperty (const char * const n, const char * const val) {

/* This function adds a property consisting of a key and a double
value to the object. */
void object::addProperty (const char * const n, const nr_double_t val, const bool def) {
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 (p);
addProperty (n, p);
}

/* This function sets the specified property consisting of a key and a
double value in the object. */
void object::setProperty (const char * const n, const nr_double_t val) {
void object::setProperty (const std::string &n, const nr_double_t val) {
property * p = prop->findProperty (n);
if (p != NULL)
p->set (val);
Expand All @@ -99,24 +101,22 @@ void object::setProperty (const char * const n, const nr_double_t val) {
/* Th function sets the specified property consisting of a key and a
double value in the object. The property is marked a scalability
property. */
void object::setScaledProperty (const char * const n, const nr_double_t val) {
char prop[64];
sprintf (prop, "Scaled:%s", n);
setProperty (prop, val);
void object::setScaledProperty (const std::string &n, const nr_double_t val) {
setProperty ("Scaled:"+n, val);
}

/* This function adds a property consisting of a key and a variable
value to the object. */
void object::addProperty (const char * n, variable * const val, const bool def) {
void object::addProperty (const std::string &n, variable * const val, const bool def) {
property * p = new property (n, val);
p->setDefault(def);
addProperty (p);
addProperty (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 char * const n) const {
qucs::vector * object::getPropertyVector (const std::string &n) const {
const property * p = prop->findProperty (n);
if (p != NULL)
return p->getVector ();
Expand All @@ -126,7 +126,7 @@ qucs::vector * object::getPropertyVector (const char * const n) const {
/* 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 char * const n) const {
const char * object::getPropertyString (const std::string &n) const {
const property * p = prop->findProperty (n);
if (p != NULL)
return p->getString ();
Expand All @@ -135,7 +135,7 @@ const char * object::getPropertyString (const char * const n) const {

/* Returns the requested property reference variable name. If there
is no such property the function returns NULL. */
const char * object::getPropertyReference (const char * const n) const {
const char * object::getPropertyReference (const std::string &n) const {
const property * p = prop->findProperty (n);
if (p != NULL)
return p->getReference ();
Expand All @@ -145,7 +145,7 @@ const char * object::getPropertyReference (const char * const n) const {
/* 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 char * const n) const {
nr_double_t object::getPropertyDouble (const std::string &n) const {
const property * p = prop->findProperty (n);
if (p != NULL)
return p->getDouble ();
Expand All @@ -155,9 +155,8 @@ nr_double_t object::getPropertyDouble (const char * const n) const {
/* 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 char * const n) const{
char txt[64];
sprintf (txt, "Scaled:%s", n);
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)
Expand All @@ -169,7 +168,7 @@ nr_double_t object::getScaledProperty (const char * const n) const{
/* 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 char * const n) const {
int object::getPropertyInteger (const std::string &n) const {
const property * p = prop->findProperty (n);
if (p != NULL)
return p->getInteger ();
Expand All @@ -178,14 +177,14 @@ int object::getPropertyInteger (const char * const n) const {

/* 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 char * const n) const {
bool object::hasProperty (const std::string &n) const {
return (prop && prop->findProperty (n)) ? true : false;
}

/* 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 char * const n) const {
bool object::isPropertyGiven (const std::string &n) const {
if (prop != NULL) {
const property * p = prop->findProperty (n);
if (p != NULL && !p->isDefault ())
Expand All @@ -199,7 +198,7 @@ bool object::isPropertyGiven (const char * const n) const {
void object::copyProperties (property * org) {
prop = NULL;
while (org != NULL) {
addProperty (new property (*org));
addProperty (org->getName(),new property (*org));
org = org->getNext ();
}
}
Expand Down
32 changes: 16 additions & 16 deletions qucs-core/src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ class object
void setName (const std::string &n) { this->name = n; };
//! Get the name of the object.
const char * getName (void) const { return this->name.c_str(); };
void addProperty (property * const);
void addProperty (const char * const n, const char * const val, const bool def = false);
void addProperty (const char * const, const nr_double_t, const bool def = false);
void addProperty (const char * const, variable * const, const bool def = false);
void setProperty (const char * const, const char * const);
void setProperty (const char * const, nr_double_t);
void setScaledProperty (const char * const, const nr_double_t);
void setProperty (const char * const, variable * const);
vector * getPropertyVector (const char * const) const;
const char * getPropertyString (const char * const) const;
const char * getPropertyReference (const char * const) const;
nr_double_t getPropertyDouble (const char * const) const;
nr_double_t getScaledProperty (const char * const) const;
int getPropertyInteger (const char * const) const;
bool hasProperty (const char * const) const ;
bool isPropertyGiven (const char * const) const;
void addProperty (const std::string &n, property * const p);
void addProperty (const std::string &n, const char * const val, const bool def = false);
void addProperty (const std::string &n, const nr_double_t, const bool def = false);
void addProperty (const std::string &n, variable * const, const bool def = false);
void setProperty (const std::string &n, const char * const);
void setProperty (const std::string &n, nr_double_t);
void setScaledProperty (const std::string &n, const nr_double_t);
void setProperty (const std::string &n, variable * const);
vector * getPropertyVector (const std::string &n) const;
const char * getPropertyString (const std::string &n) const;
const char * getPropertyReference (const std::string &n) const;
nr_double_t getPropertyDouble (const std::string &n) const;
nr_double_t getScaledProperty (const std::string &n) const;
int getPropertyInteger (const std::string &n) const;
bool hasProperty (const std::string &n) const ;
bool isPropertyGiven (const std::string &n) const;
void deleteProperties (void);
int countProperties (void) const;
const char *
Expand Down
2 changes: 1 addition & 1 deletion qucs-core/src/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ property::~property () {
/* 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 char * const n) {
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)
Expand Down
2 changes: 1 addition & 1 deletion qucs-core/src/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class property
void set (int);
void set (const std::string &);
void set (variable *);
property * findProperty (const char * const);
property * findProperty (const std::string &n);
std::string toString (void) const;
bool isDefault (void) const { return def; }
void setDefault (bool d) { def = d; }
Expand Down

0 comments on commit 08c2bd6

Please sign in to comment.