Qucs-core  0.0.19
property.cpp
Go to the documentation of this file.
00001 /*
00002  * property.cpp - generic property class implementation
00003  *
00004  * Copyright (C) 2003-2009 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #if HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <ctype.h>
00033 #include <cmath>
00034 #include <string>
00035 
00036 #include "complex.h"
00037 #include "variable.h"
00038 #include "property.h"
00039 
00040 namespace qucs {
00041 
00042 using namespace eqn;
00043 
00044 // Constructor creates an unnamed instance of the property class.
00045 property::property () :
00046   str()
00047 {
00048   type = PROPERTY_UNKNOWN;
00049   value = 0.0;
00050   var = NULL;
00051   def = false;
00052 }
00053 
00054 
00055 // Destructor deletes the property object.
00056 property::~property () {
00057 #if 0 /* FIXME: do this at another code location */
00058   if (type == PROPERTY_VAR) {
00059     constant * c = var->getConstant ();
00060     if (c->getType () == TAG_VECTOR) {
00061       delete c;
00062       delete var;
00063     }
00064   }
00065 #endif
00066 }
00067 
00068 
00069 // Short macro in order to obtain the correct constant value.
00070 #define D(con) ((constant *) (con))->d
00071 #define S(con) ((constant *) (con))->s
00072 #define V(con) ((constant *) (con))->v
00073 
00074 // Returns the property's value as vector.
00075 qucs::vector * property::getVector (void) const {
00076   if (var != NULL) {
00077     if (var->getType () == VAR_CONSTANT)
00078       return V (var->getConstant ());
00079     else if (var->getType () == VAR_REFERENCE)
00080       return V (var->getReference()->getResult ());
00081   }
00082   return NULL;
00083 }
00084 
00085 // Returns the property's value as string.
00086 const char * property::getString (void) const {
00087   if (var != NULL)
00088     return S (var->getConstant ());
00089   return str.c_str();
00090 }
00091 
00092 // Returns the property's reference if it is a variable.
00093 const char * property::getReference (void) const {
00094   if (var != NULL)
00095     return var->getName ();
00096   return str.c_str();
00097 }
00098 
00099 // Returns the property's value as double.
00100 nr_double_t property::getDouble (void) const {
00101   if (var != NULL) {
00102     if (var->getType () == VAR_CONSTANT)
00103       return D (var->getConstant ());
00104     else if (var->getType () == VAR_REFERENCE)
00105       return D (var->getReference()->getResult ());
00106   }
00107   return value;
00108 }
00109 
00110 // Returns the property's value as integer.
00111 int property::getInteger (void) const {
00112   if (var != NULL) return (int) std::floor (D (var->getConstant ()));
00113   return (int) std::floor (value);
00114 }
00115 
00116 // Sets the property's value being a double.
00117 void property::set (const nr_double_t val) {
00118   type = PROPERTY_DOUBLE;
00119   value = val;
00120 }
00121 
00122 // Sets the property's value being an integer.
00123 void property::set (const int val) {
00124   type = PROPERTY_INT;
00125   value = val;
00126 }
00127 
00128 // Sets the property's value being a variable.
00129 void property::set (variable * const val) {
00130   type = PROPERTY_VAR;
00131   var = val;
00132 }
00133 
00134 // Sets the property's value being a string.
00135 void property::set (const std::string &val) {
00136   type = PROPERTY_STR;
00137   this->str = val;
00138 }
00139 
00140 // This function returns a text representation of the property object.
00141 std::string property::toString (void) const {
00142   switch (type) {
00143   case PROPERTY_UNKNOWN:
00144     return "(no such type)";
00145     break;
00146   case PROPERTY_INT:
00147     return std::to_string(std::floor(value));
00148     break;
00149   case PROPERTY_STR:
00150     return std::string(this->str);
00151     break;
00152   case PROPERTY_DOUBLE:
00153     return std::to_string(value);
00154     break;
00155   case PROPERTY_VAR:
00156     return var->getName();
00157     break;
00158   }
00159   return "";
00160 }
00161 
00162 } // namespace qucs