Branch data Line data Source code
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.
43 : 476253 : object::object () {
44 : 476253 : name = NULL;
45 : 476253 : prev = next = NULL;
46 : 476253 : prop = NULL;
47 : 476253 : }
48 : :
49 : : // This constructor creates a named instance of the object class.
50 : 1492 : object::object (const char * n) {
51 : : // create a copy of the character array pointed to by n
52 : : // and get a pointer to the copy using strdup
53 : 1492 : name = strdup (n);
54 : 1492 : prev = next = NULL;
55 : 1492 : prop = NULL;
56 : 1492 : }
57 : :
58 : : /* This copy constructor creates a instance of the object class based
59 : : on the given object. */
60 : 9223 : object::object (const object & o) {
61 [ # # ][ + + ]: 9223 : name = o.name ? strdup (o.name) : NULL;
62 : 9223 : next = o.next;
63 : 9223 : prev = o.prev;
64 : 9223 : copyProperties (o.prop);
65 : 9223 : }
66 : :
67 : : // Destructor deletes an instance of the object class.
68 : 486962 : object::~object () {
69 [ # # ][ + + ]: 486962 : if (name) free (name);
70 : 486962 : deleteProperties ();
71 [ # # ][ - + ]: 486962 : }
72 : :
73 : : // Sets the name of the object.
74 : 511553 : void object::setName (const char * const n) {
75 [ + + ]: 511553 : if (name) free (name);
76 [ + - ]: 511553 : name = n ? strdup (n) : NULL;
77 : 511553 : }
78 : :
79 : : // The function adds a complete property to the object property list.
80 : 12857 : void object::addProperty (property * const p) {
81 : 12857 : p->setNext (prop);
82 : 12857 : prop = p;
83 : 12857 : }
84 : :
85 : : /* This function adds a property consisting of a key and a string
86 : : value to the object. */
87 : 894 : property * object::addProperty (const char * const n, const char * const val) {
88 [ + - ][ + - ]: 894 : property * p = new property (n, val);
[ + - ][ + - ]
89 : 894 : addProperty (p);
90 : 894 : return p;
91 : : }
92 : :
93 : : /* This function sets the specified property consisting of a key and a
94 : : string value in the object. */
95 : 8132 : void object::setProperty (const char * const n, const char * const val) {
96 : 8132 : property * p = prop->findProperty (n);
97 [ + + ]: 8132 : if (p != NULL)
98 [ + - ][ + - ]: 8056 : p->set (val);
99 : : else
100 : 76 : addProperty (n, val);
101 : 8132 : }
102 : :
103 : : /* This function adds a property consisting of a key and a double
104 : : value to the object. */
105 : 11850 : property * object::addProperty (const char * const n, const nr_double_t val) {
106 [ + - ][ + - ]: 11850 : property * p = new property (n, val);
[ + - ]
107 : 11850 : addProperty (p);
108 : 11850 : return p;
109 : : }
110 : :
111 : : /* This function sets the specified property consisting of a key and a
112 : : double value in the object. */
113 : 364887 : void object::setProperty (const char * const n, const nr_double_t val) {
114 : 364887 : property * p = prop->findProperty (n);
115 [ + + ]: 364887 : if (p != NULL)
116 : 363038 : p->set (val);
117 : : else
118 : 1849 : addProperty (n, val);
119 : 364887 : }
120 : :
121 : : /* Th function sets the specified property consisting of a key and a
122 : : double value in the object. The property is marked a scalability
123 : : property. */
124 : 345690 : void object::setScaledProperty (const char * const n, const nr_double_t val) {
125 : : char prop[64];
126 : 345690 : sprintf (prop, "Scaled:%s", n);
127 [ + - ]: 345690 : setProperty (prop, val);
128 : 345690 : }
129 : :
130 : : /* This function adds a property consisting of a key and a variable
131 : : value to the object. */
132 : 113 : property * object::addProperty (const char * n, variable * const val) {
133 [ + - ][ + - ]: 113 : property * p = new property (n, val);
[ + - ]
134 : 113 : addProperty (p);
135 : 113 : return p;
136 : : }
137 : :
138 : : /* Returns the requested property value which has been previously
139 : : added as its vector representation. If there is no such property
140 : : the function returns NULL. */
141 : 7274 : qucs::vector * object::getPropertyVector (const char * const n) const {
142 : 7274 : const property * p = prop->findProperty (n);
143 [ + - ]: 7274 : if (p != NULL)
144 : 7274 : return p->getVector ();
145 : 7274 : return NULL;
146 : : }
147 : :
148 : : /* Returns the requested property value which has been previously
149 : : added as its text representation. If there is no such property the
150 : : function returns NULL. */
151 : 112661 : const char * object::getPropertyString (const char * const n) const {
152 : 112661 : const property * p = prop->findProperty (n);
153 [ + - ]: 112661 : if (p != NULL)
154 : 112661 : return p->getString ();
155 : 112661 : return NULL;
156 : : }
157 : :
158 : : /* Returns the requested property reference variable name. If there
159 : : is no such property the function returns NULL. */
160 : 0 : const char * object::getPropertyReference (const char * const n) const {
161 : 0 : const property * p = prop->findProperty (n);
162 [ # # ]: 0 : if (p != NULL)
163 : 0 : return p->getReference ();
164 : 0 : return NULL;
165 : : }
166 : :
167 : : /* Returns the requested property value which has been previously
168 : : added as its double representation. If there is no such property
169 : : the function returns zero. */
170 : 12259868 : nr_double_t object::getPropertyDouble (const char * const n) const {
171 : 12259868 : const property * p = prop->findProperty (n);
172 [ + + ]: 12259868 : if (p != NULL)
173 : 12245865 : return p->getDouble ();
174 : 12259868 : return 0.0;
175 : : }
176 : :
177 : : /* The functions returns the requested (scalability) property value
178 : : which has been previously added. If there is no such scaled
179 : : property the function returns the standard property or zero. */
180 : 6026058 : nr_double_t object::getScaledProperty (const char * const n) const{
181 : : char txt[64];
182 : 6026058 : sprintf (txt, "Scaled:%s", n);
183 : : // try to find scaled property
184 [ + - ]: 6026058 : const property * p = prop->findProperty (txt);
185 [ + + ]: 6026058 : if (p != NULL)
186 [ + - ]: 5809862 : return p->getDouble ();
187 : : // default to standard property
188 [ + - ]: 6026058 : return getPropertyDouble (n);
189 : : }
190 : :
191 : : /* Returns the requested property value which has been previously
192 : : added as its integer representation. If there is no such property
193 : : the function returns zero. */
194 : 370805 : int object::getPropertyInteger (const char * const n) const {
195 : 370805 : const property * p = prop->findProperty (n);
196 [ + - ]: 370805 : if (p != NULL)
197 : 370805 : return p->getInteger ();
198 : 370805 : return 0;
199 : : }
200 : :
201 : : /* The function checks whether the object has got a certain property
202 : : value. If so it returns non-zero, otherwise it returns zero. */
203 : 382246 : bool object::hasProperty (const char * const n) const {
204 [ + - ][ + + ]: 382246 : return (prop && prop->findProperty (n)) ? true : false;
205 : : }
206 : :
207 : : /* The function checks whether the object has got a certain property
208 : : value and if this has its default value. If so it returns non-zero,
209 : : otherwise it returns zero. */
210 : 3950 : bool object::isPropertyGiven (const char * const n) const {
211 [ + - ]: 3950 : if (prop != NULL) {
212 : 3950 : const property * p = prop->findProperty (n);
213 [ + - ][ + + ]: 3950 : if (p != NULL && !p->isDefault ())
[ + + ]
214 : 24 : return true;
215 : : }
216 : 3950 : return false;
217 : : }
218 : :
219 : : /* This function copies all properties in the given property list into
220 : : an object. */
221 : 9223 : void object::copyProperties (property * org) {
222 : 9223 : prop = NULL;
223 [ - + ]: 9223 : while (org != NULL) {
224 [ # # ]: 0 : addProperty (new property (*org));
225 : 0 : org = org->getNext ();
226 : : }
227 : 9223 : }
228 : :
229 : : // Deletes all properties of an object.
230 : 486962 : void object::deleteProperties (void) {
231 : : property * n;
232 [ + + ]: 499819 : for (property * p = prop; p != NULL; p = n) {
233 : 12857 : n = p->getNext ();
234 [ + - ]: 12857 : delete p;
235 : : }
236 : 486962 : prop = NULL;
237 : 486962 : }
238 : :
239 : : // The function returns the number of properties in the object.
240 : 0 : int object::countProperties (void) const {
241 : 0 : int res = 0;
242 [ # # ]: 0 : for (property * p = prop; p != NULL; p = p->getNext ()) res++;
243 : 0 : return res;
244 : : }
245 : :
246 : : // This function returns a text representation of the objects properties.
247 : 0 : const char * object::propertyList (void) const {
248 [ # # ]: 0 : std::string ptxt;
249 [ # # ]: 0 : for (property * p = prop; p != NULL; p = p->getNext ()) {
250 [ # # ]: 0 : std::string n = std::string(p->getName ());
251 [ # # ]: 0 : std::string val = std::string(p->toString ());
252 [ # # ][ # # ]: 0 : std::string text = n+"=\""+val+"\"";
[ # # ]
253 [ # # ]: 0 : ptxt += text;
254 [ # # ]: 0 : if (p->getNext () != NULL)
255 [ # # ]: 0 : ptxt += " ";
256 : 0 : }
257 : 0 : return ptxt.c_str();
258 : : }
259 : :
260 : : } // namespace qucs
|