/usr/include/osmium/javascript/template.hpp is in libosmium-dev 0.0~20111213-g7f3500a-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | #ifndef OSMIUM_JAVASCRIPT_TEMPLATE_HPP
#define OSMIUM_JAVASCRIPT_TEMPLATE_HPP
/*
Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).
This file is part of Osmium (https://github.com/joto/osmium).
Osmium is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License or (at your option) the GNU
General Public License as published by the Free Software Foundation, either
version 3 of the Licenses, or (at your option) any later version.
Osmium is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU
General Public License for more details.
You should have received a copy of the Licenses along with Osmium. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <v8.h>
namespace Osmium {
/**
* @brief %Javascript support for %Osmium.
*/
namespace Javascript {
/**
* Base class for all Javascript template classes. Javascript
* template classes describe templates from which Javascript
* objects can be created, so for every C++ class that should
* be accessible from Javascript there is a corresponding
* template class.
*
* Note that Javascript templates have nothing to do with C++
* templates.
*/
class Template {
public:
template<class T>
static T& get() {
static T t;
return t;
}
/**
* Create a Javascript object instance from the Javascript template
* wrapping a C++ object.
*/
v8::Local<v8::Object> create_instance(void* wrapped) {
v8::Local<v8::Object> instance = js_template->NewInstance();
instance->SetInternalField(0, v8::External::New(wrapped));
return instance;
}
template <class TWrapped>
v8::Persistent<v8::Object> create_persistent_instance(TWrapped* wrapped) {
v8::Persistent<v8::Object> instance = v8::Persistent<v8::Object>::New(create_instance(wrapped));
instance.MakeWeak(wrapped, Osmium::Javascript::Template::free_instance<TWrapped>);
return instance;
}
template <class TWrapped>
static void free_instance(v8::Persistent<v8::Value> instance, void* obj) {
instance.Dispose();
delete static_cast<TWrapped*>(obj);
}
/**
* Function that always returns undefined.
*/
v8::Handle<v8::Value> js_undefined(const v8::Arguments& /*args*/) {
return v8::Undefined();
}
/*
These magic helper function are used to connect Javascript
methods to C++ methods. They are given to the SetAccessor,
SetIndexedPropertyHandler and SetNamedPropertyHandler
functions of a v8::ObjectTemplate object, respectively.
The first template argument is the class of the object we
want to access, for instance Osmium::OSM::Node.
The second template argument is the member function on the
object that we want to call when this function is called
from Javascript.
*/
template<class TObject, v8::Handle<v8::Value> (TObject::*func)() const>
static v8::Handle<v8::Value> accessor_getter(v8::Local<v8::String>, const v8::AccessorInfo &info) {
return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))();
}
template<class TObject, v8::Handle<v8::Value> (TObject::*func)(v8::Local<v8::String>) const>
static v8::Handle<v8::Value> named_property_getter(v8::Local<v8::String> property, const v8::AccessorInfo &info) {
return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))(property);
}
template<class TObject, v8::Handle<v8::Value> (TObject::*func)(uint32_t) const>
static v8::Handle<v8::Value> indexed_property_getter(uint32_t index, const v8::AccessorInfo &info) {
return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))(index);
}
template<class TObject, v8::Handle<v8::Value> (TObject::*func)(uint32_t)>
static v8::Handle<v8::Value> indexed_property_getter(uint32_t index, const v8::AccessorInfo &info) {
return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))(index);
}
template<class TObject, v8::Handle<v8::Array> (TObject::*func)() const>
static v8::Handle<v8::Array> property_enumerator(const v8::AccessorInfo &info) {
return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))();
}
template<class TObject, v8::Handle<v8::Value> (TObject::*func)(const v8::Arguments&)>
static v8::Handle<v8::Value> function_template(const v8::Arguments& args) {
return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(args.Holder()->GetInternalField(0))->Value()) )->*(func))(args);
}
protected:
v8::Persistent<v8::ObjectTemplate> js_template;
/**
* Constructor.
*/
Template(int field_count=1) {
js_template = v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New());
js_template->SetInternalFieldCount(field_count);
}
~Template() {
js_template.Dispose();
}
private:
// copy constructor and assignment operator are private and can't be used
Template(const Template&);
Template& operator=(const Template&);
}; // class Template
} // namespace Javascript
} // namespace Osmium
#endif // OSMIUM_JAVASCRIPT_TEMPLATE_HPP
|