src/Structures.h
author David Anderson <dvander@alliedmods.net>
Sat Aug 04 17:51:43 2012 -0700 (2012-08-04)
changeset 146 dc7d3b7a918c
parent 86 32ad0523b309
child 147 9c544823d090
permissions -rw-r--r--
Basic support for parsing and creating types from structure syntax.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jc_structure_h_
    20 #define _include_jc_structure_h_
    21 
    22 #include "Objects.h"
    23 #include "Handles.h"
    24 #include "Vector.h"
    25 
    26 namespace ke {
    27 
    28 class Type;
    29 class String;
    30 class FixedArray;
    31 
    32 // When describing composite structures, the GC must be able to trace interior
    33 // references to Objects. To make this possible, maps that describe composite
    34 // structures must provide a list of offsets into their objects that are
    35 // traceable. StructureBufferBuilder assists with this.
    36 class StructureBufferBuilder
    37 {
    38   public:
    39     StructureBufferBuilder();
    40 
    41     bool add(Handle<Type> type, unsigned *offset);
    42 
    43     unsigned structureSize() const {
    44         return size_;
    45     }
    46     unsigned traceableOffsetCount() const {
    47         return offsets_.length();
    48     }
    49     const unsigned *offsets() {
    50         return offsets_.buffer();
    51     }
    52 
    53   private:
    54     Vector<unsigned> offsets_;
    55     unsigned size_;
    56 };
    57 
    58 // Represents a property descriptor for a structure field.
    59 class Descriptor : public Object
    60 {
    61     // The name of the field.
    62     Barriered<String> name_;
    63 
    64     // The type of the field.
    65     Barriered<Type> type_;
    66 
    67     // The offset into an object at which this field can be accessed.
    68     unsigned offset_;
    69 
    70   public:
    71     static Descriptor *New(Zone *zone, Handle<String> name, Handle<Type> type,
    72                            unsigned offset);
    73 
    74     String *name() {
    75         return name_;
    76     }
    77     Type *type() {
    78         return type_;
    79     }
    80     unsigned offset() {
    81         return offset_;
    82     }
    83 
    84     // Begin GC descriptor.
    85     static inline size_t offsetOfName() {
    86         return OFFSETOF(Descriptor, name_);
    87     }
    88     static inline size_t offsetOfType() {
    89         return OFFSETOF(Descriptor, type_);
    90     }
    91     // End GC descriptor.
    92     
    93     static inline Descriptor *cast(Object *obj) {
    94         assert(!obj || obj->is(MapKind_Descriptor));
    95         return reinterpret_cast<Descriptor *>(obj);
    96     }
    97 };
    98 
    99 }
   100 
   101 #endif // _include_jc_structure_h_
   102