src/Box.h
changeset 168 4d0fff61d65e
child 169 0456a16c2a63
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Box.h	Sun Aug 05 21:24:41 2012 -0700
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* vim: set ts=4 sw=4 tw=99 et:
     1.5 + *
     1.6 + * Copyright (C) 2012 David Anderson
     1.7 + *
     1.8 + * This file is part of SourcePawn.
     1.9 + *
    1.10 + * SourcePawn is free software: you can redistribute it and/or modify it under
    1.11 + * the terms of the GNU General Public License as published by the Free
    1.12 + * Software Foundation, either version 3 of the License, or (at your option)
    1.13 + * any later version.
    1.14 + * 
    1.15 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License along with
    1.20 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.21 + */
    1.22 +#ifndef _include_sourcepawn_box_h_
    1.23 +#define _include_sourcepawn_box_h_
    1.24 +
    1.25 +#include "Objects.h"
    1.26 +#include "Handles.h"
    1.27 +#include "Heap.h"
    1.28 +#include "Types.h"
    1.29 +
    1.30 +namespace ke {
    1.31 +
    1.32 +class Type;
    1.33 +
    1.34 +class Box : public Object
    1.35 +{
    1.36 +    Barriered<Type> type_;
    1.37 +    union {
    1.38 +        Object *obj;
    1.39 +        float f;
    1.40 +        int i32;
    1.41 +    } u;
    1.42 +
    1.43 +  public:
    1.44 +    static Box *New(Zone *zone, Handle<Type> type, int i32, Heap::Tenure = Heap::Tenure_Default);
    1.45 +    static Box *New(Zone *zone, Handle<Type> type, float f, Heap::Tenure = Heap::Tenure_Default);
    1.46 +    static Box *New(Zone *zone, Handle<Type> type, Object *obj, Heap::Tenure = Heap::Tenure_Default);
    1.47 +
    1.48 +    Type *type() {
    1.49 +        return type_;
    1.50 +    }
    1.51 +
    1.52 +    int toInt32() {
    1.53 +        assert(type()->pod() == PrimitiveType_Int32);
    1.54 +        return u.i32;
    1.55 +    }
    1.56 +    float toFloat() {
    1.57 +        assert(type()->pod() == PrimitiveType_Float);
    1.58 +        return u.f;
    1.59 +    }
    1.60 +
    1.61 +    // Begin GC descriptor.
    1.62 +    static inline size_t offsetOfType() {
    1.63 +        return OFFSETOF(Box, type_);
    1.64 +    }
    1.65 +    static inline size_t offsetOfObj() {
    1.66 +        return OFFSETOF(Box, u.obj);
    1.67 +    }
    1.68 +    // End GC descriptor.
    1.69 +    
    1.70 +    static inline Box *cast(Object *obj) {
    1.71 +        assert(!obj || obj->is(MapKind_Box));
    1.72 +        return reinterpret_cast<Box *>(obj);
    1.73 +    }
    1.74 +};
    1.75 +
    1.76 +}
    1.77 +
    1.78 +#endif // _include_sourcepawn_box_h_
    1.79 +