1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/NativeTable.h Sat Nov 17 18:41:01 2012 -0800
1.3 @@ -0,0 +1,89 @@
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_sp2_native_table_h_
1.23 +#define _include_sp2_native_table_h_
1.24 +
1.25 +#include "AllocatorPolicies.h"
1.26 +#include "Strings.h"
1.27 +#include "HashTable.h"
1.28 +#include "Vector.h"
1.29 +#include "VirtualObjectVisitor.h"
1.30 +
1.31 +namespace ke {
1.32 +
1.33 +class NativeTable
1.34 +{
1.35 + public:
1.36 + NativeTable(Zone *zone);
1.37 + ~NativeTable();
1.38 +
1.39 + private:
1.40 + struct NativeInfo {
1.41 + Unbarriered<String> name;
1.42 + void *address;
1.43 + size_t index;
1.44 +
1.45 + NativeInfo(String *name, void *address, size_t index)
1.46 + : address(address),
1.47 + index(index)
1.48 + {
1.49 + this->name = name;
1.50 + }
1.51 + };
1.52 +
1.53 + struct Policy
1.54 + {
1.55 + typedef NativeInfo * Payload;
1.56 +
1.57 + static uint32 hash(const CharsAndLength &key) {
1.58 + return HashCharSequence(key.str(), key.length());
1.59 + }
1.60 +
1.61 + static bool matches(const CharsAndLength &key, NativeInfo *info) {
1.62 + if (info->name->length() != key.length())
1.63 + return false;
1.64 + return memcmp(info->name->chars(), key.str(), key.length() * sizeof(char)) == 0;
1.65 + }
1.66 + };
1.67 +
1.68 + typedef HashTable<Policy, ZoneAllocatorPolicy> NativeMap;
1.69 +
1.70 + public:
1.71 + bool initialize();
1.72 +
1.73 + bool bind(const char *name, void *address);
1.74 + bool add(Handle<String> name, size_t *indexp);
1.75 + String *name(size_t index) const {
1.76 + return natives_[index]->name;
1.77 + }
1.78 + void *address(size_t index) const {
1.79 + return natives_[index]->address;
1.80 + }
1.81 +
1.82 + void accept(VirtualObjectVisitor *visitor);
1.83 +
1.84 + private:
1.85 + Zone *zone_;
1.86 + NativeMap nativeMap_;
1.87 + Vector<NativeInfo *> natives_;
1.88 +};
1.89 +
1.90 +}
1.91 +
1.92 +#endif // _include_sp2_native_table_h_