OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkOncePtr_DEFINED | 8 #ifndef SkOncePtr_DEFINED |
9 #define SkOncePtr_DEFINED | 9 #define SkOncePtr_DEFINED |
10 | 10 |
11 #include "../private/SkAtomics.h" | 11 #include "../private/SkAtomics.h" |
12 #include "SkUniquePtr.h" | 12 #include <memory> |
13 | 13 |
14 template <typename T> class SkBaseOncePtr; | 14 template <typename T> class SkBaseOncePtr; |
15 | 15 |
16 // Use this to create a global static pointer that's intialized exactly once whe
n you call get(). | 16 // Use this to create a global static pointer that's intialized exactly once whe
n you call get(). |
17 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr
<type> name; | 17 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr
<type> name; |
18 | 18 |
19 // Use this for a local or member pointer that's initialized exactly once when y
ou call get(). | 19 // Use this for a local or member pointer that's initialized exactly once when y
ou call get(). |
20 template <typename T, typename Delete = skstd::default_delete<T>> | 20 template <typename T, typename Delete = std::default_delete<T>> |
21 class SkOncePtr : SkNoncopyable { | 21 class SkOncePtr : SkNoncopyable { |
22 public: | 22 public: |
23 SkOncePtr() { sk_bzero(this, sizeof(*this)); } | 23 SkOncePtr() { sk_bzero(this, sizeof(*this)); } |
24 ~SkOncePtr() { | 24 ~SkOncePtr() { |
25 if (T* ptr = (T*)*this) { | 25 if (T* ptr = (T*)*this) { |
26 Delete()(ptr); | 26 Delete()(ptr); |
27 } | 27 } |
28 } | 28 } |
29 | 29 |
30 template <typename F> | 30 template <typename F> |
31 T* get(const F& f) const { | 31 T* get(const F& f) const { |
32 return fOnce.get(f); | 32 return fOnce.get(f); |
33 } | 33 } |
34 | 34 |
35 operator T*() const { | 35 operator T*() const { |
36 return (T*)fOnce; | 36 return (T*)fOnce; |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 SkBaseOncePtr<T> fOnce; | 40 SkBaseOncePtr<T> fOnce; |
41 }; | 41 }; |
42 | 42 |
43 // If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default. | 43 // If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default. |
44 template <typename T> | 44 template <typename T> |
45 class SkOncePtr<T[]> : public SkOncePtr<T, skstd::default_delete<T[]>> {}; | 45 class SkOncePtr<T[]> : public SkOncePtr<T, std::default_delete<T[]>> {}; |
46 | 46 |
47 /* TODO(mtklein): in next CL | 47 /* TODO(mtklein): in next CL |
48 typedef SkBaseOncePtr<void> SkOnceFlag; | 48 typedef SkBaseOncePtr<void> SkOnceFlag; |
49 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name | 49 #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name |
50 | 50 |
51 template <typename F> | 51 template <typename F> |
52 inline void SkOnce(SkOnceFlag* once, const F& f) { | 52 inline void SkOnce(SkOnceFlag* once, const F& f) { |
53 once->get([&]{ f(); return (void*)2; }); | 53 once->get([&]{ f(); return (void*)2; }); |
54 } | 54 } |
55 */ | 55 */ |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 // TODO: If state == 1 spin until it's not? | 97 // TODO: If state == 1 spin until it's not? |
98 } | 98 } |
99 | 99 |
100 // fState == 0 --> we have not created our ptr yet | 100 // fState == 0 --> we have not created our ptr yet |
101 // fState == 1 --> someone is in the middle of creating our ptr | 101 // fState == 1 --> someone is in the middle of creating our ptr |
102 // else --> (T*)fState is our ptr | 102 // else --> (T*)fState is our ptr |
103 mutable uintptr_t fState; | 103 mutable uintptr_t fState; |
104 }; | 104 }; |
105 | 105 |
106 #endif//SkOncePtr_DEFINED | 106 #endif//SkOncePtr_DEFINED |
OLD | NEW |