summaryrefslogtreecommitdiff
path: root/src/CommonsJavaScript/PrivateObject.h
blob: 8bd816a0982fb4ca46559797c754d08f8ba6b9b8 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
#ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_PRIVATEOBJECT_H_
#define WRTDEVICEAPIS_COMMONSJAVASCRIPT_PRIVATEOBJECT_H_

#include <JavaScriptCore/JavaScript.h>
#include <dpl/noncopyable.h>
#include <dpl/assert.h>
#include <Commons/Exception.h>
#include <Commons/TypeTraits.h>

namespace WrtDeviceApis {
namespace CommonsJavaScript {
template<class T>
struct NoAcquire
{
    void acquire(T*)
    {}

  protected:
    ~NoAcquire()
    {}
};

template<class T>
struct AcquireByProtect
{
    void acquire(T* object)
    {
        Assert(object && "Object passed to protect can't be NULL.");
        JSValueProtect(object->getContext(), object->getObject());
    }

  protected:
    ~AcquireByProtect()
    {}
};

template<class T>
struct NoRelease
{
    void release(T* object)
    {
        (void)object;
    }

  protected:
    ~NoRelease()
    {}
};

template<class T>
struct ReleaseByDelete
{
    void release(T* object)
    {
        delete object->getObject();
    }

  protected:
    ~ReleaseByDelete()
    {}
};

template<class T>
struct ReleaseByUnprotect
{
    void release(T* object)
    {
        Assert(object && "Object passed to unprotect can't be NULL.");
        JSValueUnprotect(object->getContext(), object->getObject());
    }

  protected:
    ~ReleaseByUnprotect()
    {}
};

template<class T>
struct NoOwnership : protected NoAcquire<T>,
    protected NoRelease<T>
{
  protected:
    ~NoOwnership()
    {}
};

template<class T>
struct OwnershipByAcquisition : protected NoAcquire<T>,
    protected ReleaseByDelete<T>
{
  protected:
    ~OwnershipByAcquisition()
    {}
};

template<class T>
struct OwnershipByProtection : protected AcquireByProtect<T>,
    protected ReleaseByUnprotect<T>
{
  protected:
    ~OwnershipByProtection()
    {}
};

template<class PrivateClass,
         template <class> class OwnershipPolicy = OwnershipByAcquisition>
class PrivateObject : public DPL::Noncopyable,
    protected OwnershipPolicy<PrivateObject<PrivateClass, OwnershipPolicy> >
{
  public:
    typedef PrivateClass ObjectType;

  public:
    /**
     * Creates storage object for JS private data.
     * @param context JS (root/global) context.
     * @param object Object to store.
     * @throw NullPointerException When object is pointer and is set to NULL.
     */
    PrivateObject(JSContextRef context,
                  const PrivateClass& object) :
        m_context(context),
        m_object(object)
    {
        Assert(NULL != m_context && "Context is NULL.");
        Assert(!Commons::IsNull<PrivateClass>::value(
                   object) && "Object is NULL.");
        this->acquire(this);
    }

    /**
     * Destroys instance of the object.
     */
    virtual ~PrivateObject()
    {
        this->release(this);
    }

    /**
     * Gets stored JS context.
     * @return JavaScript context.
     */
    virtual JSContextRef getContext() const
    {
        return m_context;
    }

    /**
     * Gets stored object.
     * @return Stored object.
     */
    virtual PrivateClass getObject() const
    {
        return m_object;
    }

  protected:
    JSContextRef m_context; ///< JS context.
    PrivateClass m_object; ///< Stored object.
};

/**
 * Specialization for type void.
 */
template<>
class PrivateObject<void, NoOwnership> : private DPL::Noncopyable
{
  public:
    /**
     * Creates storage object for JS private data.
     * @param context JS (root/global) context.
     * @remarks Takes ownership over stored object.
     */
    explicit PrivateObject(JSContextRef context) : m_context(context)
    {
        Assert(NULL != m_context && "Context is NULL.");
    }

    /**
     * Destroys instance of the object.
     */
    virtual ~PrivateObject()
    {}

    /**
     * Gets stored JS context.
     * @return JavaScript context.
     */
    virtual JSContextRef getContext() const
    {
        return m_context;
    }

  protected:
    JSContextRef m_context;
};

template<class C>
struct PrivateObjectT
{
    typedef PrivateObject<C, NoOwnership> Type;
};

template<class C>
struct PrivateObjectT<C*>
{
    typedef PrivateObject<C*, OwnershipByAcquisition> Type;
};

template<>
struct PrivateObjectT<JSObjectRef>
{
    typedef PrivateObject<JSObjectRef, NoOwnership> Type;
};

template<>
struct PrivateObjectT<void>
{
    typedef PrivateObject<void, NoOwnership> Type;
};
} // CommonsJavaScript
} // WrtDeviceApis

#endif /* PRIVATEOBJECT_H_ */