Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
property.cpp
Go to the documentation of this file.
1 /*
2  * property.cpp - generic property class implementation
3  *
4  * Copyright (C) 2003-2009 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$
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 <ctype.h>
33 #include <cmath>
34 
35 #include "complex.h"
36 #include "variable.h"
37 #include "property.h"
38 
39 namespace qucs {
40 
41 using namespace eqn;
42 
43 // Constructor creates an unnamed instance of the property class.
46  name = NULL;
47  value = 0.0;
48  str = NULL;
49  txt = NULL;
50  var = NULL;
51  next = NULL;
52  def = false;
53 }
54 
55 // Constructor creates a named instance of the property class.
56 property::property (const char * n) {
58  name = n ? strdup (n) : NULL;
59  value = 0.0;
60  str = NULL;
61  txt = NULL;
62  var = NULL;
63  next = NULL;
64  def = false;
65 }
66 
67 /* This full qualified constructor creates an instance of the property
68  class containing both the key and the value of the property. */
69 property::property (const char * n, const char * val) {
71  name = n ? strdup (n) : NULL;
72  str = val ? strdup (val) : NULL;
73  value = 0.0;
74  txt = NULL;
75  var = NULL;
76  next = NULL;
77  def = false;
78 }
79 
80 /* This full qualified constructor creates an instance of the property
81  class containing both the key and the value of the property. */
82 property::property (const char * n, nr_double_t val) {
84  name = n ? strdup (n) : NULL;
85  value = val;
86  str = NULL;
87  txt = NULL;
88  var = NULL;
89  next = NULL;
90  def = false;
91 }
92 
93 /* This full qualified constructor creates an instance of the property
94  class containing both the key and the value of the property. */
95 property::property (const char * n, variable * val) {
97  name = n ? strdup (n) : NULL;
98  var = val;
99  value = 0.0;
100  txt = NULL;
101  str = NULL;
102  next = NULL;
103  def = false;
104 }
105 
106 /* The copy constructor creates a new instance of the property class
107  based on the given property object. */
109  type = p.type;
110  name = p.name ? strdup (p.name) : NULL;
111  str = p.str ? strdup (p.str) : NULL;
112  value = p.value;
113  txt = p.txt ? strdup (p.txt) : NULL;
114  next = p.next;
115  var = p.var;
116  def = p.def;
117 }
118 
119 // Destructor deletes the property object.
121 #if 0 /* FIXME: do this at another code location */
122  if (type == PROPERTY_VAR) {
123  constant * c = var->getConstant ();
124  if (c->getType () == TAG_VECTOR) {
125  delete c;
126  delete var;
127  }
128  }
129 #endif
130  if (name) free (name);
131  if (str) free (str);
132  if (txt) free (txt);
133 }
134 
135 // Sets the name of the property.
136 void property::setName (char * n) {
137  if (name) free (name);
138  name = n ? strdup (n) : NULL;
139 }
140 
141 // Returns the name of the property.
142 char * property::getName (void) {
143  return name;
144 }
145 
146 /* Goes through the chained list of the properties and looks for a
147  property matching the given key and returns its value if possible.
148  If there is no such property the function returns NULL. */
149 property * property::findProperty (const char * n) {
150  for (property * p = this; p != NULL; p = p->getNext ()) {
151  if (!strcmp (p->getName (), n)) return p;
152  }
153  return NULL;
154 }
155 
156 // Short macro in order to obtain the correct constant value.
157 #define D(con) ((constant *) (con))->d
158 #define S(con) ((constant *) (con))->s
159 #define V(con) ((constant *) (con))->v
160 
161 // Returns the property's value as vector.
163  if (var != NULL) {
164  if (var->getType () == VAR_CONSTANT)
165  return V (var->getConstant ());
166  else if (var->getType () == VAR_REFERENCE)
167  return V (var->getReference()->getResult ());
168  }
169  return NULL;
170 }
171 
172 // Returns the property's value as string.
173 char * property::getString (void) {
174  if (var != NULL) return S (var->getConstant ());
175  return str;
176 }
177 
178 // Returns the property's reference if it is a variable.
179 char * property::getReference (void) {
180  if (var != NULL) return var->getName ();
181  return str;
182 }
183 
184 // Returns the property's value as double.
185 nr_double_t property::getDouble (void) {
186  if (var != NULL) {
187  if (var->getType () == VAR_CONSTANT)
188  return D (var->getConstant ());
189  else if (var->getType () == VAR_REFERENCE)
190  return D (var->getReference()->getResult ());
191  }
192  return value;
193 }
194 
195 // Returns the property's value as integer.
197  if (var != NULL) return (int) std::floor (D (var->getConstant ()));
198  return (int) std::floor (value);
199 }
200 
201 // Sets the property's value being a double.
202 void property::set (nr_double_t val) {
204  value = val;
205 }
206 
207 // Sets the property's value being an integer.
208 void property::set (int val) {
209  type = PROPERTY_INT;
210  value = val;
211 }
212 
213 // Sets the property's value being a variable.
214 void property::set (variable * val) {
215  type = PROPERTY_VAR;
216  var = val;
217 }
218 
219 // Sets the property's value being a string.
220 void property::set (char * val) {
221  type = PROPERTY_STR;
222  if (str) free (str);
223  str = val ? strdup (val) : NULL;
224 }
225 
226 // This function returns a text representation of the property object.
227 char * property::toString (void) {
228  char text[256];
229  if (txt) free (txt);
230  switch (type) {
231  case PROPERTY_UNKNOWN:
232  txt = strdup ("(no such type)");
233  break;
234  case PROPERTY_INT:
235  sprintf (text, "%d", (int) std::floor (value));
236  txt = strdup (text);
237  break;
238  case PROPERTY_STR:
239  txt = strdup (str);
240  break;
241  case PROPERTY_DOUBLE:
242  sprintf (text, "%g", (double) value);
243  txt = strdup (text);
244  break;
245  case PROPERTY_VAR:
246  sprintf (text, "%s", var->getName ());
247  txt = strdup (text);
248  break;
249  }
250  return txt;
251 }
252 
253 } // namespace qucs
property * findProperty(const char *)
Definition: property.cpp:149
name
Definition: parse_mdl.y:352
property * getNext(void)
Definition: property.h:51
char * getReference(void)
Definition: property.cpp:179
n
Definition: parse_citi.y:147
#define S(con)
Definition: property.cpp:158
char * getName(void)
Definition: property.cpp:142
virtual ~property()
Definition: property.cpp:120
variable * var
Definition: property.h:76
next
Definition: parse_spice.y:859
char * str
Definition: property.h:73
qucs::vector * getVector(void)
Definition: property.cpp:162
free($1)
char * name
Definition: property.h:72
#define D(con)
Definition: property.cpp:157
nr_complex_t floor(const nr_complex_t z)
Complex floor.
Definition: complex.cpp:623
nr_double_t getDouble(void)
Definition: property.cpp:185
value
Definition: parse_vcd.y:315
char * getString(void)
Definition: property.cpp:173
type
Definition: parse_vcd.y:164
property * next
Definition: property.h:77
var
Definition: parse_citi.y:145
int getInteger(void)
Definition: property.cpp:196
char * toString(void)
Definition: property.cpp:227
nr_double_t value
Definition: property.h:75
void set(nr_double_t)
Definition: property.cpp:202
#define V(con)
Definition: property.cpp:159
char * txt
Definition: property.h:74
void setName(char *)
Definition: property.cpp:136