Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
resourcecallback.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFXUTILS_RESOURCECALLBACK_H_
19 #define ION_GFXUTILS_RESOURCECALLBACK_H_
20 
21 #include <vector>
22 
23 #include "ion/base/referent.h"
25 #include "ion/port/semaphore.h"
26 
27 namespace ion {
28 namespace gfxutils {
29 
33 template <typename T> class ResourceCallback : public base::Referent {
34  public:
36  explicit ResourceCallback(bool do_wait) {
37  if (!do_wait)
38  semaphore_.Post();
39  callback_holder_.Reset(this);
40  wait_holder_.Reset(this);
41  }
42 
44  const std::vector<T>& GetData() const { return data_; }
45 
50  void Callback(const std::vector<T>& data) {
52  data_ = data;
53 
56  RefPtr holder(this);
57  callback_holder_ = NULL;
58 
60  semaphore_.Post();
61 
62  holder = NULL;
65  }
66 
67  void WaitForCompletion(std::vector<T>* data) {
69  semaphore_.Wait();
70 
73  if (!callback_holder_.Get() && data)
74  *data = data_;
75 
78  RefPtr holder(this);
79  wait_holder_ = NULL;
80  holder = NULL;
83  }
84 
85  protected:
87  ~ResourceCallback() override {
88  DCHECK(callback_holder_.Get() == NULL);
89  DCHECK(wait_holder_.Get() == NULL);
90  }
91 
92  std::vector<T> data_;
96 };
97 
109 
110 } // namespace gfxutils
111 } // namespace ion
112 
113 #endif // ION_GFXUTILS_RESOURCECALLBACK_H_
bool Post()
Wakes a single thread that is Wait()ing, or the next thread to call Wait().
Definition: semaphore.cc:85
void Callback(const std::vector< T > &data)
This function is compatible with ResourceManager::InfoCallback, and should be used as the callback pa...
bool Wait()
Blocks the calling thread until another thread calls Post().
Definition: semaphore.cc:205
Class that allows blocking until a callback is called and automagically destroys itself after both Ca...
base::ReferentPtr< ResourceCallback< T > >::Type RefPtr
#define DCHECK(expr)
Definition: logging.h:331
~ResourceCallback() override
The constructor is protected because this class is derived from Referent.
A Semaphore enables threads and process synchronization.
Definition: semaphore.h:40
ResourceCallback< gfx::ResourceManager::FramebufferInfo > FramebufferCallback
void WaitForCompletion(std::vector< T > *data)
ResourceCallback< gfx::ResourceManager::TextureImageInfo > TextureImageCallback
Thread-safe abstract base class.
Definition: referent.h:49
ResourceCallback< gfx::ResourceManager::ShaderInfo > ShaderCallback
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
ResourceCallback< gfx::ResourceManager::TextureInfo > TextureCallback
A ReferentPtr is a smart shared pointer to an instance of some class derived from Referent...
Definition: referent.h:59
ResourceCallback< gfx::ResourceManager::SamplerInfo > SamplerCallback
void Reset(T *new_shared)
Changes the pointer to point to the given shared, which may be NULL.
Definition: sharedptr.h:92
ResourceCallback< gfx::ResourceManager::ProgramInfo > ProgramCallback
ResourceCallback< gfx::ResourceManager::PlatformInfo > PlatformCallback
const std::vector< T > & GetData() const
Returns the data received by Callback().
ResourceCallback< gfx::ResourceManager::ArrayInfo > ArrayCallback
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
ResourceCallback< gfx::ResourceManager::BufferInfo > BufferCallback