Coord2D.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 Coord2D.cpp
22 
23  \brief Utility functions to register the Coord2D class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../geometry/Coord2D.h"
28 #include "../../common/Utils.h"
29 #include "Geometry.h"
30 
31 // STL
32 #include <memory>
33 
34 ::v8::Handle<::v8::Value> Coord2D_XGetter(::v8::Local<::v8::String> /*prop*/, const ::v8::AccessorInfo& info)
35 {
36  ::v8::HandleScope hs;
37 
38  if(info.Holder().IsEmpty())
39  return ::v8::ThrowException(::v8::String::New("Missing object in Coord2D x getter!"));
40 
41  te::gm::Coord2D* c = te::v8::common::Unwrap<te::gm::Coord2D>(info.Holder());
42 
43  return ::v8::Number::New(c->x);
44 }
45 
46 void Coord2D_XSetter(::v8::Local<::v8::String> /*prop*/, ::v8::Local<::v8::Value> value, const ::v8::AccessorInfo& info)
47 {
48  ::v8::HandleScope hs;
49 
50  if(info.Holder().IsEmpty())
51  {
52  ::v8::ThrowException(::v8::String::New("Missing object in Coord2D x setter!"));
53  return;
54  }
55 
56  if(value.IsEmpty() || !value->IsNumber())
57  {
58  ::v8::ThrowException(::v8::String::New("Missing value or wrong type in Coord2D x setter!"));
59  return;
60  }
61 
62  te::gm::Coord2D* c = te::v8::common::Unwrap<te::gm::Coord2D>(info.Holder());
63 
64  double x = value->ToNumber()->Value();
65 
66  c->x = x;
67 
68  return;
69 }
70 
71 ::v8::Handle<::v8::Value> Coord2D_YGetter(::v8::Local<::v8::String> /*prop*/, const ::v8::AccessorInfo& info)
72 {
73  ::v8::HandleScope hs;
74 
75  if(info.Holder().IsEmpty())
76  return ::v8::ThrowException(::v8::String::New("Missing object in Coord2D y getter!"));
77 
78  te::gm::Coord2D* c = te::v8::common::Unwrap<te::gm::Coord2D>(info.Holder());
79 
80  return ::v8::Number::New(c->y);
81 }
82 
83 void Coord2D_YSetter(::v8::Local<::v8::String> /*prop*/, ::v8::Local<::v8::Value> value, const ::v8::AccessorInfo& info)
84 {
85  ::v8::HandleScope hs;
86 
87  if(info.Holder().IsEmpty())
88  {
89  ::v8::ThrowException(::v8::String::New("Missing object in Coord2D y setter!"));
90  return;
91  }
92 
93  if(value.IsEmpty() || !value->IsNumber())
94  {
95  ::v8::ThrowException(::v8::String::New("Missing value or wrong type in Coord2D y setter!"));
96  return;
97  }
98 
99  te::gm::Coord2D* c = te::v8::common::Unwrap<te::gm::Coord2D>(info.Holder());
100 
101  double y = value->ToNumber()->Value();
102 
103  c->y = y;
104 
105  return;
106 }
107 
108 ::v8::Handle<::v8::Value> Coord2D_Constructor(const ::v8::Arguments& args)
109 {
110  ::v8::HandleScope hs;
111 
112  if(!args.IsConstructCall())
113  return ::v8::ThrowException(::v8::String::New("In order to create a coordinate you need call its constructor like: var c = new TeCoord2D(1.0, 2.0);"));
114 
115  if(args.Holder().IsEmpty())
116  return ::v8::ThrowException(::v8::String::New("Coord2D constructor must use object notation!"));
117 
118  if((args.Length() != 2) || !args[0]->IsNumber() || !args[1]->IsNumber())
119  return ::v8::ThrowException(::v8::String::New("Missing parameter in Coord2D constructor or wrong parameter type!"));
120 
121  double x = args[0]->ToNumber()->Value();
122  double y = args[1]->ToNumber()->Value();
123 
124  std::auto_ptr<te::gm::Coord2D> c(new te::gm::Coord2D(x, y));
125 
126  ::v8::Local<::v8::Object> jc = te::v8::common::Make(c.get(), te::v8::jsi::GetCoord2DTemplate, true);
127 
128  c.release();
129 
130  return hs.Close(jc);
131 }
132 
133 void te::v8::jsi::RegisterCoord2D(::v8::Local<::v8::Object>& global)
134 {
135  ::v8::HandleScope hs;
136 
137  ::v8::Local<::v8::FunctionTemplate> jsc = ::v8::FunctionTemplate::New(Coord2D_Constructor);
138 
139  global->Set(::v8::String::New("TeCoord2D"), jsc->GetFunction());
140 }
141 
142 static ::v8::Persistent<::v8::FunctionTemplate> sg_coord2d_template;
143 
144 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetCoord2DTemplate()
145 {
146  if(sg_coord2d_template.IsEmpty())
147  {
148  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
149 
150  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
151 
152  prototype->SetAccessor(::v8::String::NewSymbol("x"), Coord2D_XGetter, Coord2D_XSetter);
153  prototype->SetAccessor(::v8::String::NewSymbol("y"), Coord2D_YGetter, Coord2D_YSetter);
154 
155  sg_coord2d_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
156  }
157 
158  return sg_coord2d_template;
159 }
160 
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
static::v8::Persistent<::v8::FunctionTemplate > sg_coord2d_template
Definition: Coord2D.cpp:142
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
::v8::Persistent<::v8::FunctionTemplate > & GetCoord2DTemplate()
It returns a reference to the persistent template of a Coord2D object.
Definition: Coord2D.cpp:144
::v8::Handle<::v8::Value > Coord2D_Constructor(const ::v8::Arguments &args)
Definition: Coord2D.cpp:108
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void RegisterCoord2D(::v8::Local<::v8::Object > &global)
It register the Coord2D class.
Definition: Coord2D.cpp:133
void Coord2D_YSetter(::v8::Local<::v8::String >,::v8::Local<::v8::Value > value, const ::v8::AccessorInfo &info)
Definition: Coord2D.cpp:83
::v8::Handle<::v8::Value > Coord2D_XGetter(::v8::Local<::v8::String >, const ::v8::AccessorInfo &info)
Definition: Coord2D.cpp:34
void Coord2D_XSetter(::v8::Local<::v8::String >,::v8::Local<::v8::Value > value, const ::v8::AccessorInfo &info)
Definition: Coord2D.cpp:46
::v8::Handle<::v8::Value > Coord2D_YGetter(::v8::Local<::v8::String >, const ::v8::AccessorInfo &info)
Definition: Coord2D.cpp:71