SRefAttr.h 8.23 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
#ifndef OPAL_SRefAttr_HH
#define OPAL_SRefAttr_HH

// ------------------------------------------------------------------------
// $RCSfile: SRefAttr.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.2 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Template class SRefAttr<T>
//
// ------------------------------------------------------------------------
//
// $Date: 2000/04/07 12:02:54 $
// $Author: opal $
//
// ------------------------------------------------------------------------

#include "AbstractObjects/AttributeBase.h"
#include "AbstractObjects/OpalData.h"
#include "AbstractObjects/Object.h"
#include "Expressions/ADeferred.h"
#include "Expressions/SValue.h"
#include "Utilities/OpalException.h"
kraus's avatar
kraus committed
27
//#include "Utilities/Options.h"
28
#include "Utilities/Options.h"
gsell's avatar
gsell committed
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
#include <vector>


namespace Expressions {

    // Class SRefAttr
    // ----------------------------------------------------------------------

    /// An attribute defined as a reference to a scalar.
    //  The referred attribute may have values of type real, logical or string.
    //  When a reference is seen, the pointers to the relevant object and
    //  attribute are left zero.  When the expression value is required,
    //  the object and the attribute are searched, and the pointers cached.
    //  The reference is registered with the object.  If the object referred
    //  to is deleted, it calls the invalidate() method of all reference
    //  expressions referring to it.  This resets the pointers to zero, so
    //  that the next evaluation forces to search for a replacement object.

    template <class T>
    class SRefAttr: public AttributeBase {

    public:

        /// Constructor.
        //  Use object name [b]oName[/b] to identify the object containing
        //  the scalar, and [b]aName[/b] to identify the scalar itself.
Steve Russell's avatar
Steve Russell committed
55
        SRefAttr(const std::string &oName, const std::string &aName, int index);
gsell's avatar
gsell committed
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

        SRefAttr(const SRefAttr &);
        virtual ~SRefAttr();

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

        /// Evaluate.
        //  Evaluate the reference and return the value.
        virtual T evaluate() const;

        /// Return real value.
        //  This function has been added for speed of access.
        virtual double getReal();

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

        /// Print the reference.
        virtual void print(std::ostream &) const;

        /// Store new value.
        //  Evaluate the reference and assign to the scalar referred to.
        virtual void set(const T &) const;

    private:

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

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

        // The name of the type referred to.
Steve Russell's avatar
Steve Russell committed
92
        static const std::string typeName;
gsell's avatar
gsell committed
93 94

        // The referred object, attribute and index.
Steve Russell's avatar
Steve Russell committed
95 96
        const std::string obj_name;
        const std::string att_name;
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
        const int itsIndex;

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


    template <class T>
    inline std::ostream &operator<<(std::ostream &os, const SRefAttr<T> &a) {
        a.print(os);
        return os;
    }


    // Implementation of class SRefAttr<T>.
    // ------------------------------------------------------------------------

    template <class T>
    SRefAttr<T>::SRefAttr
Steve Russell's avatar
Steve Russell committed
117
    (const std::string &oName, const std::string &aName, int index):
gsell's avatar
gsell committed
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
        obj_name(oName), att_name(aName), itsIndex(index),
        itsObject(0), itsAttr(0)
    {}

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


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



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


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

        if(AttributeBase *base = &itsAttr->getBase()) {
            if(itsIndex) {
                if(ADeferred<T> *value = dynamic_cast<ADeferred<T>*>(base)) {
                    std::vector<T> array = value->evaluate();
                    if(itsIndex > int(array.size())) {
                        throw OpalException("SRefAttr::evaluate()", "Reference \"" +
                                            getImage() + "\" has index out of range.");
                    } else {
Steve Russell's avatar
Steve Russell committed
154
                        return array[itsIndex - 1];
gsell's avatar
gsell committed
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
                    }
                } else {
                    throw OpalException("SRefAttr::evaluate()", "Reference \"" +
                                        getImage() + "\" is not an array.");
                }
            } else {
                if(SValue<T> *value = dynamic_cast<SValue<T> *>(base)) {
                    return value->evaluate();
                } else {
                    throw OpalException("SRefAttr::evaluate()", getImage() +
                                        "\" is of the wrong type.");
                }
            }
        }

        return T(0);
    }


    template <class T>
    double SRefAttr<T>::getReal() {
        throw OpalException("SValue<T>::getReal()",
                            "Attribute is not of real type.");
    }


    template <> inline
    double SRefAttr<double>::getReal() {
        return evaluate();
    }


    template <class T>
    void SRefAttr<T>::print(std::ostream &os) const {
        os << obj_name;
        if(! att_name.empty()) os << "->" << att_name;
        if(itsIndex != 0) os << '[' << itsIndex << ']';
        return;
    }


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


    template <class T>
    void SRefAttr<T>::set(const T &value) const {
        fill();

        if(AttributeBase *base = &itsAttr->getBase()) {
            if(dynamic_cast<SValue<T> *>(base)) {
                return itsAttr->set(new SValue<T>(value));
            } else {
                throw OpalException("Real::get()", "Attribute \"" +
                                    itsAttr->getName() + "\" is of the wrong type.");
            }
        }
    }


    template <class T>
    void SRefAttr<T>::fill() const {
        if(itsObject == 0) {
            itsObject = OpalData::getInstance()->find(obj_name);
            if(itsObject == 0) {
                if(att_name.empty()  &&  itsIndex <= 0) {
                    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("SRefAttr::fill()",
                                        "Object \"" + obj_name + "\" is unknown.");
                }
            }

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

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

}

#endif // OPAL_SRefAttr_HH