Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 118 lines (94 sloc) 3.627 kB
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
1 #include "utils.hpp"
2 #include "mapnik_expression.hpp"
3 #include "mapnik_feature.hpp"
4 #include "utils.hpp"
5
6 #include <boost/make_shared.hpp>
7
8 Persistent<FunctionTemplate> Expression::constructor;
9
10 void Expression::Initialize(Handle<Object> target) {
11
12 HandleScope scope;
13
14 constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(Expression::New));
15 constructor->InstanceTemplate()->SetInternalFieldCount(1);
16 constructor->SetClassName(String::NewSymbol("Expression"));
17
18 NODE_SET_PROTOTYPE_METHOD(constructor, "toString", toString);
19 NODE_SET_PROTOTYPE_METHOD(constructor, "evaluate", evaluate);
20
21 target->Set(String::NewSymbol("Expression"),constructor->GetFunction());
22 }
23
24 Expression::Expression() :
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
25 ObjectWrap(),
26 this_() {}
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
27
28 Expression::~Expression()
29 {
30 }
31
32 Handle<Value> Expression::New(const Arguments& args)
33 {
34 HandleScope scope;
35 if (!args.IsConstructCall())
36 return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword"));
37
38 if (args[0]->IsExternal())
39 {
40 //std::clog << "external!\n";
41 Local<External> ext = Local<External>::Cast(args[0]);
42 void* ptr = ext->Value();
43 Expression* e = static_cast<Expression*>(ptr);
44 e->Wrap(args.This());
45 return args.This();
46 }
47
48 mapnik::expression_ptr e_ptr;
49 try
50 {
51 if (args.Length() == 1 && args[0]->IsString()){
52 e_ptr = mapnik::parse_expression(TOSTR(args[0]),"utf8");
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
53
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
54 } else {
55 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
56 String::New("invalid arguments: accepts a single argument of string type")));
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
57 }
58 }
38dd2ab @springmeyer better c++ style as per mapnik conventions
springmeyer authored
59 catch (std::exception const& ex)
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
60 {
61 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
62 String::New(ex.what())));
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
63 }
64 catch (...)
65 {
66 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
67 String::New("unknown exception happened, please file bug")));
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
68 }
69
70 if (e_ptr)
71 {
72 Expression* e = new Expression();
73 e->Wrap(args.This());
74 e->this_ = e_ptr;
75 return args.This();
76 }
77 else
78 {
79 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
80 String::New("unknown exception happened, please file bug")));
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
81 }
82
83 return Undefined();
84 }
85
86 Handle<Value> Expression::toString(const Arguments& args)
87 {
88 HandleScope scope;
89
90 Expression* e = ObjectWrap::Unwrap<Expression>(args.This());
91 return scope.Close(String::New( mapnik::to_expression_string(*e->get()).c_str() ));
92 }
93
94 Handle<Value> Expression::evaluate(const Arguments& args)
95 {
96 HandleScope scope;
97
98 if (!args.Length() > 0) {
99 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
100 String::New("requires a mapnik.Feature as an argument")));
4355b9f @springmeyer remainder of mapnik.Expression - refs #58
springmeyer authored
101 }
102
103 Local<Object> obj = args[0]->ToObject();
104 if (obj->IsNull() || obj->IsUndefined()) {
105 return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a mapnik.Feature not null/undefined")));
106 }
107
108 if (!Feature::constructor->HasInstance(obj)) {
109 return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a mapnik.Feature")));
110 }
111
112 Feature* f = ObjectWrap::Unwrap<Feature>(obj);
113
114 Expression* e = ObjectWrap::Unwrap<Expression>(args.This());
115 mapnik::value value_obj = boost::apply_visitor(mapnik::evaluate<mapnik::Feature,mapnik::value>(*(f->get())),*(e->get()));
116 return scope.Close(boost::apply_visitor(node_mapnik::value_converter(),value_obj.base()));
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
117 }
Something went wrong with that request. Please try again.