RealVariable.cpp 2.21 KB
Newer Older
gsell's avatar
gsell committed
1
//
2 3
// Class RealVariable
//   The REAL VARIABLE definition.
gsell's avatar
gsell committed
4
//
5 6
// Copyright (c) 2000 - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
gsell's avatar
gsell committed
7
//
8 9 10 11 12 13 14 15 16
// This file is part of OPAL.
//
// OPAL 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 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
gsell's avatar
gsell committed
17 18
//
#include "ValueDefinitions/RealVariable.h"
19

gsell's avatar
gsell committed
20 21 22
#include "AbstractObjects/OpalData.h"
#include "Attributes/Attributes.h"

23
#include <iostream>
gsell's avatar
gsell committed
24 25 26 27 28 29 30 31 32


RealVariable::RealVariable():
    ValueDefinition(1, "REAL_VARIABLE",
                    "The \"REAL VARIABLE\" statement defines a global "
                    "real variable:\n"
                    "\tREAL VARIABLE <name>=<real-expression>;\n") {
    itsAttr[0] = Attributes::makeReal("VALUE", "The variable value", 0.0);

33 34
    registerOwnership(AttributeHandler::STATEMENT);

gsell's avatar
gsell committed
35 36 37 38 39 40 41 42
    // Construct the P0 variable.
    RealVariable *p0 = new RealVariable("P0", this, 1.0);
    OpalData::getInstance()->create(p0);
    p0->setDirty(true);
    OpalData::getInstance()->setP0(p0);
}


43
RealVariable::RealVariable(const std::string &name, RealVariable *parent,
gsell's avatar
gsell committed
44 45 46 47 48 49
                           double value):
    ValueDefinition(name, parent) {
    Attributes::setReal(itsAttr[0], value);
}


50
RealVariable::RealVariable(const std::string &name, RealVariable *parent):
gsell's avatar
gsell committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    ValueDefinition(name, parent)
{}


RealVariable::~RealVariable()
{}


bool RealVariable::canReplaceBy(Object *object) {
    // Replace only by another variable.
    return (dynamic_cast<RealVariable *>(object) != 0);
}


65
RealVariable *RealVariable::clone(const std::string &name) {
gsell's avatar
gsell committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    return new RealVariable(name, this);
}


double RealVariable::getReal() const {
    return Attributes::getReal(itsAttr[0]);
}


void RealVariable::print(std::ostream &os) const {
    os << "REAL " << getOpalName()
       << (itsAttr[0].isExpression() ? ":=" : "=") << itsAttr[0] << ';';
    os << std::endl;
}
80 81 82

void RealVariable::printValue(std::ostream &os) const {
    os << itsAttr[0];
83
}