Osmium  0.1
include/osmium/javascript/template.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_JAVASCRIPT_TEMPLATE_HPP
00002 #define OSMIUM_JAVASCRIPT_TEMPLATE_HPP
00003 
00004 /*
00005 
00006 Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).
00007 
00008 This file is part of Osmium (https://github.com/joto/osmium).
00009 
00010 Osmium is free software: you can redistribute it and/or modify it under the
00011 terms of the GNU Lesser General Public License or (at your option) the GNU
00012 General Public License as published by the Free Software Foundation, either
00013 version 3 of the Licenses, or (at your option) any later version.
00014 
00015 Osmium is distributed in the hope that it will be useful, but WITHOUT ANY
00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00017 PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU
00018 General Public License for more details.
00019 
00020 You should have received a copy of the Licenses along with Osmium. If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #include <v8.h>
00026 
00027 namespace Osmium {
00028 
00032     namespace Javascript {
00033 
00044         class Template {
00045 
00046         public:
00047 
00048             template<class T>
00049             static T& get() {
00050                 static T t;
00051                 return t;
00052             }
00053 
00058             v8::Local<v8::Object> create_instance(void* wrapped) {
00059                 v8::Local<v8::Object> instance = js_template->NewInstance();
00060                 instance->SetInternalField(0, v8::External::New(wrapped));
00061                 return instance;
00062             }
00063 
00064             template <class TWrapped>
00065             v8::Persistent<v8::Object> create_persistent_instance(TWrapped* wrapped) {
00066                 v8::Persistent<v8::Object> instance = v8::Persistent<v8::Object>::New(create_instance(wrapped));
00067                 instance.MakeWeak(wrapped, Osmium::Javascript::Template::free_instance<TWrapped>);
00068                 return instance;
00069             }
00070 
00071             template <class TWrapped>
00072             static void free_instance(v8::Persistent<v8::Value> instance, void* obj) {
00073                 instance.Dispose();
00074                 delete static_cast<TWrapped*>(obj);
00075             }
00076 
00080             v8::Handle<v8::Value> js_undefined(const v8::Arguments& /*args*/) {
00081                 return v8::Undefined();
00082             }
00083 
00084             /*
00085                 These magic helper function are used to connect Javascript
00086                 methods to C++ methods. They are given to the SetAccessor,
00087                 SetIndexedPropertyHandler and SetNamedPropertyHandler
00088                 functions of a v8::ObjectTemplate object, respectively.
00089 
00090                 The first template argument is the class of the object we
00091                 want to access, for instance Osmium::OSM::Node.
00092 
00093                 The second template argument is the member function on the
00094                 object that we want to call when this function is called
00095                 from Javascript.
00096 
00097             */
00098             template<class TObject, v8::Handle<v8::Value> (TObject::*func)() const>
00099             static v8::Handle<v8::Value> accessor_getter(v8::Local<v8::String>, const v8::AccessorInfo &info) {
00100                 return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))();
00101             }
00102 
00103             template<class TObject, v8::Handle<v8::Value> (TObject::*func)(v8::Local<v8::String>) const>
00104             static v8::Handle<v8::Value> named_property_getter(v8::Local<v8::String> property, const v8::AccessorInfo &info) {
00105                 return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))(property);
00106             }
00107 
00108             template<class TObject, v8::Handle<v8::Value> (TObject::*func)(uint32_t) const>
00109             static v8::Handle<v8::Value> indexed_property_getter(uint32_t index, const v8::AccessorInfo &info) {
00110                 return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))(index);
00111             }
00112 
00113             template<class TObject, v8::Handle<v8::Value> (TObject::*func)(uint32_t)>
00114             static v8::Handle<v8::Value> indexed_property_getter(uint32_t index, const v8::AccessorInfo &info) {
00115                 return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))(index);
00116             }
00117 
00118             template<class TObject, v8::Handle<v8::Array> (TObject::*func)() const>
00119             static v8::Handle<v8::Array> property_enumerator(const v8::AccessorInfo &info) {
00120                 return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0))->Value()) )->*(func))();
00121             }
00122 
00123             template<class TObject, v8::Handle<v8::Value> (TObject::*func)(const v8::Arguments&)>
00124             static v8::Handle<v8::Value> function_template(const v8::Arguments& args) {
00125                 return (( reinterpret_cast<TObject*>(v8::Local<v8::External>::Cast(args.Holder()->GetInternalField(0))->Value()) )->*(func))(args);
00126             }
00127 
00128         protected:
00129 
00130             v8::Persistent<v8::ObjectTemplate> js_template;
00131 
00135             Template(int field_count=1) {
00136                 js_template = v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New());
00137                 js_template->SetInternalFieldCount(field_count);
00138             }
00139 
00140             ~Template() {
00141                 js_template.Dispose();
00142             }
00143 
00144         private:
00145 
00146             // copy constructor and assignment operator are private and can't be used
00147             Template(const Template&);
00148             Template& operator=(const Template&);
00149 
00150         }; // class Template
00151 
00152     } // namespace Javascript
00153 
00154 } // namespace Osmium
00155 
00156 #endif // OSMIUM_JAVASCRIPT_TEMPLATE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines