Viskores  1.0
Token.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_Token_h
19 #define viskores_cont_Token_h
20 
22 
23 #include <viskores/Types.h>
24 
25 #include <condition_variable>
26 #include <memory>
27 #include <mutex>
28 #include <type_traits>
29 
30 namespace viskores
31 {
32 namespace cont
33 {
34 
43 class VISKORES_CONT_EXPORT Token final
44 {
45  class InternalStruct;
46  mutable std::unique_ptr<InternalStruct> Internals;
47 
48  struct HeldReference;
49 
51  {
52  virtual ~ObjectReference() = default;
53  };
54 
55  template <typename ObjectType>
57  {
58  ObjectType Object;
59 
60  ObjectReferenceImpl(const ObjectType& object)
61  : Object(object)
62  {
63  }
64 
65  ObjectReferenceImpl(ObjectType&& object)
66  : Object(std::move(object))
67  {
68  }
69 
70  ObjectReferenceImpl() = delete;
72 
73  ~ObjectReferenceImpl() override = default;
74  };
75 
76 public:
78  VISKORES_CONT Token(Token&& rhs);
80 
83 
87  VISKORES_CONT void DetachFromAll();
88 
108  template <typename T>
109  VISKORES_CONT void Attach(T&& object,
110  viskores::cont::Token::ReferenceCount* referenceCountPointer,
111  std::unique_lock<std::mutex>& lock,
112  std::condition_variable* conditionVariablePointer)
113  {
114  this->Attach(std::unique_ptr<ObjectReference>(
115  new ObjectReferenceImpl<typename std::decay<T>::type>(std::forward<T>(object))),
116  referenceCountPointer,
117  lock,
118  conditionVariablePointer);
119  }
120 
122  template <typename T>
123  VISKORES_CONT void Attach(T&& object,
124  viskores::cont::Token::ReferenceCount* referenceCountPoiner,
125  std::mutex* mutexPointer,
126  std::condition_variable* conditionVariablePointer)
127  {
128  std::unique_lock<std::mutex> lock(*mutexPointer, std::defer_lock);
129  this->Attach(std::forward<T>(object), referenceCountPoiner, lock, conditionVariablePointer);
130  }
131 
137  VISKORES_CONT bool IsAttached(viskores::cont::Token::ReferenceCount* referenceCountPointer) const;
138 
139  class Reference
140  {
141  friend Token;
142  const void* InternalsPointer;
143  VISKORES_CONT Reference(const void* internalsPointer)
144  : InternalsPointer(internalsPointer)
145  {
146  }
147 
148  public:
150  {
151  return this->InternalsPointer == rhs.InternalsPointer;
152  }
154  {
155  return this->InternalsPointer != rhs.InternalsPointer;
156  }
157  };
158 
169  VISKORES_CONT Reference GetReference() const;
170 
171 private:
172  VISKORES_CONT void Attach(
173  std::unique_ptr<viskores::cont::Token::ObjectReference>&& objectReference,
174  viskores::cont::Token::ReferenceCount* referenceCountPointer,
175  std::unique_lock<std::mutex>& lock,
176  std::condition_variable* conditionVariablePointer);
177 
178  VISKORES_CONT bool IsAttached(std::unique_lock<std::mutex>& lock,
179  viskores::cont::Token::ReferenceCount* referenceCountPointer) const;
180 };
181 
184 {
185  return token.GetReference() == ref;
186 }
189 {
190  return token.GetReference() != ref;
191 }
192 
194  const viskores::cont::Token& token)
195 {
196  return ref == token.GetReference();
197 }
199  const viskores::cont::Token& token)
200 {
201  return ref != token.GetReference();
202 }
203 }
204 } // namespace viskores::cont
205 
206 #endif //viskores_cont_Token_h
viskores::cont::operator==
bool operator==(const viskores::cont::Token &token, viskores::cont::Token::Reference ref)
Definition: Token.h:182
Types.h
viskores::cont::Token::ObjectReferenceImpl::ObjectReferenceImpl
ObjectReferenceImpl(const ObjectType &object)
Definition: Token.h:60
viskores::cont::Token::Internals
std::unique_ptr< InternalStruct > Internals
Definition: Token.h:45
viskores::cont::Token::ObjectReferenceImpl
Definition: Token.h:56
viskores::cont::Token::Reference::operator!=
bool operator!=(Reference rhs) const
Definition: Token.h:153
viskores::cont::Token::Reference::Token
friend Token
Definition: Token.h:141
viskores::cont::Token::Reference::Reference
Reference(const void *internalsPointer)
Definition: Token.h:143
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::cont::Token::Attach
void Attach(T &&object, viskores::cont::Token::ReferenceCount *referenceCountPoiner, std::mutex *mutexPointer, std::condition_variable *conditionVariablePointer)
Add an object to attach to the Token.
Definition: Token.h:123
viskores::cont::operator!=
bool operator!=(const viskores::cont::Token &token, viskores::cont::Token::Reference ref)
Definition: Token.h:187
viskores::cont::Token::ObjectReferenceImpl::ObjectReferenceImpl
ObjectReferenceImpl(ObjectType &&object)
Definition: Token.h:65
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::cont::Token::ObjectReferenceImpl::Object
ObjectType Object
Definition: Token.h:58
viskores::cont::Token::ReferenceCount
viskores::IdComponent ReferenceCount
Use this type to represent counts of how many tokens are holding a resource.
Definition: Token.h:82
viskores::cont::Token::Reference::operator==
bool operator==(Reference rhs) const
Definition: Token.h:149
viskores::cont::Token::ObjectReference
Definition: Token.h:50
viskores::cont::Token::Reference::InternalsPointer
const void * InternalsPointer
Definition: Token.h:142
viskores::cont::Token::GetReference
Reference GetReference() const
Returns a reference object to this Token.
viskores::cont::Token::Reference
Definition: Token.h:139
viskores::cont::Token::Attach
void Attach(T &&object, viskores::cont::Token::ReferenceCount *referenceCountPointer, std::unique_lock< std::mutex > &lock, std::condition_variable *conditionVariablePointer)
Add an object to attach to the Token.
Definition: Token.h:109
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores_cont_export.h