src/NativeTable.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 196 9904633b1dfc
permissions -rw-r--r--
Normalise line endings.
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of SourcePawn.
asherkin@263
     6
 *
asherkin@263
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#ifndef _include_sp2_native_table_h_
asherkin@263
    20
#define _include_sp2_native_table_h_
asherkin@263
    21
asherkin@263
    22
#include <string.h>
asherkin@263
    23
#include "AllocatorPolicies.h"
asherkin@263
    24
#include "Strings.h"
asherkin@263
    25
#include "HashTable.h"
asherkin@263
    26
#include "Vector.h"
asherkin@263
    27
#include "VirtualObjectVisitor.h"
asherkin@263
    28
asherkin@263
    29
namespace ke {
asherkin@263
    30
asherkin@263
    31
class NativeTable
asherkin@263
    32
{
asherkin@263
    33
  public:
asherkin@263
    34
    NativeTable(Zone *zone);
asherkin@263
    35
    ~NativeTable();
asherkin@263
    36
asherkin@263
    37
  private:
asherkin@263
    38
    struct NativeInfo {
asherkin@263
    39
        Unbarriered<String> name;
asherkin@263
    40
        void *address;
asherkin@263
    41
        size_t index;
asherkin@263
    42
asherkin@263
    43
        NativeInfo(String *name, void *address, size_t index)
asherkin@263
    44
          : address(address),
asherkin@263
    45
            index(index)
asherkin@263
    46
        {
asherkin@263
    47
            this->name = name;
asherkin@263
    48
        }
asherkin@263
    49
    };
asherkin@263
    50
asherkin@263
    51
    struct Policy
asherkin@263
    52
    {
asherkin@263
    53
        typedef NativeInfo * Payload;
asherkin@263
    54
asherkin@263
    55
        static uint32 hash(const CharsAndLength &key) {
asherkin@263
    56
            return HashCharSequence(key.str(), key.length());
asherkin@263
    57
        }
asherkin@263
    58
        
asherkin@263
    59
        static bool matches(const CharsAndLength &key, NativeInfo *info) {
asherkin@263
    60
            if (info->name->length() != key.length())
asherkin@263
    61
                return false;
asherkin@263
    62
            return memcmp(info->name->chars(), key.str(), key.length() * sizeof(char)) == 0;
asherkin@263
    63
        }
asherkin@263
    64
    };
asherkin@263
    65
asherkin@263
    66
    typedef HashTable<Policy, ZoneAllocatorPolicy> NativeMap;
asherkin@263
    67
asherkin@263
    68
  public:
asherkin@263
    69
    bool initialize();
asherkin@263
    70
asherkin@263
    71
    bool bind(const char *name, void *address);
asherkin@263
    72
    bool add(Handle<String> name, size_t *indexp);
asherkin@263
    73
    String *name(size_t index) const {
asherkin@263
    74
        return natives_[index]->name;
asherkin@263
    75
    }
asherkin@263
    76
    void *address(size_t index) const {
asherkin@263
    77
        return natives_[index]->address;
asherkin@263
    78
    }
asherkin@263
    79
asherkin@263
    80
    void accept(VirtualObjectVisitor *visitor);
asherkin@263
    81
asherkin@263
    82
  private:
asherkin@263
    83
    Zone *zone_;
asherkin@263
    84
    NativeMap nativeMap_;
asherkin@263
    85
    Vector<NativeInfo *> natives_;
asherkin@263
    86
};
asherkin@263
    87
asherkin@263
    88
}
asherkin@263
    89
asherkin@263
    90
#endif // _include_sp2_native_table_h_