-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.h
115 lines (94 loc) · 2.78 KB
/
Client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#ifndef CLIENT_H_
#define CLIENT_H_
#include "StdAfx.h"
namespace Sarona
{
class JSObject;
class PhysWorld;
/*
The Client class is created, owned and deleted by PhysWorld
Client::JSHandle is a helper class owned by the JS engine and
provides weak-ref access to the real Client class.
TODO: The Client class, though, outlives the JS engine? always?
*/
class Client
{
friend class PhysWorld;
public:
class JSHandle;
private:
struct Event
{
string type;
string subtype;
v8::Persistent<v8::Value> function; // string or function
v8::Persistent<v8::Object> context;
~Event() {function.Dispose(); context.Dispose();}
};
ptr_map<string, Event> m_events;
public:
string m_name;
ZCom_ConnID m_connection_id;
bool m_level_confirmed;
bool m_disconnected;
PhysWorld* m_world;
Client(PhysWorld* world_) :
m_name("Unnamed"),
m_connection_id(0),
m_level_confirmed(false),
m_disconnected(true),
m_world(world_){}
void CallEvent(const string& type, const string& subtype, const std::vector<v8::Handle< v8::Value > >& args = vector<v8::Handle<v8::Value> >());
// JS Side:
void SetCamera(v8::Handle<v8::Value> options);
void BindEvent(string type, string subtype, v8::Handle<v8::Value> func, v8::Handle<v8::Object> ctx = v8::Handle<v8::Object>());
};
class Client::JSHandle
{
private:
Client* m_ref;
public:
JSHandle():m_ref(NULL){};
void initialize(Client* ref){m_ref = ref;}
// wrapper functions that direct the calls to the native object
// throw exception if handle is invalid for some reason
// TODO: this copypaste mess could be cleaned with some template/macro hackery?
void SetCamera(v8::Handle<v8::Value> val)
{
if(!m_ref)
throw std::string("Invalid client handle");
return m_ref->SetCamera(val);
}
void BindEvent(string type, string subtype, v8::Handle<v8::Value> func, v8::Handle<v8::Value> ctx)
{
if(!m_ref)
throw std::string("Invalid client handle");
if(!ctx->IsObject())
throw std::string("Invalid context passed");
return m_ref->BindEvent(type, subtype, func, ctx->ToObject());
}
void BindEvent(string type, string subtype, v8::Handle<v8::Value> func)
{
if(!m_ref)
throw std::string("Invalid client handle");
return m_ref->BindEvent(type, subtype, func, v8::Handle<v8::Object>());
}
static v8::Handle<v8::Object> SetupClass(v8::Handle<v8::Object> dest);
};
}
// Class Name
JUICE_CLASSWRAP_CLASSNAME(Sarona::Client::JSHandle, "Client");
// Ctor binding
namespace v8{namespace juice{namespace cw{
template <>
struct Factory<Sarona::Client::JSHandle> : Factory_CtorForwarder<Sarona::Client::JSHandle,
tmp::TypeList<
convert::CtorForwarder0<Sarona::Client::JSHandle>
>
>
{};
}}}
#define CLASSWRAP_BOUND_TYPE Sarona::Client::JSHandle
#include <v8/juice/ClassWrap_TwoWay.h>
#endif // CLIENT_H_