Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
object.cpp
Go to the documentation of this file.
1 /*
2  * object.cpp - generic object class implementation
3  *
4  * Copyright (C) 2003, 2004, 2005, 2006, 2008 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: object.cpp 1870 2013-03-06 12:52:07Z crobarcro $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 
34 #include "logging.h"
35 #include "complex.h"
36 #include "property.h"
37 #include "object.h"
38 #include "variable.h"
39 
40 namespace qucs {
41 
42 // Constructor creates an unnamed instance of the object class.
44  name = NULL;
45  prev = next = NULL;
46  prop = NULL;
47  ptxt = NULL;
48 }
49 
50 // This constructor creates a named instance of the object class.
51 object::object (const char * n) {
52  // create a copy of the character array pointed to by n
53  // and get a pointer to the copy using strdup
54  name = strdup (n);
55  prev = next = NULL;
56  prop = NULL;
57  ptxt = NULL;
58 }
59 
60 /* This copy constructor creates a instance of the object class based
61  on the given object. */
62 object::object (const object & o) {
63  name = o.name ? strdup (o.name) : NULL;
64  next = o.next;
65  prev = o.prev;
66  ptxt = o.ptxt ? strdup (o.ptxt) : NULL;
67  copyProperties (o.prop);
68 }
69 
70 // Destructor deletes an instance of the object class.
72  if (name) free (name);
73  if (ptxt) free (ptxt);
75 }
76 
77 // Sets the name of the object.
78 void object::setName (const char * n) {
79  if (name) free (name);
80  name = n ? strdup (n) : NULL;
81 }
82 
83 // Returns the name of the object.
84 char * object::getName (void) {
85  return name;
86 }
87 
88 // The function adds a complete property to the object property list.
90  p->setNext (prop);
91  prop = p;
92 }
93 
94 /* This function adds a property consisting of a key and a string
95  value to the object. */
96 property * object::addProperty (const char * n, const char * val) {
97  property * p = new property (n, val);
98  addProperty (p);
99  return p;
100 }
101 
102 /* This function sets the specified property consisting of a key and a
103  string value in the object. */
104 void object::setProperty (const char * n, char * val) {
105  property * p = prop->findProperty (n);
106  if (p != NULL)
107  p->set (val);
108  else
109  addProperty (n, val);
110 }
111 
112 /* This function adds a property consisting of a key and a double
113  value to the object. */
114 property * object::addProperty (const char * n, nr_double_t val) {
115  property * p = new property (n, val);
116  addProperty (p);
117  return p;
118 }
119 
120 /* This function sets the specified property consisting of a key and a
121  double value in the object. */
122 void object::setProperty (const char * n, nr_double_t val) {
123  property * p = prop->findProperty (n);
124  if (p != NULL)
125  p->set (val);
126  else
127  addProperty (n, val);
128 }
129 
130 /* Th function sets the specified property consisting of a key and a
131  double value in the object. The property is marked a scalability
132  property. */
133 void object::setScaledProperty (const char * n, nr_double_t val) {
134  char prop[64];
135  sprintf (prop, "Scaled:%s", n);
136  setProperty (prop, val);
137 }
138 
139 /* This function adds a property consisting of a key and a variable
140  value to the object. */
141 property * object::addProperty (const char * n, variable * val) {
142  property * p = new property (n, val);
143  addProperty (p);
144  return p;
145 }
146 
147 /* Returns the requested property value which has been previously
148  added as its vector representation. If there is no such property
149  the function returns NULL. */
151  property * p = prop->findProperty (n);
152  if (p != NULL) return p->getVector ();
153  return NULL;
154 }
155 
156 /* Returns the requested property value which has been previously
157  added as its text representation. If there is no such property the
158  function returns NULL. */
159 char * object::getPropertyString (const char * n) {
160  property * p = prop->findProperty (n);
161  if (p != NULL) return p->getString ();
162  return NULL;
163 }
164 
165 /* Returns the requested property reference variable name. If there
166  is no such property the function returns NULL. */
167 char * object::getPropertyReference (const char * n) {
168  property * p = prop->findProperty (n);
169  if (p != NULL) return p->getReference ();
170  return NULL;
171 }
172 
173 /* Returns the requested property value which has been previously
174  added as its double representation. If there is no such property
175  the function returns zero. */
176 nr_double_t object::getPropertyDouble (const char * n) {
177  property * p = prop->findProperty (n);
178  if (p != NULL) return p->getDouble ();
179  return 0.0;
180 }
181 
182 /* The functions returns the requested (scalability) property value
183  which has been previously added. If there is no such scaled
184  property the function returns the standard property or zero. */
185 nr_double_t object::getScaledProperty (const char * n) {
186  char txt[64];
187  sprintf (txt, "Scaled:%s", n);
188  // try to find scaled property
189  property * p = prop->findProperty (txt);
190  if (p != NULL) return p->getDouble ();
191  // default to standard property
192  return getPropertyDouble (n);
193 }
194 
195 /* Returns the requested property value which has been previously
196  added as its integer representation. If there is no such property
197  the function returns zero. */
198 int object::getPropertyInteger (const char * n) {
199  property * p = prop->findProperty (n);
200  if (p != NULL) return p->getInteger ();
201  return 0;
202 }
203 
204 /* The function checks whether the object has got a certain property
205  value. If so it returns non-zero, otherwise it returns zero. */
206 bool object::hasProperty (const char * n) {
207  return (prop && prop->findProperty (n)) ? true : false;
208 }
209 
210 /* The function checks whether the object has got a certain property
211  value and if this has its default value. If so it returns non-zero,
212  otherwise it returns zero. */
213 bool object::isPropertyGiven (const char * n) {
214  if (prop != NULL) {
215  property * p = prop->findProperty (n);
216  if (p != NULL && !p->isDefault ()) return true;
217  }
218  return false;
219 }
220 
221 /* This function copies all properties in the given property list into
222  an object. */
224  prop = NULL;
225  while (org != NULL) {
226  addProperty (new property (*org));
227  org = org->getNext ();
228  }
229 }
230 
231 // Deletes all properties of an object.
233  property * n;
234  for (property * p = prop; p != NULL; p = n) {
235  n = p->getNext ();
236  delete p;
237  }
238  prop = NULL;
239 }
240 
241 // The function returns the number of properties in the object.
243  int res = 0;
244  for (property * p = prop; p != NULL; p = p->getNext ()) res++;
245  return res;
246 }
247 
248 // This function returns a text representation of the objects properties.
249 char * object::propertyList (void) {
250  if (ptxt) free (ptxt);
251  int len = countProperties () + 1;
252  ptxt = (char *) malloc (len); ptxt[0] = '\0';
253  for (property * p = prop; p != NULL; p = p->getNext ()) {
254  char * n = p->getName ();
255  char * val = p->toString ();
256  char * text = (char *) malloc (strlen (n) + strlen (val) + 4);
257  sprintf (text, "%s=\"%s\"", n, val);
258  len += strlen (text);
259  ptxt = (char *) realloc (ptxt, len);
260  strcat (ptxt, text);
261  if (p->getNext () != NULL) strcat (ptxt, " ");
262  free (text);
263  }
264  return ptxt;
265 }
266 
267 } // namespace qucs
void setProperty(const char *, char *)
Definition: object.cpp:104
void setNext(property *p)
Definition: property.h:52
char * getPropertyReference(const char *)
Definition: object.cpp:167
property * findProperty(const char *)
Definition: property.cpp:149
nr_double_t getPropertyDouble(const char *)
Definition: object.cpp:176
int getPropertyInteger(const char *)
Definition: object.cpp:198
property * getNext(void)
Definition: property.h:51
bool hasProperty(const char *)
Definition: object.cpp:206
char * getReference(void)
Definition: property.cpp:179
object * next
Definition: object.h:88
nr_double_t getScaledProperty(const char *)
Definition: object.cpp:185
n
Definition: parse_citi.y:147
bool isPropertyGiven(const char *)
Definition: object.cpp:213
void setScaledProperty(const char *, nr_double_t)
Definition: object.cpp:133
vector * getPropertyVector(const char *)
Definition: object.cpp:150
object * prev
Definition: object.h:89
int countProperties(void)
Definition: object.cpp:242
void setName(const char *)
Definition: object.cpp:78
void deleteProperties(void)
Definition: object.cpp:232
void addProperty(property *)
Definition: object.cpp:89
qucs::vector * getVector(void)
Definition: property.cpp:162
free($1)
nr_double_t getDouble(void)
Definition: property.cpp:185
char * getString(void)
Definition: property.cpp:173
property * prop
Definition: object.h:90
virtual ~object()
Definition: object.cpp:71
char * ptxt
Definition: object.h:91
int getInteger(void)
Definition: property.cpp:196
char * propertyList(void)
Definition: object.cpp:249
char * getName(void)
Definition: object.cpp:84
char * name
Definition: object.h:87
char * getPropertyString(const char *)
Definition: object.cpp:159
void set(nr_double_t)
Definition: property.cpp:202
void copyProperties(property *)
Definition: object.cpp:223