ARefExpr.h 5.63 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#ifndef OPAL_ARefExpr_HH
#define OPAL_ARefExpr_HH

// ------------------------------------------------------------------------
// $RCSfile: ARefExpr.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.3.4.2 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class ARefExpr:
//
// ------------------------------------------------------------------------
//
// $Date: 2004/11/18 22:52:40 $
// $Author: jsberg $
//
// ------------------------------------------------------------------------

#include "AbstractObjects/Expressions.h"
#include "AbstractObjects/Invalidator.h"
#include "Expressions/AValue.h"
#include "Expressions/SValue.h"
#include "Utilities/OpalException.h"
#include "AbstractObjects/OpalData.h"
#include <iosfwd>
#if defined(__GNUC__) && __GNUC__ < 3
#include <strstream>
#else
#include <sstream>
#endif
#include <vector>


namespace Expressions {

    // Class ARefExpr
    // ------------------------------------------------------------------------
    /// An expression defined as a reference to an array.
    //  The components of the array may be real, logical or string.
    //  Reference expressions are (re)evaluated as needed as part of the
    //  containing expression.

    template <class T>
46
    class ARefExpr: public OArray<T>, public Invalidator {
gsell's avatar
gsell committed
47 48 49 50 51 52

    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
53
        ARefExpr(const std::string &objName, const std::string &attName);
gsell's avatar
gsell committed
54 55 56 57 58

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

        /// Make clone.
59
        virtual OArray<T> *clone() const;
gsell's avatar
gsell committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

        /// Evaluate the reference and return the value referred to.
        virtual std::vector<T> evaluate() const;

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

    private:

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

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

        // Make print image.
Steve Russell's avatar
Steve Russell committed
76
        const std::string getImage() const;
gsell's avatar
gsell committed
77 78

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

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


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

    template <class T>
    ARefExpr<T>::ARefExpr
Steve Russell's avatar
Steve Russell committed
93
    (const std::string &objName, const std::string &attName):
gsell's avatar
gsell committed
94 95 96 97 98 99 100
        obj_name(objName), att_name(attName),
        itsObject(0), itsAttr(0)
    {}


    template <class T>
    ARefExpr<T>::ARefExpr(const ARefExpr<T> &rhs):
101
        OArray<T>(rhs),Invalidator(rhs),
gsell's avatar
gsell committed
102 103 104 105 106 107 108 109 110 111 112 113
        obj_name(rhs.obj_name), att_name(rhs.att_name),
        itsObject(rhs.itsObject), itsAttr(rhs.itsAttr)
    {}


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


    template <class T>
114
    OArray<T> *ARefExpr<T>::clone() const {
gsell's avatar
gsell committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        return new ARefExpr<T>(*this);
    }


    template <class T>
    inline std::vector<T> ARefExpr<T>::evaluate() const {
        fill();

        if(AttributeBase *base = &itsAttr->getBase()) {
            if(AValue<T> *value = dynamic_cast<AValue<T>*>(base)) {
                return value->evaluate();
            } else if(SValue<T> *value = dynamic_cast<SValue<T>*>(base)) {
                return std::vector<T>(1, value->evaluate());
            } else {
                throw OpalException("ARefExpr::evaluate()", "Reference \"" +
                                    getImage() + "\" is not an array.");
            }
        } else {
            return std::vector<T>();
        }
    }


    template <class T>
Steve Russell's avatar
Steve Russell committed
139
    const std::string ARefExpr<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
#else
        return os.str();
#endif
    }


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


    template <class T>
    void ARefExpr<T>::fill() const {
        if(itsObject == 0) {
            itsObject = OpalData::getInstance()->find(obj_name);
            if(itsObject == 0) {
                throw OpalException("ARefExpr::fill()",
                                    "Object \"" + obj_name + "\" is unknown.");
            }

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

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

}

#endif // OPAL_ARefExpr_HH