1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/include/keima.h Sun Jul 08 01:54:08 2012 -0700
1.3 @@ -0,0 +1,206 @@
1.4 +/*
1.5 + * Copyright (C) 2004-2012 David Anderson
1.6 + *
1.7 + * This file is part of SourcePawn.
1.8 + *
1.9 + * SourcePawn is free software: you can redistribute it and/or modify it under
1.10 + * the terms of the GNU General Public License as published by the Free
1.11 + * Software Foundation, either version 3 of the License, or (at your option)
1.12 + * any later version.
1.13 + *
1.14 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along with
1.19 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.20 + */
1.21 +#ifndef _include_sourcepawn3_api_h_
1.22 +#define _include_sourcepawn3_api_h_
1.23 +
1.24 +#include <stddef.h>
1.25 +
1.26 +namespace ke {
1.27 +
1.28 +class Zone;
1.29 +class Object;
1.30 +class HandleScope;
1.31 +class HeapRoot;
1.32 +
1.33 +}
1.34 +
1.35 +namespace Ke {
1.36 +
1.37 +/*
1.38 + * A Handle is a safe pointer that holds an object reference, such that it
1.39 + * can be scanned and potentially moved by the garbage collector.
1.40 + */
1.41 +template <typename T>
1.42 +class Handle
1.43 +{
1.44 + protected:
1.45 + template <typename S> void typeCheck() {
1.46 + while (false) {
1.47 + T *v1 = NULL;
1.48 + S *v2 = NULL;
1.49 + v1 = v2;
1.50 + (void)v1;
1.51 + }
1.52 + }
1.53 +
1.54 + public:
1.55 + Handle()
1.56 + : obj_(NULL)
1.57 + {
1.58 + }
1.59 +
1.60 + template <typename S> Handle(Handle<S> other)
1.61 + : obj_(reinterpret_cast<S *>(*other))
1.62 + {
1.63 + typeCheck<S>();
1.64 + }
1.65 +
1.66 + explicit Handle(T *obj)
1.67 + : obj_(obj)
1.68 + {
1.69 + }
1.70 +
1.71 + T *operator ->() const {
1.72 + return obj_;
1.73 + }
1.74 + T *operator *() const {
1.75 + return obj_;
1.76 + }
1.77 + operator bool () const {
1.78 + return !!obj_;
1.79 + }
1.80 +
1.81 + private:
1.82 + T *obj_;
1.83 +};
1.84 +
1.85 +/*
1.86 + * A local variable that roots an object. Locals are owned by the nearest
1.87 + * HandleScope, and as soon as that HandleScope dies, all its Locals are
1.88 + * no longer available.
1.89 + */
1.90 +template <typename T>
1.91 +class Local : public Handle<T>
1.92 +{
1.93 + public:
1.94 + Local()
1.95 + : Handle<T>()
1.96 + {
1.97 + }
1.98 +
1.99 + template <typename S> Local(Local<S> other)
1.100 + : Handle<T>(reinterpret_cast<T *>(*other))
1.101 + {
1.102 + typeCheck<S>();
1.103 + }
1.104 +
1.105 + template <typename S> Local(S *other)
1.106 + : Handle<T>(other)
1.107 + {
1.108 + }
1.109 +};
1.110 +
1.111 +/*
1.112 + * A non-local variable that outlives HandleScopes. A Root is reference-
1.113 + * counted. When its reference count reaches 0, the Root is removed.
1.114 + */
1.115 +template <typename T>
1.116 +class Root : public Handle<T>
1.117 +{
1.118 + public:
1.119 + Root()
1.120 + : Handle<T>()
1.121 + {
1.122 + }
1.123 +
1.124 + template <typename S> Root(const Root<S> &other)
1.125 + : Handle<T>(reinterpret_cast<T *>(*other))
1.126 + {
1.127 + typeCheck<S>();
1.128 + incref();
1.129 + }
1.130 +
1.131 + explicit Root(ke::HeapRoot **obj)
1.132 + : Handle<T>(reinterpret_cast<T *>(obj))
1.133 + {
1.134 + incref();
1.135 + }
1.136 +
1.137 + static Root<T> New(Handle<T> other);
1.138 +
1.139 + ~Root() {
1.140 + decref();
1.141 + }
1.142 +
1.143 + template <typename S>
1.144 + Root<T> &operator =(Root<S> other) {
1.145 + decref();
1.146 + new (this) Root<T>(other);
1.147 + return *this;
1.148 + }
1.149 +
1.150 + private:
1.151 + void incref();
1.152 + void decref();
1.153 +};
1.154 +
1.155 +/*
1.156 + * A HandleScope is where Local roots are allcoated. When the HandleScope
1.157 + * is destroyed, all Handles created under it are also destroyed. HandleScopes
1.158 + * may only be allocated on the stack, in LIFO order.
1.159 + */
1.160 +class HandleScope
1.161 +{
1.162 + protected:
1.163 + ke::Zone *zone_;
1.164 + ke::Object **position_;
1.165 + HandleScope *prev_;
1.166 +
1.167 + HandleScope(ke::Zone *zone, ke::Object **position, HandleScope *prev)
1.168 + : zone_(zone),
1.169 + position_(position),
1.170 + prev_(prev)
1.171 + {
1.172 + }
1.173 +
1.174 + public:
1.175 + HandleScope();
1.176 + ~HandleScope();
1.177 +
1.178 + static ke::Object **CreateHandle(ke::Object *obj);
1.179 +
1.180 + private:
1.181 + // Disallow silly stuff.
1.182 + HandleScope(const HandleScope &);
1.183 + void operator =(const HandleScope &);
1.184 + void *operator new (size_t size);
1.185 + void operator delete(void *, size_t);
1.186 +};
1.187 +
1.188 +class Module;
1.189 +
1.190 +/*
1.191 + * Initializes the Keima engine. This must be called exactly once before any
1.192 + * other Keima functions are called. After this, it is safe to use Keima
1.193 + * on the calling thread.
1.194 + */
1.195 +extern bool Initialize();
1.196 +
1.197 +/*
1.198 + * Shuts down the Keima engine and frees data structures on all threads.
1.199 + */
1.200 +extern void Shutdown();
1.201 +
1.202 +/*
1.203 + * Compiles a file and returns a Module.
1.204 + */
1.205 +extern Local<Module> Compile(const char *path);
1.206 +
1.207 +}
1.208 +
1.209 +#endif // _include_sourcepawn3_api_h_