/* Theoretic - Graph Theoretic Byte Code Engineering
 * Copyright (C) 2001-2002  Jay Freeman (saurik)
*/

/*
 *        Redistribution and use in source and binary
 * forms, with or without modification, are permitted
 * provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation
 *    and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse
 *    or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef THEORETIC_VALUE_H
#define THEORETIC_VALUE_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

namespace Theoretic {

    class ValueImpl {
      private:
        uint32_t refs;

      public:
        ValueImpl() : refs(0) {
        }

        void AddRef() {
            ++refs;
        }

        void Release() {
            if (0 == --refs)
                delete this;
        }

        virtual double Double() const = 0;
        virtual uint32_t Ui32() const = 0;
        virtual void Print(std::wostream &out) const = 0;
    };

    template <typename T>
    class BasicValueImpl :
        public ValueImpl
    {
        T value;

      public:
        inline BasicValueImpl() {}
        inline BasicValueImpl(T value) :
            value(value)
        {}

        double Double() const {
            return static_cast<double>(value);
        }

        uint32_t Ui32() const {
            return static_cast<uint32_t>(value);
        }

        void Print(std::wostream &out) const {
            out << value;
        }

        static BasicValueImpl<T> *Parse(uint8_t * &data) {
            return new BasicValueImpl<T>(*((T * &) data)++);
        }
    };

    template <>
    void BasicValueImpl<int64_t>::Print(std::wostream &out) const {
        // XXX: This sucks...
        out << (uint32_t) value;
    }

    class Value {
      private:
        ValueImpl *value;

      public:
        Value() : value(NULL) {}

        ~Value() {
            if (value)
                value->Release();
        }

        template <typename T>
        inline void Set(T value) {
            operator = (new BasicValueImpl<T>(value));
        }

        Value &operator = (ValueImpl *rhs) {
            if (value)
                value->Release();
            value = rhs;
            value->AddRef();
            return *this;
        }

        inline Value &operator = (const Value &rhs) {
            return operator = (rhs.value);
        }

        inline double Double() const {
            return value->Double();
        }

        inline uint32_t Ui32() const {
            return value->Ui32();
        }

        inline void Print(std::wostream &out) const {
            value->Print(out);
        }
    };

    inline std::wostream &operator <<(std::wostream &out, const Value &value) {
        value.Print(out);
        return out;
    }

    typedef BasicValueImpl<uint8_t>  U1Value;
    typedef BasicValueImpl<uint16_t> U2Value;
    typedef BasicValueImpl<uint32_t> U4Value;
    typedef BasicValueImpl<uint64_t> U8Value;

    typedef BasicValueImpl<int8_t>   I1Value;
    typedef BasicValueImpl<int16_t>  I2Value;
    typedef BasicValueImpl<int32_t>  I4Value;
    typedef BasicValueImpl<int64_t>  I8Value;

    typedef BasicValueImpl<float>    R4Value;
    typedef BasicValueImpl<double>   R8Value;

    typedef BasicValueImpl<uint32_t> TValue;

}

#endif//THEORETIC_VALUE_H