vSMC
vSMC: Scalable Monte Carlo
dispatch_object.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/gcd/dispatch_object.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_GCD_DISPATCH_OBJECT_HPP
33 #define VSMC_GCD_DISPATCH_OBJECT_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #include <dispatch/dispatch.h>
37 
38 namespace vsmc {
39 
44 template <typename DispatchType>
46 {
47  public :
48 
56  DispatchObject (const DispatchType &object, bool retained) :
57  object_(object) {if (!retained) retain();}
58 
60  object_(other.object_) {retain();}
61 
63  const DispatchObject<DispatchType> &other)
64  {
65  if (this != &other && object_ != other.object_) {
66  release();
67  object_ = other.object_;
68  retain();
69  }
70 
71  return *this;
72  }
73 
74 #if VSMC_HAS_CXX11_RVALUE_REFERENCES
76  object_(cxx11::move(other.object_)) {other.object_ = VSMC_NULLPTR;}
77 
80  {
81  using std::swap;
82 
83  if (this != &other && object_ != other.object_)
84  swap(object_, other.object_);
85 
86  return *this;
87  }
88 #endif
89 
90  ~DispatchObject () {release();}
91 
93  DispatchType object () const {return object_;}
94 
96  void object (DispatchType obj)
97  {
98  if (object_ != obj) {
99  release();
100  object_ = obj;
101  retain();
102  }
103  }
104 
105  void *get_context () const
106  {return ::dispatch_get_context(object_);}
107 
108  void set_context (void *context) const
109  {::dispatch_set_context(object_, context);}
110 
111  void set_finalizer_f (::dispatch_function_t finalizer) const
112  {::dispatch_set_finalizer_f(object_, finalizer);}
113 
114  private :
115 
116  DispatchType object_;
117 
118  void retain ()
119  {
120  if (object_ != VSMC_NULLPTR)
121  ::dispatch_retain(object_);
122  }
123 
124  void release ()
125  {
126  if (object_ != VSMC_NULLPTR)
127  ::dispatch_release(object_);
128  }
129 }; // class DispatchObject
130 
131 } // namespace vsmc
132 
133 #endif // VSMC_GCD_DISPATCH_OBJECT_HPP
Definition: adapter.hpp:37
Base class of Dispatch objects.
DispatchObject(DispatchObject< DispatchType > &&other)
void * get_context() const
DispatchObject(const DispatchObject< DispatchType > &other)
void object(DispatchType obj)
Set the underlying Dispatch object and retain it.
void set_context(void *context) const
remove_reference< T >::type && move(T &&t) noexcept
#define VSMC_NULLPTR
nullptr
Definition: defines.hpp:79
void swap(Array< T, N > &ary1, Array< T, N > &ary2)
Array ADL of swap.
Definition: array.hpp:317
DispatchObject(const DispatchType &object, bool retained)
Create a DispatchObject from its C-type object.
void set_finalizer_f(::dispatch_function_t finalizer) const
DispatchObject< DispatchType > & operator=(const DispatchObject< DispatchType > &other)
DispatchType object() const
Return the underlying Dispatch object.