src/terralib/binding/v8/common/Utils.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Utils.h
22 
23  \brief Utility functions for dealing with JNI.
24  */
25 
26 #ifndef __TERRALIB_BINDING_V8_COMMON_INTERNAL_UTILS_H
27 #define __TERRALIB_BINDING_V8_COMMON_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "JsObject.h"
32 
33 // STL
34 #include <cassert>
35 #include <map>
36 #include <string>
37 
38 // Google V8
39 #include <v8.h>
40 
41 namespace te
42 {
43  namespace v8
44  {
45  namespace common
46  {
47  /*!
48  \brief It reads a file into a v8 string.
49 
50  \param fileName The name of the file to be read.
51 
52  \return A v8 string with the file content.
53 
54  \exception Exception It throws an exception if it can not read the file.
55 
56  \pre A HandleScope must exist before calling this method.
57 
58  \todo Check the implementaion for exceptions in the read loop!
59  */
60  TEV8COMMONEXPORT ::v8::Handle<::v8::String> ReadFile(const std::string& fileName);
61 
62  /*!
63  \brief It caches the object method into the persistent output functor.
64 
65  \param obj The object that must have a method called methodName.
66  \param methodName The name of a method belonging to obj.
67  \param outFtor A persistent function where the reference to the method will me cached.
68 
69  \exception Exception It throws an exception if it can not find the method or something goes wrong.
70 
71  \pre A HandleScope must exist before calling this method.
72  */
73  TEV8COMMONEXPORT void Cache(::v8::Local<::v8::Object>& obj, const std::string& methodName, ::v8::Persistent<::v8::Function>& outFtor);
74 
75  /*!
76  \brief This function will print all the arguments to the standard output.
77 
78  \param args The arguments to be printed.
79 
80  \return An undefinded V8 value just to allow the function to be exported to JavaScript environment!
81  */
82  TEV8COMMONEXPORT ::v8::Handle<::v8::Value> Print(const ::v8::Arguments& args);
83 
84  /*!
85  \brief An utility function that extracts the C++ object from a wrapped object.
86 
87  \param obj The object holding the C++ pointer.
88 
89  \return The C++ pointer casted to the right type.
90 
91  \pre There must be a valid HandleScope before calling this routine.
92  */
93  template<class T> inline T* Unwrap(::v8::Handle<::v8::Object> obj)
94  {
95  ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(obj->GetInternalField(0));
96  void* p = field->Value();
97  JsObject<T>* jsObj = static_cast<JsObject<T>*>(p);
98  return static_cast<T*>(jsObj->m_handle);
99  }
100 
101  /*!
102  \brief An utility function that extracts the C++ object from a wrapped object.
103 
104  \param obj The object holding the C++ pointer.
105 
106  \return The C++ pointer casted to the right type.
107 
108  \pre There must be a valid HandleScope before calling this routine.
109  */
110  template<class T> inline T* UnwrapAndLooseOwnership(::v8::Handle<::v8::Object> obj)
111  {
112  ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(obj->GetInternalField(0));
113  void* p = field->Value();
114  JsObject<T>* jsObj = static_cast<JsObject<T>*>(p);
115  jsObj->m_isOwner = false;
116  return static_cast<T*>(jsObj->m_handle);
117  }
118 
119  /*!
120  \brief An utility function that extracts the C++ object from a wrapped object.
121 
122  \param obj The object holding the C++ pointer.
123 
124  \return The C++ pointer casted to the right type.
125 
126  \pre There must be a valid HandleScope before calling this routine.
127  */
128  template<class T> inline void LooseOwnership(::v8::Handle<::v8::Object> obj)
129  {
130  ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(obj->GetInternalField(0));
131  void* p = field->Value();
132  JsObject<T>* jsObj = static_cast<JsObject<T>*>(p);
133  jsObj->m_isOwner = false;
134  return;
135  }
136 
137  /*!
138  \brief The call-back function for releasing objects.
139 
140  \param obj The object to be disposed.
141  \param parameter The C++ pointer to be released.
142 
143  \pre There must be a valid HandleScope before calling this routine.
144  */
145  template<class T> void JsObjectRelease(::v8::Persistent<::v8::Value> obj, void* parameter)
146  {
147  //::v8::HandleScope hs;
148 
149  ::v8::Handle<::v8::Object> oobj = obj->ToObject();
150 
151  ::v8::Handle<::v8::External> field = ::v8::Handle<::v8::External>::Cast(oobj->GetInternalField(0));
152 
153  void* p = field->Value();
154 
155  JsObject<T>* cobj = static_cast<JsObject<T>*>(p);
156 
157  assert(cobj == static_cast<JsObject<T>*>(parameter));
158 
159  delete cobj;
160 
161  obj.Dispose();
162  }
163 
164  /*!
165  \brief It creates a new JavaScript object from a C++ object (obj).
166 
167  The new object will have tfunc as its object template.
168  If isOwner is true the JavaScript object will be the owner of the given C++ object pointer.
169 
170  \param obj A valid pointer to a C++ object.
171  \param tfunc A function with no arguments that returns the object template to be applied.
172  \param isOwner If true when the JavaSript object goes out if the scope it automatically release the C++ object, otherwise, it doesn't.
173 
174  \pre There must be a valid HandleScope before calling this routine.
175  */
176  template<class T, class TF> inline ::v8::Local<::v8::Object> Make(T* obj, TF tfunc, const bool isOwner)
177  {
178  //::v8::HandleScope hs;
179 
180  ::v8::Persistent<::v8::FunctionTemplate>& objFuncTemplate = tfunc();
181  ::v8::Local<::v8::ObjectTemplate> objTemplate = objFuncTemplate->InstanceTemplate();
182  objTemplate->SetInternalFieldCount(1);
183  ::v8::Local<::v8::Object> jsObj = objTemplate->NewInstance();
184 
185 // store the C++ object pointer in the JavaScript object
186  JsObject<T>* o = new JsObject<T>(obj, isOwner);
187 
188  ::v8::Local<::v8::External> objPtr = ::v8::External::New(o);
189  jsObj->SetInternalField(0, objPtr);
190 
191 // set the release call back function for the object
192  ::v8::Persistent<::v8::Object> pjsObj = ::v8::Persistent<::v8::Object>::New(jsObj);
193 
194  pjsObj.MakeWeak(o, JsObjectRelease<T>);
195 
196  //return hs.Close(jsObj);
197  return jsObj;
198  }
199 
200  /*!
201  \brief It converts the input map to an object representing the associative conteiner.
202 
203  \param m An input map to be converted to a JavsScript object.
204 
205  \pre There must be a valid HandleScope before calling this routine.
206 
207  \return An object representing the associative conteiner.
208  */
209  inline ::v8::Local<::v8::Object> Convert2Js(const std::map<std::string, std::string>& m)
210  {
211  ::v8::Local<::v8::Object> jsObj = ::v8::Object::New();
212 
213  std::map<std::string, std::string>::const_iterator it = m.begin();
214  std::map<std::string, std::string>::const_iterator itend = m.begin();
215 
216  while(it != itend)
217  {
218  ::v8::Local<::v8::String> key = ::v8::String::New(it->first.c_str());
219  ::v8::Local<::v8::String> value = ::v8::String::New(it->second.c_str());
220 
221  jsObj->Set(key, value);
222 
223  ++it;
224  }
225 
226  return jsObj;
227  }
228 
229  /*!
230  \brief It converts the input map to an object representing the associative conteiner.
231 
232  \param m An input map to be converted to a JavsScript object.
233 
234  \pre There must be a valid HandleScope before calling this routine.
235 
236  \return An object representing the associative conteiner.
237  */
238  inline void Convert2Cpp(const ::v8::Local<::v8::Object>& jsmap, std::map<std::string, std::string>& cppmap)
239  {
240  assert(!jsmap.IsEmpty());
241 
242  ::v8::Local<::v8::Array> amap = jsmap->GetPropertyNames();
243 
244  unsigned int size = amap->Length();
245 
246  for(unsigned int i = 0; i < size; ++i)
247  {
248  ::v8::Local<::v8::Value> key = amap->Get(i);
249  ::v8::Local<::v8::Value> value = jsmap->Get(key);
250 
251  ::v8::String::Utf8Value ukey(key->ToString());
252  ::v8::String::Utf8Value uvalue(value->ToString());
253 
254  cppmap[*ukey] = *uvalue;
255  }
256 
257  return;
258  }
259 
260 
261  } // end namespace common
262  } // end namespace v8
263 } // end namespace te
264 
265 #endif // __TERRALIB_BINDING_V8_COMMON_INTERNAL_UTILS_H
266 
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
void JsObjectRelease(::v8::Persistent<::v8::Value > obj, void *parameter)
The call-back function for releasing objects.
T * m_handle
A pointer to a C++ object.
Definition: JsObject.h:45
TEV8COMMONEXPORT::v8::Handle<::v8::String > ReadFile(const std::string &fileName)
It reads a file into a v8 string.
An auxiliary data structure for helping to control the garbage collection of wrapped C++ objects asso...
Definition: JsObject.h:43
inline::v8::Local<::v8::Object > Convert2Js(const std::map< std::string, std::string > &m)
It converts the input map to an object representing the associative conteiner.
bool m_isOwner
If true it specifies that JsObject has the ownership of the C++ handle.
Definition: JsObject.h:46
#define TEV8COMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
URI C++ Library.
Definition: Attributes.h:37
void LooseOwnership(::v8::Handle<::v8::Object > obj)
An utility function that extracts the C++ object from a wrapped object.
T * UnwrapAndLooseOwnership(::v8::Handle<::v8::Object > obj)
An utility function that extracts the C++ object from a wrapped object.
T * Unwrap(::v8::Handle<::v8::Object > obj)
An utility function that extracts the C++ object from a wrapped object.
void Convert2Cpp(const ::v8::Local<::v8::Object > &jsmap, std::map< std::string, std::string > &cppmap)
It converts the input map to an object representing the associative conteiner.
te::gm::Polygon * p
TEV8COMMONEXPORT void Cache(::v8::Local<::v8::Object > &obj, const std::string &methodName,::v8::Persistent<::v8::Function > &outFtor)
It caches the object method into the persistent output functor.
An auxiliary data structure for helping to control the garbage collection of wrapped C++ objects asso...
TEV8COMMONEXPORT::v8::Handle<::v8::Value > Print(const ::v8::Arguments &args)
This function will print all the arguments to the standard output.