Qucs-core  0.0.19
object.cpp
Go to the documentation of this file.
00001 /*
00002  * object.cpp - generic object class implementation
00003  *
00004  * Copyright (C) 2003, 2004, 2005, 2006, 2008 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: object.cpp 1870 2013-03-06 12:52:07Z crobarcro $
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 <assert.h>
00033 #include <utility>
00034 
00035 #include "logging.h"
00036 #include "complex.h"
00037 #include "property.h"
00038 #include "object.h"
00039 #include "variable.h"
00040 
00041 namespace qucs {
00042 
00043 /* This function adds a property consisting of a key and a string
00044    value to the object. */
00045 void object::addProperty (const std::string &n, const char * const val, const bool def) {
00046   property p;
00047   p.set(val);
00048   p.setDefault(def);
00049   props.insert({{n,p}});
00050 }
00051 
00052 /* This function sets the specified property consisting of a key and a
00053    string value in the object. */
00054 void object::setProperty (const std::string &n, const char * const val) {
00055     auto it = props.find(n);
00056     if(it != props.end())
00057       (*it).second.set(val);
00058     else
00059       addProperty (n, val);
00060 }
00061 
00062 /* This function adds a property consisting of a key and a double
00063    value to the object. */
00064 void object::addProperty (const std::string &n, const nr_double_t val, const bool def) {
00065   property p;
00066   p.set(val);
00067   p.setDefault(def);
00068   props.insert({{n,p}});
00069 }
00070 
00071 /* This function sets the specified property consisting of a key and a
00072    double value in the object. */
00073 void object::setProperty (const std::string &n, const nr_double_t val) {
00074   auto it = props.find(n);
00075   if(it != props.end())
00076     (*it).second.set(val);
00077   else
00078     addProperty (n, val);
00079 }
00080 
00081 /* Th function sets the specified property consisting of a key and a
00082    double value in the object.  The property is marked a scalability
00083    property. */
00084 void object::setScaledProperty (const std::string &n, const nr_double_t val) {
00085   setProperty ("Scaled:"+n, val);
00086 }
00087 
00088 /* This function adds a property consisting of a key and a variable
00089    value to the object. */
00090 void object::addProperty (const std::string &n, variable * const val, const bool def) {
00091   property p;
00092   p.set(val);
00093   p.setDefault(def);
00094   props.insert({{n,p}});
00095 }
00096 
00097 /* Returns the requested property value which has been previously
00098    added as its vector representation.  If there is no such property
00099    the function returns NULL. */
00100 qucs::vector * object::getPropertyVector (const std::string &n) const {
00101   const auto &it = props.find(n);
00102   if(it != props.end())
00103     return (*it).second.getVector();
00104   else
00105     return NULL;
00106 }
00107 
00108 /* Returns the requested property value which has been previously
00109    added as its text representation.  If there is no such property the
00110    function returns NULL. */
00111 const char * object::getPropertyString (const std::string &n) const {
00112   const auto &it = props.find(n);
00113   if(it != props.end())
00114     return (*it).second.getString();
00115   else
00116     return NULL;
00117 }
00118 
00119 /* Returns the requested property reference variable name.  If there
00120    is no such property the function returns NULL. */
00121 const char * object::getPropertyReference (const std::string &n) const {
00122   const auto &it = props.find(n);
00123   if(it != props.end())
00124     return (*it).second.getReference();
00125   else
00126     return NULL;
00127 }
00128 
00129 /* Returns the requested property value which has been previously
00130    added as its double representation.  If there is no such property
00131    the function returns zero. */
00132 nr_double_t object::getPropertyDouble (const std::string &n) const {
00133   const auto &it = props.find(n);
00134   if(it != props.end())
00135     return (*it).second.getDouble();
00136   else
00137     return 0.0;
00138 }
00139 
00140 /* The functions returns the requested (scalability) property value
00141    which has been previously added.  If there is no such scaled
00142    property the function returns the standard property or zero. */
00143 nr_double_t object::getScaledProperty (const std::string &n) const{
00144   std::string txt = "Scaled:"+n;
00145   const auto &it = props.find(txt);
00146   if(it != props.end())
00147     return (*it).second.getDouble();
00148   else
00149     return this->getPropertyDouble(n);
00150 }
00151 
00152 /* Returns the requested property value which has been previously
00153    added as its integer representation.  If there is no such property
00154    the function returns zero. */
00155 int object::getPropertyInteger (const std::string &n) const {
00156   const auto &it = props.find(n);
00157   if(it != props.end())
00158     return (*it).second.getInteger();
00159   else
00160     return 0;
00161 }
00162 
00163 /* The function checks whether the object has got a certain property
00164    value.  If so it returns non-zero, otherwise it returns zero. */
00165 bool object::hasProperty (const std::string &n) const {
00166   return props.find(n) !=  props.end();
00167 }
00168 
00169 /* The function checks whether the object has got a certain property
00170    value and if this has its default value.  If so it returns  non-zero,
00171    otherwise it returns zero. */
00172 bool object::isPropertyGiven (const std::string &n) const {
00173   const auto &it = props.find(n);
00174   if(it != props.end())
00175     return !(*it).second.isDefault();
00176   else
00177     return false;
00178 }
00179 
00180 // The function returns the number of properties in the object.
00181 int object::countProperties (void) const {
00182   return props.size();
00183 }
00184 
00185 // This function returns a text representation of the objects properties.
00186 const char * object::propertyList (void) const {
00187   std::string ptxt;
00188   for(auto it = props.cbegin(); it != props.cend(); ++it) {      
00189     std::string n = it->first;    
00190     std::string val = it->second.toString ();
00191     std::string text = n+"=\""+val+"\"";
00192     ptxt += text;
00193   }
00194   return ptxt.c_str();
00195 }
00196 
00197 } // namespace qucs