v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
casts_eastl.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "casts.hpp"
4 
5 // Tell EASTL not to redefine initializer_list
6 #define EA_HAVE_CPP11_INITIALIZER_LIST 1
7 
8 #include <EASTL/vector_map.h>
9 #include <EASTL/vector_multimap.h>
10 #include <EASTL/utility.h>
11 #include <EASTL/string.h>
12 #include <EASTL/fixed_string.h>
13 #include <EASTL/vector_set.h>
14 #include <EASTL/vector.h>
15 
16 namespace v8toolkit {
17 
18 
19 
20 template<class FirstT, class SecondT>
21 struct v8toolkit::CastToNative<eastl::pair<FirstT, SecondT>>{
22  eastl::pair<FirstT, SecondT> operator()(v8::Isolate * isolate, v8::Local<v8::Value> value) const {
24  pair_type_helper<eastl::pair, FirstT, SecondT>(isolate, value);
25  }
26 };
27 
28 
29 
30 
31 // EASTL VECTOR_MAP
32 template<class Key, class Value, class... Args>
33 struct CastToNative<eastl::vector_map<Key, Value, Args...>> {
34  eastl::vector_map<Key, Value, Args...> operator()(v8::Isolate *isolate, v8::Local <v8::Value> value) const {
36  return map_type_helper<eastl::vector_map, Key, Value, Args...>(isolate, value);
37  }
38 };
39 
40 
41 // EASTL VECTOR
42 template<class T, class... Args>
43 struct CastToNative<eastl::vector<T, Args...>> {
44  eastl::vector<T, Args...> operator()(v8::Isolate *isolate, v8::Local <v8::Value> value) const {
46  return vector_type_helper<eastl::vector, T, Args...>(isolate, value);
47  }
48 };
49 
50 
51 
52 // EASTL VECTOR_SET
53 template<class T, class... Args>
54 struct CastToNative<eastl::vector_set<T, Args...>> {
55  eastl::vector_set<T, Args...> operator()(v8::Isolate *isolate, v8::Local <v8::Value> value) const {
57  auto vector = vector_type_helper<eastl::vector, T>(isolate, value);
58  eastl::vector_set<T, Args...> set;
59  for(auto & i : vector) {
60  set.emplace(std::move(i));
61  }
62  return set;
63  }
64 };
65 
66 
67 
68 
69 
70 template<class Key, class Value, class... Args>
71 struct CastToNative<eastl::vector_multimap<Key, Value, Args...>> {
72  eastl::vector_multimap<Key, Value, Args...> operator()(v8::Isolate *isolate, v8::Local <v8::Value> value) const {
74  return multimap_type_helper<eastl::vector_multimap, Key, Value, Args...>(isolate, value);
75  }
76 };
77 
78 CAST_TO_NATIVE(eastl::string, {HANDLE_FUNCTION_VALUES; return eastl::string(*v8::String::Utf8Value(value)); });
79 
80 CAST_TO_JS(eastl::string, {return v8::String::NewFromUtf8(isolate, value.c_str());});
81 
82 
83 
84 template<class CharType, int Length, bool Overflow, class Allocator>
85 struct CastToJS<eastl::fixed_string<CharType, Length, Overflow, Allocator>> {
86  v8::Local<v8::Value> operator()(v8::Isolate * isolate, eastl::fixed_string<CharType, Length, Overflow, Allocator> const & value) const {
87  return v8::String::NewFromUtf8(isolate, value.c_str(), v8::String::kNormalString, value.length());
88  }
89 };
90 
91 template<class CharType, int Length, bool Overflow, class Allocator>
92 struct CastToNative<eastl::fixed_string<CharType, Length, Overflow, Allocator>> {
93  eastl::fixed_string<CharType, Length, Overflow, Allocator> operator()(v8::Isolate * isolate, v8::Local<v8::Value> value) const {
95  return eastl::fixed_string<CharType, Length, Overflow, Allocator>(*v8::String::Utf8Value(value));
96  }
97 };
98 
99 
100 // CastToJS<eastl::vector<>>
101 template<class T, class... Rest>
102 struct CastToJS<eastl::vector<T, Rest...>> {
103  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector<T, Rest...> const & vector);
104  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector<T, Rest...> && vector) {
105  return this->operator()(isolate, vector);
106  }
107 };
108 
109 
110 template<class T, class... Rest>
111 v8::Local<v8::Value>CastToJS<eastl::vector<T, Rest...>>::operator()(v8::Isolate *isolate, eastl::vector<T, Rest...> const & vector) {
112  return cast_to_js_vector_helper<eastl::vector, T, Rest...>(isolate, vector);
113 }
114 
115 
116 template<class A, class B, class... Rest>
117 struct CastToJS<eastl::vector_multimap<A, B, Rest...>> {
118  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector_multimap<A, B, Rest...> const & multimap) {
119  return casttojs_multimaplike(isolate, multimap);
120  }
121 };
122 
123 
124 template<class A, class B, class... Rest>
125 struct CastToJS<eastl::vector_map<A, B, Rest...>> {
126  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector_map<A, B, Rest...> const & map) {
127  return cast_to_js_map_helper<eastl::vector_map, A, B, int&, Rest...>(isolate, map);
128  }
129  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector_map<A, B, Rest...> && map) {
130  return cast_to_js_map_helper<eastl::vector_map, A, B, int&&, Rest...>(isolate, map);
131  }
132 
133 };
134 
135 
136 
137 template<class T, class... Rest>
138 struct CastToJS<eastl::vector_set<T, Rest...>> {
139  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector_set<T, Rest...> const & set) {
140  return cast_to_js_vector_helper<eastl::vector_set, T&, Rest...>(isolate, set);
141  }
142  v8::Local<v8::Value> operator()(v8::Isolate *isolate, eastl::vector_set<T, Rest...> && set) {
143  return cast_to_js_vector_helper<eastl::vector_set, T&&, Rest...>(isolate, set);
144  }
145 
146 };
147 
148 
149 } // namespace v8toolkit
150 
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::fixed_string< CharType, Length, Overflow, Allocator > const &value) const
Definition: casts_eastl.hpp:86
eastl::vector_map< Key, Value, Args... > operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts_eastl.hpp:34
Definition: sample.cpp:17
eastl::pair< FirstT, SecondT > operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts_eastl.hpp:22
eastl::vector_set< T, Args... > operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts_eastl.hpp:55
::std::string string
Definition: gtest-port.h:1097
ContainerTemplate< Key, Value, Rest... > multimap_type_helper(v8::Isolate *isolate, v8::Local< v8::Value > value)
Definition: casts.hpp:463
CAST_TO_NATIVE(bool,{HANDLE_FUNCTION_VALUES;return static_cast< bool >(value->ToBoolean() ->Value());})
auto vector_type_helper(v8::Isolate *isolate, v8::Local< v8::Value > value) -> VectorTemplate< std::remove_reference_t< std::result_of_t< CastToNative< T >(v8::Isolate *, v8::Local< v8::Value >)>>, Rest... >
Definition: casts.hpp:306
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::vector_map< A, B, Rest... > &&map)
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
CAST_TO_JS(bool,{return v8::Boolean::New(isolate, value);})
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::vector_set< T, Rest... > const &set)
eastl::fixed_string< CharType, Length, Overflow, Allocator > operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts_eastl.hpp:93
eastl::vector< T, Args... > operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts_eastl.hpp:44
bool Value(const T &value, M matcher)
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::vector_multimap< A, B, Rest... > const &multimap)
void operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts.hpp:108
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::vector< T, Rest... > &&vector)
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::vector_map< A, B, Rest... > const &map)
v8::Local< v8::Object > casttojs_multimaplike(v8::Isolate *isolate, MultiMapLike< A, B, Rest... > const &map)
Definition: casts.hpp:846
v8::Local< v8::Value > cast_to_js_map_helper(v8::Isolate *isolate, MapTemplate< KeyType, ValueType, Rest... > const &map)
Definition: casts.hpp:645
v8::Local< v8::Value > operator()(v8::Isolate *isolate, eastl::vector_set< T, Rest... > &&set)
Matcher< T > A()
ContainerTemplate< Key, Value, Rest... > map_type_helper(v8::Isolate *isolate, v8::Local< v8::Value > value)
Definition: casts.hpp:433
internal::KeyMatcher< M > Key(M inner_matcher)
v8::Local< v8::Value > cast_to_js_vector_helper(v8::Isolate *isolate, VectorTemplate< std::remove_reference_t< ValueType >, Rest... > const &vector)
Definition: casts.hpp:665
eastl::vector_multimap< Key, Value, Args... > operator()(v8::Isolate *isolate, v8::Local< v8::Value > value) const
Definition: casts_eastl.hpp:72
const T & move(const T &t)
Definition: gtest-port.h:1317
#define HANDLE_FUNCTION_VALUES
Definition: casts.hpp:69