/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of SourcePawn.
 *
 * SourcePawn 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.
 * 
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_sourcepawn_box_h_
#define _include_sourcepawn_box_h_

#include "Objects.h"
#include "Handles.h"
#include "Heap.h"
#include "Types.h"

namespace ke {

class Type;

class Box : public Object
{
    Barriered<Type> type_;
    union {
        Object *obj;
        float f;
        int i32;
    } u;

  public:
    static Box *New(Zone *zone, Handle<Type> type, int i32, Heap::Tenure = Heap::Tenure_Default);
    static Box *New(Zone *zone, Handle<Type> type, float f, Heap::Tenure = Heap::Tenure_Default);
    static Box *New(Zone *zone, Handle<Type> type, Object *obj, Heap::Tenure = Heap::Tenure_Default);

    Type *type() {
        return type_;
    }

    int toInt32() {
        assert(type()->pod() == PrimitiveType_Int32);
        return u.i32;
    }
    float toFloat() {
        assert(type()->pod() == PrimitiveType_Float);
        return u.f;
    }

    // Begin GC descriptor.
    static inline size_t offsetOfType() {
        return OFFSETOF(Box, type_);
    }
    static inline size_t offsetOfObj() {
        return OFFSETOF(Box, u.obj);
    }
    // End GC descriptor.
    
    static inline Box *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_Box));
        return reinterpret_cast<Box *>(obj);
    }
};

}

#endif // _include_sourcepawn_box_h_

