|
| 1 | +#if ! defined(V8_CONVERT_NATIVE_JS_MAPPER_HPP_INCLUDED) |
| 2 | +#define V8_CONVERT_NATIVE_JS_MAPPER_HPP_INCLUDED |
| 3 | + |
| 4 | +#include "detail/convert_core.hpp" |
| 5 | +namespace cvv8 { |
| 6 | + /** |
| 7 | + A helper class to assist in the "two-way-binding" of |
| 8 | + natives to JS objects. This class holds native-to-JS |
| 9 | + binding information. |
| 10 | + |
| 11 | + In the general case, a native-to-JS conversion is only |
| 12 | + needed at the framework-level if bound/converted |
| 13 | + functions/methods will _return_ bound native |
| 14 | + pointers/references. If they only return "core" types (numbers |
| 15 | + and strings, basically), or explicitly return v8-supported |
| 16 | + types (e.g. v8::Handle<v8::Value>) then no native-to-JS |
| 17 | + conversion is typically needed. |
| 18 | +
|
| 19 | + Known limitations: |
| 20 | +
|
| 21 | + This type does not fully support subclass conversions. |
| 22 | + e.g. the following function binding: |
| 23 | +
|
| 24 | + @code |
| 25 | + virtual MyType * (MyType::*)(); |
| 26 | + @endcode |
| 27 | +
|
| 28 | + _should_ be able to return a MySubType from derived implementations |
| 29 | + but it currently cannot. Handling this requires that a parent class |
| 30 | + be told each of its subclasses, and that we add internal handlers |
| 31 | + which try lookups on those classes if a conversion to MyType fails. |
| 32 | +
|
| 33 | + Reminder to self: the v8::juice tree has an example of that which we |
| 34 | + can probably plunder. |
| 35 | + */ |
| 36 | + template <typename T> |
| 37 | + struct NativeToJSMap |
| 38 | + { |
| 39 | + private: |
| 40 | + typedef TypeInfo<T> TI; |
| 41 | + typedef typename TI::Type Type; |
| 42 | + /** |
| 43 | + The native type to bind to. |
| 44 | + */ |
| 45 | + typedef typename TI::NativeHandle NativeHandle; |
| 46 | + /** The type for holding the JS 'this' object. */ |
| 47 | + typedef v8::Persistent<v8::Object> JSObjHandle; |
| 48 | + //typedef v8::Handle<v8::Object> JSObjHandle; // Hmmm. |
| 49 | + typedef std::pair<NativeHandle,JSObjHandle> ObjBindT; |
| 50 | + typedef std::map<void const *, ObjBindT> OneOfUsT; |
| 51 | + /** Maps (void const *) to ObjBindT. |
| 52 | + |
| 53 | + Reminder to self: we might need to make this map a static |
| 54 | + non-function member to work around linking problems (at |
| 55 | + least on Windows) which lead to multiple instances of |
| 56 | + the returned map being created when the types being |
| 57 | + bound are loaded from multiple DLLs. The out-of-class |
| 58 | + initialization of the member is going to require a really |
| 59 | + ugly set of template parameters, though. |
| 60 | + */ |
| 61 | + static OneOfUsT & Map() |
| 62 | + { |
| 63 | + static OneOfUsT bob; |
| 64 | + return bob; |
| 65 | + } |
| 66 | + public: |
| 67 | + /** Maps obj as a lookup key for jself. Returns false if !obj, |
| 68 | + else true. */ |
| 69 | + static bool Insert( JSObjHandle const & jself, |
| 70 | + NativeHandle obj ) |
| 71 | + { |
| 72 | + return obj |
| 73 | + ? (Map().insert( std::make_pair( obj, std::make_pair( obj, jself ) ) ),true) |
| 74 | + : 0; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + Removes any mapping of the given key. Returns the |
| 79 | + mapped native, or 0 if none is found. |
| 80 | + */ |
| 81 | + static NativeHandle Remove( void const * key ) |
| 82 | + { |
| 83 | + typedef typename OneOfUsT::iterator Iterator; |
| 84 | + OneOfUsT & map( Map() ); |
| 85 | + Iterator it = map.find( key ); |
| 86 | + if( map.end() == it ) |
| 87 | + { |
| 88 | + return 0; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + NativeHandle victim = (*it).second.first; |
| 93 | + map.erase(it); |
| 94 | + return victim; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + Returns the native associated (via Insert()) |
| 100 | + with key, or 0 if none is found. |
| 101 | + */ |
| 102 | + static NativeHandle GetNative( void const * key ) |
| 103 | + { |
| 104 | + if( ! key ) return 0; |
| 105 | + else |
| 106 | + { |
| 107 | + typename OneOfUsT::iterator it = Map().find(key); |
| 108 | + return (Map().end() == it) |
| 109 | + ? 0 |
| 110 | + : (*it).second.first; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + Returns the JS object associated with key, or |
| 116 | + an empty handle if !key or no object is found. |
| 117 | + */ |
| 118 | + static v8::Handle<v8::Object> GetJSObject( void const * key ) |
| 119 | + { |
| 120 | + if( ! key ) return v8::Handle<v8::Object>(); |
| 121 | + typename OneOfUsT::const_iterator it = Map().find(key); |
| 122 | + if( Map().end() == it ) return v8::Handle<v8::Object>(); |
| 123 | + else return (*it).second.second; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + A base NativeToJS<T> implementation for classes which use NativeToJSMap<T> |
| 128 | + to hold their native-to-JS bindings. To be used like this: |
| 129 | + |
| 130 | + @code |
| 131 | + // must be in the v8::convert namespace! |
| 132 | + template <> |
| 133 | + struct NativeToJS<MyType> : NativeToJSMap<MyType>::NativeToJSImpl {}; |
| 134 | + @endcode |
| 135 | + */ |
| 136 | + struct NativeToJSImpl |
| 137 | + { |
| 138 | + v8::Handle<v8::Value> operator()( Type const * n ) const |
| 139 | + { |
| 140 | + typedef NativeToJSMap<T> BM; |
| 141 | + v8::Handle<v8::Value> const & rc( BM::GetJSObject(n) ); |
| 142 | + if( rc.IsEmpty() ) return v8::Null(); |
| 143 | + else return rc; |
| 144 | + } |
| 145 | + v8::Handle<v8::Value> operator()( Type const & n ) const |
| 146 | + { |
| 147 | + return this->operator()( &n ); |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | +#if 0 |
| 152 | + //! Experimental |
| 153 | + template <typename ParentType> |
| 154 | + struct NativeToJSImpl_Subclass |
| 155 | + { |
| 156 | + v8::Handle<v8::Value> operator()( Type const * n ) const |
| 157 | + { |
| 158 | + typedef NativeToJSMap<T> BM; |
| 159 | + v8::Handle<v8::Value> const & rc( BM::GetJSObject(n) ); |
| 160 | + if( rc.IsEmpty() ) |
| 161 | + { |
| 162 | + typedef typename NativeToJSMap<ParentType>::NativeToJSImpl PI; |
| 163 | + return PI()(n); |
| 164 | +#if 0 |
| 165 | + typedef typename TypeInfo<ParentType>::NativeHandle PH; |
| 166 | + rc = CastToJS<ParentType>(n); |
| 167 | + if( rc.IsEmpty() ) return v8::Null(); |
| 168 | + else return rc; |
| 169 | +#endif |
| 170 | + } |
| 171 | + else return rc; |
| 172 | + } |
| 173 | + v8::Handle<v8::Value> operator()( Type const & n ) const |
| 174 | + { |
| 175 | + return this->operator()( &n ); |
| 176 | + } |
| 177 | + }; |
| 178 | +#endif |
| 179 | + }; |
| 180 | + |
| 181 | +} // namespaces |
| 182 | + |
| 183 | +#endif /* include guard */ |
0 commit comments