OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkTemplates_DEFINED | 10 #ifndef SkTemplates_DEFINED |
11 #define SkTemplates_DEFINED | 11 #define SkTemplates_DEFINED |
12 | 12 |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
14 #include <limits> | |
15 #include <limits.h> | 14 #include <limits.h> |
16 #include <new> | 15 #include <new> |
17 | 16 |
18 /** \file SkTemplates.h | 17 /** \file SkTemplates.h |
19 | 18 |
20 This file contains light-weight template classes for type-safe and exception
-safe | 19 This file contains light-weight template classes for type-safe and exception
-safe |
21 resource management. | 20 resource management. |
22 */ | 21 */ |
23 | 22 |
24 /** | 23 /** |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 * Returns a pointer to a D which comes byteOffset bytes after S. | 58 * Returns a pointer to a D which comes byteOffset bytes after S. |
60 */ | 59 */ |
61 template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffs
et) { | 60 template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffs
et) { |
62 // The intermediate char* has the same const-ness as D as this produces bett
er error messages. | 61 // The intermediate char* has the same const-ness as D as this produces bett
er error messages. |
63 // This relies on the fact that reinterpret_cast can add constness, but cann
ot remove it. | 62 // This relies on the fact that reinterpret_cast can add constness, but cann
ot remove it. |
64 return reinterpret_cast<D*>( | 63 return reinterpret_cast<D*>( |
65 reinterpret_cast<typename SkTConstType<char, SkTIsConst<D>::value>::type
*>(ptr) + byteOffset | 64 reinterpret_cast<typename SkTConstType<char, SkTIsConst<D>::value>::type
*>(ptr) + byteOffset |
66 ); | 65 ); |
67 } | 66 } |
68 | 67 |
69 /** SkTSetBit<N, T>::value is a T with the Nth bit set. */ | |
70 template<unsigned N, typename T = uintmax_t> struct SkTSetBit { | |
71 static const T value = static_cast<T>(1) << N; | |
72 SK_COMPILE_ASSERT(sizeof(T)*CHAR_BIT > N, SkTSetBit_N_too_large); | |
73 SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, SkTSetBit_T_must_be_in
teger); | |
74 SK_COMPILE_ASSERT(!std::numeric_limits<T>::is_signed, SkTSetBit_T_must_be_un
signed); | |
75 SK_COMPILE_ASSERT(std::numeric_limits<T>::radix == 2, SkTSetBit_T_radix_must
_be_2); | |
76 }; | |
77 | |
78 /** \class SkAutoTCallVProc | 68 /** \class SkAutoTCallVProc |
79 | 69 |
80 Call a function when this goes out of scope. The template uses two | 70 Call a function when this goes out of scope. The template uses two |
81 parameters, the object, and a function that is to be called in the destructo
r. | 71 parameters, the object, and a function that is to be called in the destructo
r. |
82 If detach() is called, the object reference is set to null. If the object | 72 If detach() is called, the object reference is set to null. If the object |
83 reference is null when the destructor is called, we do not call the | 73 reference is null when the destructor is called, we do not call the |
84 function. | 74 function. |
85 */ | 75 */ |
86 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable { | 76 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable { |
87 public: | 77 public: |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 /** | 470 /** |
481 * Returns void* because this object does not initialize the | 471 * Returns void* because this object does not initialize the |
482 * memory. Use placement new for types that require a cons. | 472 * memory. Use placement new for types that require a cons. |
483 */ | 473 */ |
484 void* get() { return fStorage.get(); } | 474 void* get() { return fStorage.get(); } |
485 private: | 475 private: |
486 SkAlignedSStorage<sizeof(T)*N> fStorage; | 476 SkAlignedSStorage<sizeof(T)*N> fStorage; |
487 }; | 477 }; |
488 | 478 |
489 #endif | 479 #endif |
OLD | NEW |