binding/v8/jsi/geometry/LinearRing.cpp
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 LinearRing.cpp
22 
23  \brief Utility functions to register the LinearRing class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../geometry/LinearRing.h"
28 #include "../../common/Utils.h"
29 #include "Geometry.h"
30 
31 ::v8::Handle<::v8::Value> LinearRing_Constructor(const ::v8::Arguments& args)
32 {
33  ::v8::HandleScope hs;
34 
35  if(!args.IsConstructCall())
36  return ::v8::ThrowException(::v8::String::New("In order to create a LinearRing you need to call its constructor like: var mygc = new TeLinearRing(3, TE_OGC_LINESTRING, 4326)."));
37 
38  if(args.Holder().IsEmpty())
39  return ::v8::ThrowException(::v8::String::New("LinearRing constructor must use object notation!"));
40 
41  if((args.Length() != 3) ||
42  args[0].IsEmpty() || !args[0]->IsInt32() ||
43  args[1].IsEmpty() || !args[1]->IsInt32() ||
44  args[2].IsEmpty() || !args[2]->IsInt32())
45  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong parameter type in LinearRing constructor method!!"));
46 
47 
48  int npts = args[0]->Int32Value();
49  int geomType = args[1]->Int32Value();
50  int srid = args[2]->ToInt32()->Int32Value();
51 
52  std::auto_ptr<te::gm::LinearRing> g(new te::gm::LinearRing(npts, static_cast<te::gm::GeomType>(geomType), srid));
53 
54  ::v8::Local<::v8::Object> jg = te::v8::common::Make(g.get(), te::v8::jsi::GetLinearRingTemplate, true);
55 
56  g.release();
57 
58  return hs.Close(jg);
59 }
60 
61 void te::v8::jsi::RegisterLinearRing(::v8::Local<::v8::Object>& global)
62 {
63  ::v8::HandleScope hs;
64 
65  ::v8::Local<::v8::FunctionTemplate> jsgc = ::v8::FunctionTemplate::New(LinearRing_Constructor);
66 
67  global->Set(::v8::String::New("TeLinearRing"), jsgc->GetFunction());
68 }
69 
70 static ::v8::Persistent<::v8::FunctionTemplate> sg_ring_template;
71 
72 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetLinearRingTemplate()
73 {
74  if(sg_ring_template.IsEmpty())
75  {
76  ::v8::Persistent<::v8::FunctionTemplate>& lineTpl = GetLineStringTemplate();
77  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
78  result->Inherit(lineTpl);
79 
80  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
81 
82  sg_ring_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
83  }
84 
85  return sg_ring_template;
86 }
87 
88 
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
::v8::Persistent<::v8::FunctionTemplate > & GetLinearRingTemplate()
It returns a reference to the persistent template of a LinearRing object.
::v8::Persistent<::v8::FunctionTemplate > & GetLineStringTemplate()
It returns a reference to the persistent template of a LineString object.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
void RegisterLinearRing(::v8::Local<::v8::Object > &global)
It register the LinearRing class.
::v8::Handle<::v8::Value > LinearRing_Constructor(const ::v8::Arguments &args)
static::v8::Persistent<::v8::FunctionTemplate > sg_ring_template