1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #ifndef LIB_TONIC_DART_PERSISTENT_VALUE_H_ |
6 | #define LIB_TONIC_DART_PERSISTENT_VALUE_H_ |
7 | |
8 | #include <memory> |
9 | |
10 | #include "third_party/dart/runtime/include/dart_api.h" |
11 | #include "tonic/common/macros.h" |
12 | |
13 | namespace tonic { |
14 | class DartState; |
15 | |
16 | // DartPersistentValue is a bookkeeping class to help pair calls to |
17 | // Dart_NewPersistentHandle with Dart_DeletePersistentHandle. Consider using |
18 | // this class instead of holding a Dart_PersistentHandle directly so that you |
19 | // don't leak the Dart_PersistentHandle. |
20 | class DartPersistentValue { |
21 | public: |
22 | DartPersistentValue(); |
23 | DartPersistentValue(DartPersistentValue&& other); |
24 | DartPersistentValue(DartState* dart_state, Dart_Handle value); |
25 | ~DartPersistentValue(); |
26 | |
27 | Dart_PersistentHandle value() const { return value_; } |
28 | bool is_empty() const { return !value_; } |
29 | |
30 | void Set(DartState* dart_state, Dart_Handle value); |
31 | void Clear(); |
32 | Dart_Handle Get(); |
33 | Dart_Handle Release(); |
34 | |
35 | const std::weak_ptr<DartState>& dart_state() const { return dart_state_; } |
36 | |
37 | private: |
38 | std::weak_ptr<DartState> dart_state_; |
39 | Dart_PersistentHandle value_; |
40 | |
41 | TONIC_DISALLOW_COPY_AND_ASSIGN(DartPersistentValue); |
42 | }; |
43 | |
44 | } // namespace tonic |
45 | |
46 | #endif // LIB_TONIC_DART_PERSISTENT_VALUE_H_ |
47 |