| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_SHARED_IMPL_RESOURCE_OBJECT_BASE_H_ | 5 #ifndef PPAPI_SHARED_IMPL_RESOURCE_OBJECT_BASE_H_ |
| 6 #define PPAPI_SHARED_IMPL_RESOURCE_OBJECT_BASE_H_ | 6 #define PPAPI_SHARED_IMPL_RESOURCE_OBJECT_BASE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> // For NULL. |
| 9 |
| 10 #define FOR_ALL_PPAPI_RESOURCE_APIS(F) \ |
| 11 F(PPB_Audio_API) \ |
| 12 F(PPB_AudioConfig_API) \ |
| 13 F(PPB_AudioTrusted_API) \ |
| 14 F(PPB_Broker_API) \ |
| 15 F(PPB_Buffer_API) \ |
| 16 F(PPB_DirectoryReader_API) \ |
| 17 F(PPB_FileChooser_API) \ |
| 18 F(PPB_FileIO_API) \ |
| 19 F(PPB_FileRef_API) \ |
| 20 F(PPB_FileSystem_API) \ |
| 21 F(PPB_Find_API) \ |
| 22 F(PPB_Font_API) \ |
| 23 F(PPB_Graphics2D_API) \ |
| 24 F(PPB_ImageData_API) |
| 25 |
| 8 namespace ppapi { | 26 namespace ppapi { |
| 9 | 27 |
| 28 // Forward declare all the resource APIs. |
| 10 namespace thunk { | 29 namespace thunk { |
| 11 class PPB_Audio_API; | 30 #define DECLARE_RESOURCE_CLASS(RESOURCE) class RESOURCE; |
| 12 class PPB_AudioConfig_API; | 31 FOR_ALL_PPAPI_RESOURCE_APIS(DECLARE_RESOURCE_CLASS) |
| 13 class PPB_AudioTrusted_API; | 32 #undef DECLARE_RESOURCE_CLASS |
| 14 class PPB_Broker_API; | 33 } // namespace thunk |
| 15 class PPB_Buffer_API; | |
| 16 class PPB_Font_API; | |
| 17 class PPB_Graphics2D_API; | |
| 18 class PPB_ImageData_API; | |
| 19 } | |
| 20 | 34 |
| 21 class ResourceObjectBase { | 35 class ResourceObjectBase { |
| 22 public: | 36 public: |
| 37 virtual ~ResourceObjectBase(); |
| 23 | 38 |
| 24 virtual thunk::PPB_Audio_API* AsAudio_API() { return NULL; } | 39 // Dynamic casting for this object. Returns the pointer to the given type if |
| 25 virtual thunk::PPB_AudioConfig_API* AsAudioConfig_API() { return NULL; } | 40 // Inheritance-based dynamic casting for this object. Returns the pointer to |
| 26 virtual thunk::PPB_AudioTrusted_API* AsAudioTrusted_API() { return NULL; } | 41 // the given type if it's supported. Derived classes override the functions |
| 27 virtual thunk::PPB_Buffer_API* AsBuffer_API() { return NULL; } | 42 // they support to return the interface. |
| 28 virtual thunk::PPB_Broker_API* AsBroker_API() { return NULL; } | 43 #define DEFINE_TYPE_GETTER(RESOURCE) \ |
| 29 virtual thunk::PPB_Font_API* AsFont_API() { return NULL; } | 44 virtual thunk::RESOURCE* As##RESOURCE(); |
| 30 virtual thunk::PPB_Graphics2D_API* AsGraphics2D_API() { return NULL; } | 45 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_TYPE_GETTER) |
| 31 virtual thunk::PPB_ImageData_API* AsImageData_API() { return NULL; } | 46 #undef DEFINE_TYPE_GETTER |
| 32 | 47 |
| 48 // Template-based dynamic casting. See specializations below. |
| 33 template <typename T> T* GetAs() { return NULL; } | 49 template <typename T> T* GetAs() { return NULL; } |
| 34 }; | 50 }; |
| 35 | 51 |
| 36 template<> | 52 // Template-based dynamic casting. These specializations forward to the |
| 37 inline thunk::PPB_Audio_API* ResourceObjectBase::GetAs() { | 53 // AsXXX virtual functions to return whether the given type is supported. |
| 38 return AsAudio_API(); | 54 #define DEFINE_RESOURCE_CAST(RESOURCE) \ |
| 39 } | 55 template<> inline thunk::RESOURCE* ResourceObjectBase::GetAs() { \ |
| 40 template<> | 56 return As##RESOURCE(); \ |
| 41 inline thunk::PPB_AudioConfig_API* ResourceObjectBase::GetAs() { | 57 } |
| 42 return AsAudioConfig_API(); | 58 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_RESOURCE_CAST) |
| 43 } | 59 #undef DEFINE_RESOURCE_CAST |
| 44 template<> | |
| 45 inline thunk::PPB_AudioTrusted_API* ResourceObjectBase::GetAs() { | |
| 46 return AsAudioTrusted_API(); | |
| 47 } | |
| 48 template<> | |
| 49 inline thunk::PPB_Broker_API* ResourceObjectBase::GetAs() { | |
| 50 return AsBroker_API(); | |
| 51 } | |
| 52 template<> | |
| 53 inline thunk::PPB_Buffer_API* ResourceObjectBase::GetAs() { | |
| 54 return AsBuffer_API(); | |
| 55 } | |
| 56 template<> | |
| 57 inline thunk::PPB_Font_API* ResourceObjectBase::GetAs() { | |
| 58 return AsFont_API(); | |
| 59 } | |
| 60 template<> | |
| 61 inline thunk::PPB_Graphics2D_API* ResourceObjectBase::GetAs() { | |
| 62 return AsGraphics2D_API(); | |
| 63 } | |
| 64 template<> | |
| 65 inline thunk::PPB_ImageData_API* ResourceObjectBase::GetAs() { | |
| 66 return AsImageData_API(); | |
| 67 } | |
| 68 | 60 |
| 69 } // namespace ppapi | 61 } // namespace ppapi |
| 70 | 62 |
| 71 #endif // PPAPI_SHARED_IMPL_RESOURCE_OBJECT_BASE_H_ | 63 #endif // PPAPI_SHARED_IMPL_RESOURCE_OBJECT_BASE_H_ |
| OLD | NEW |