1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*$Id: u_parameter.h $ -*- C++ -*-
* Copyright (C) 2005 Albert Davis
* Author: Albert Davis <aldavis@gnu.org>
*
* This file is part of "Gnucap", the Gnu Circuit Analysis Package
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*------------------------------------------------------------------
* A class for parameterized values
* Used for spice compatible .param statements
* and passing arguments to models and subcircuits
*/
//testing=script 2014.11.25
#ifndef U_PARAMETER_H
#define U_PARAMETER_H
#include "u_opt.h"
#include "io_.h"
#include "m_expression.h"
#include "e_cardlist.h"
/*--------------------------------------------------------------------------*/
class LANGUAGE;
/*--------------------------------------------------------------------------*/
class PARA_BASE {
protected:
std::string _s;
public:
explicit PARA_BASE( ): _s(){}
explicit PARA_BASE(const PARA_BASE& p): _s(p._s) {}
explicit PARA_BASE(const std::string s): _s(s){untested();}
virtual ~PARA_BASE(){}
bool has_hard_value()const {return (_s != "");}
virtual bool has_good_value()const = 0;
virtual void parse(CS& cmd) = 0;
virtual void operator=(const std::string& s) = 0;
};
/*--------------------------------------------------------------------------*/
template <class T>
class PARAMETER : public PARA_BASE {
private:
mutable T _v;
public:
explicit PARAMETER() : PARA_BASE(), _v(NOT_INPUT) {}
PARAMETER(const PARAMETER<T>& p) : PARA_BASE(p), _v(p._v) {}
explicit PARAMETER(T v) :PARA_BASE(), _v(v) {}
//explicit PARAMETER(T v, const std::string& s) :_v(v), _s(s) {untested();}
~PARAMETER() {}
bool has_good_value()const override {return (_v != NOT_INPUT);}
//bool has_soft_value()const {untested(); return (has_good_value() && !has_hard_value());}
operator T()const {return _v;}
T e_val(const T& def, const CARD_LIST* scope)const;
void parse(CS& cmd) override;
std::string string()const {
if (_s == "#") {
return to_string(_v);
}else if (_s == "") {
return "NA(" + to_string(_v) + ")";
}else{
return _s;
}
}
void print(OMSTREAM& o)const {o << string();}
void set_default(const T& v) {_v = v; _s = "";}
void operator=(const PARAMETER& p) {_v = p._v; _s = p._s;}
void operator=(const T& v) {_v = v; _s = "#";}
//void operator=(const std::string& s) {untested();_s = s;}
void operator=(const std::string& s)override {
if (strchr("'\"{", s[0])) {
CS cmd(CS::_STRING, s);
_s = cmd.ctos("", "'\"{", "'\"}");
}else if (s == "NA") {
_s = "";
}else{
_s = s;
}
}
bool operator==(const PARAMETER& p)const {
return (_v == p._v && _s == p._s);
}
bool operator==(const T& v)const {
if (v != NOT_INPUT) {
return _v == v;
}else{
return (_v == NOT_INPUT || !has_hard_value());
}
}
//bool operator!=(const PARAMETER& p)const {untested();
// return !(*this == p);
//}
//bool operator!=(const T& v)const {untested();
// return !(*this == v);
//}
private:
T lookup_solve(const T& def, const CARD_LIST* scope)const;
};
/*--------------------------------------------------------------------------*/
/* non-class interface, so non-paramaters can have same syntax */
/* It is needed by the model compiler */
#if 0
inline bool operator==(const PARAMETER<int>& p, double v)
{untested();
if (v != NOT_INPUT) {untested();
return p.operator==(static_cast<int>(v));
}else{untested();
return (!(p.has_value()));
}
}
#endif
inline bool has_hard_value(const PARA_BASE& p)
{
return p.has_hard_value();
}
inline bool has_good_value(const PARA_BASE& p)
{
return p.has_good_value();
}
#if 0
template <class T>
bool has_soft_value(const PARA_BASE& p)
{untested();
return p.has_soft_value();
}
#endif
template <class T>
bool has_nz_value(const T& p)
{
return (has_good_value(p) && p != 0);
}
template <class T>
void set_default(PARAMETER<T>* p, const T& v)
{
assert(p);
p->set_default(v);
}
template <class T>
void set_default(T* p, const T& v)
{
assert(p);
*p = v;
}
template <class T>
void e_val(PARAMETER<T>* p, const PARAMETER<T>& def, const CARD_LIST* scope)
{
assert(p);
p->e_val(def, scope);
}
template <class T>
void e_val(PARAMETER<T>* p, const T& def, const CARD_LIST* scope)
{
assert(p);
p->e_val(def, scope);
}
#if 0
template <class T>
void e_val(T* p, const T& def, const CARD_LIST*)
{untested();
assert(p);
if (*p == NOT_INPUT) {untested();
*p = def;
}else{untested();
}
}
#endif
/*--------------------------------------------------------------------------*/
class INTERFACE PARAM_LIST {
private:
typedef std::map<std::string, PARAMETER<double> > map;
public:
typedef map::const_iterator const_iterator;
typedef map::iterator iterator;
private:
map _pl;
PARAM_LIST const* _try_again; // if you don't find it, also look here
mutable const_iterator _previous;
public:
explicit PARAM_LIST() :_try_again(NULL) {}
explicit PARAM_LIST(const PARAM_LIST& p) :_pl(p._pl), _try_again(p._try_again) {}
//explicit PARAM_LIST(PARAM_LIST* ta) :_try_again(ta) {untested();}
~PARAM_LIST() {}
PARAM_LIST& operator=(const PARAM_LIST& p) { itested();
_pl = p._pl;
return *this;
}
void parse(CS& cmd);
void print(OMSTREAM&, LANGUAGE*)const;
size_t size()const {return _pl.size();}
//bool is_empty()const {untested();return _pl.empty();}
bool is_printable(int)const;
std::string name(int)const;
std::string value(int)const;
void eval_copy(PARAM_LIST const&, const CARD_LIST*);
bool operator==(const PARAM_LIST& p)const{return _pl == p._pl;}
const PARAMETER<double>& deep_lookup(std::string)const;
const PARAMETER<double>& operator[](std::string i)const {return deep_lookup(i);}
void set(std::string, const double&);
void set(std::string, const std::string&);
void set_try_again(PARAM_LIST const* t) {_try_again = t;}
iterator begin() {return _pl.begin();}
iterator end() {return _pl.end();}
const_iterator begin()const {itested(); return _pl.begin();}
const_iterator end()const {itested(); return _pl.end();}
const_iterator find(std::string const& k) const {itested(); return _pl.find(k); }
};
/*--------------------------------------------------------------------------*/
template <>
inline bool PARAMETER<bool>::lookup_solve(const bool&, const CARD_LIST*)const
{
CS cmd(CS::_STRING, _s);
return cmd.ctob();
}
/*--------------------------------------------------------------------------*/
template <class T>
inline T PARAMETER<T>::lookup_solve(const T& def, const CARD_LIST* scope)const
{
CS cmd(CS::_STRING, _s);
Expression e(cmd);
Expression reduced(e, scope);
T v = T(reduced.eval());
if (v != NOT_INPUT) {
return v;
}else{
const PARAM_LIST* pl = scope->params();
return T(pl->deep_lookup(_s).e_val(def, scope));
}
}
/*--------------------------------------------------------------------------*/
#if 0
template <class T>
inline T PARAMETER<T>::lookup_solve(const T& def, const CARD_LIST* scope)const
{untested();
const PARAM_LIST* pl = scope->params();
return T(pl->deep_lookup(_s).e_val(def, scope));
}
#endif
/*--------------------------------------------------------------------------*/
template <class T>
T PARAMETER<T>::e_val(const T& def, const CARD_LIST* scope)const
{
assert(scope);
static int recursion=0;
static const std::string* first_name = NULL;
if (recursion == 0) {
first_name = &_s;
}else{
}
assert(first_name);
++recursion;
if (_s == "") {
// blank string means to use default value
_v = def;
if (recursion > 1) {
error(bWARNING, "parameter " + *first_name + " not specified, using default\n");
//BUG// needs to show scope
}else{
}
}else if (_s != "#") {
// anything else means look up the value
if (recursion <= OPT::recursion) {
_v = lookup_solve(def, scope);
if (_v == NOT_INPUT) {
error(bDANGER, "parameter " + *first_name + " value is \"NOT_INPUT\"\n");
//BUG// needs to show scope
//BUG// it is likely to have a numeric overflow resulting from the bad value
}else{
}
}else{itested();
_v = def;
error(bDANGER, "parameter " + *first_name + " recursion too deep\n");
}
}else{
// start with # means we have a final value
}
--recursion;
return _v;
}
/*--------------------------------------------------------------------------*/
template <>
inline void PARAMETER<bool>::parse(CS& cmd)
{
bool new_val;
cmd >> new_val;
if (cmd) {
_v = new_val;
_s = "#";
}else{untested();
std::string name;
//cmd >> name;
name = cmd.ctos(",=();", "'{\"", "'}\"");
if (cmd) {untested();
if (name == "NA") {untested();
_s = "";
}else{untested();
_s = name;
}
}else{untested();
}
}
if (!cmd) {untested();
_v = true;
_s = "#";
}else{
}
}
/*--------------------------------------------------------------------------*/
template <class T>
inline void PARAMETER<T>::parse(CS& cmd)
{
T new_val;
cmd >> new_val;
if (cmd) {
_v = new_val;
_s = "#";
}else{
std::string name;
//cmd >> name;
name = cmd.ctos(",=();", "'{\"", "'}\"");
if (cmd) {
if (cmd.match1('(')) {
_s = name + '(' + cmd.ctos("", "(", ")") + ')';
}else{
_s = name;
}
if (name == "NA") {untested();
_s = "";
}else{
}
}else{
}
}
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
INTERFACE bool Get(CS& cmd, const std::string& key, PARAMETER<bool>* val);
INTERFACE bool Get(CS& cmd, const std::string& key, PARAMETER<int>* val);
/*--------------------------------------------------------------------------*/
template <class T>
inline OMSTREAM& operator<<(OMSTREAM& o, const PARAMETER<T> p)
{
p.print(o);
return o;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
#endif
// vim:ts=8:sw=2:noet:
|