SRefExpr.h 6.15 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#ifndef OPAL_SRefExpr_HH
#define OPAL_SRefExpr_HH

// ------------------------------------------------------------------------
// $RCSfile: SRefExpr.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.4.4.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class SRefExpr:
//
// ------------------------------------------------------------------------
//
// $Date: 2002/12/09 15:06:08 $
// $Author: jsberg $
//
// ------------------------------------------------------------------------

#include "AbstractObjects/Expressions.h"
#include "AbstractObjects/Invalidator.h"
23
#include "AbstractObjects/OpalData.h"
gsell's avatar
gsell committed
24 25
#include "Expressions/SValue.h"
#include "Utilities/OpalException.h"
26
#include "Utilities/Options.h"
gsell's avatar
gsell committed
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
#include <iosfwd>
#if defined(__GNUC__) && __GNUC__ < 3
#include <strstream>
#else
#include <sstream>
#endif


namespace Expressions {

    // Class SRefExpr
    // ----------------------------------------------------------------------
    /// An expression defined as a reference to a scalar.
    //  The referred attribute may have values of type real, logical or string.
    //  Reference expressions are (re)evaluated as needed as part of the
    //  containing expression.

    template <class T>
    class SRefExpr: public Scalar<T>, public Invalidator {

    public:

        /// Constructor.
        //  Use [b]objName[/b] to identify the object containg the array, and
        //  [b]attName[/b] to identify the array itself.
Steve Russell's avatar
Steve Russell committed
52
        SRefExpr(const std::string &objName, const std::string &attName);
gsell's avatar
gsell committed
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

        SRefExpr(const SRefExpr<T> &rhs);
        virtual ~SRefExpr();

        /// Make clone.
        virtual Scalar<T> *clone() const;

        /// Evaluate.
        virtual T evaluate() const;

        /// Invalidate.
        //  Force re-evaluation of the reference.
        virtual void invalidate();

        /// Print expression.
        virtual void print(std::ostream &os, int precedence = 99) const;

    private:

        // Not implemented.
        void operator=(const SRefExpr &);

        // Fill in the reference.
        void fill() const;

        // Make print image.
Steve Russell's avatar
Steve Russell committed
79
        const std::string getImage() const;
gsell's avatar
gsell committed
80 81

        // The referred object and attribute names.
Steve Russell's avatar
Steve Russell committed
82 83
        const std::string obj_name;
        const std::string att_name;
gsell's avatar
gsell committed
84 85 86 87 88 89 90 91 92 93 94 95

        // The object and attribute referred to.
        mutable Object    *itsObject;
        mutable Attribute *itsAttr;
    };


    // Implementation
    // ------------------------------------------------------------------------

    template <class T>
    SRefExpr<T>::SRefExpr
Steve Russell's avatar
Steve Russell committed
96
    (const std::string &objName, const std::string &attName):
gsell's avatar
gsell committed
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
        obj_name(objName), att_name(attName),
        itsObject(0), itsAttr(0)
    {}


    template <class T>
    SRefExpr<T>::SRefExpr(const SRefExpr<T> &rhs):
        obj_name(rhs.obj_name), att_name(rhs.att_name),
        itsObject(rhs.itsObject), itsAttr(rhs.itsAttr)
    {}


    template <class T>
    SRefExpr<T>::~SRefExpr() {
        if(itsObject) itsObject->unregisterReference(this);
    }


    template <class T>
    Scalar<T> *SRefExpr<T>::clone() const {
        return new SRefExpr<T>(*this);
    }


    template <class T>
    inline T SRefExpr<T>::evaluate() const {
        fill();

        if(AttributeBase *base = &itsAttr->getBase()) {
            if(SValue<T> *value = dynamic_cast<SValue<T>*>(base)) {
                return value->evaluate();
            } else {
                throw OpalException("SRefExpr::evaluate()", "Reference \"" +
                                    getImage() + "\" is not a variable.");
            }
        }

        return T(0);
    }


    template <class T>
Steve Russell's avatar
Steve Russell committed
139
    const std::string SRefExpr<T>::getImage() const {
gsell's avatar
gsell committed
140 141 142 143 144 145 146 147 148
#if defined(__GNUC__) && __GNUC__ < 3
        char buffer[128];
        std::ostrstream os(buffer, sizeof(buffer));
#else
        std::ostringstream os;
#endif
        print(os);
        os << std::ends;
#if defined(__GNUC__) && __GNUC__ < 3
Steve Russell's avatar
Steve Russell committed
149
        return std::string(buffer);
gsell's avatar
gsell committed
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
#else
        return os.str();
#endif
    }


    template <class T>
    void SRefExpr<T>::invalidate() {
        itsObject = 0;
        itsAttr = 0;
    }


    template <class T>
    void SRefExpr<T>::print(std::ostream &os, int) const {
        os << obj_name;
        if(! att_name.empty()) os << "->" << att_name;
        return;
    }


    template <class T>
    void SRefExpr<T>::fill() const {
        if(itsObject == 0) {
            itsObject = OpalData::getInstance()->find(obj_name);
            if(itsObject == 0) {
                if(att_name.empty()) {
                    static Object *variable = OpalData::getInstance()->find("REAL_VARIABLE");
                    itsObject = variable->clone(obj_name);
                    OpalData::getInstance()->define(itsObject);

                    if(Options::verify) {
                        std::cerr << "\nThe <variable> \"" << obj_name
                                  << "\" is unknown, created with zero value.\n"
                                  << std::endl;
                    }
                } else {
                    throw OpalException("SRefExpr::fill()",
                                        "Object \"" + obj_name + "\" is unknown.");
                }
            }

            // Register the reference with the object, to allow invalidation
            // when the object is deleted.
            itsObject->registerReference(const_cast<SRefExpr<T>*>(this));

            if(att_name.empty()) {
                itsAttr = itsObject->findAttribute("VALUE");
                if(itsAttr == 0) {
                    throw OpalException("SRefExpr::fill()", "Object \"" + obj_name +
                                        "\" is not a variable, constant or vector.");
                }
            } else {
                itsAttr = itsObject->findAttribute(att_name);
                if(itsAttr == 0) {
                    throw OpalException("SRefExpr::fill()", "Attribute \"" + obj_name +
                                        "->" + att_name + "\" is unknown.");
                }
            }
        }
    }

}

#endif // OPAL_SRefExpr_HH