FAST  3.2.0
Framework for Heterogeneous Medical Image Computing and Visualization
SimpleDataObject.hpp
Go to the documentation of this file.
1 #ifndef SIMPLEDATAOBJECT_HPP
2 #define SIMPLEDATAOBJECT_HPP
3 
4 #include "DataObject.hpp"
5 #include "Access/Access.hpp"
6 
7 namespace fast {
8 
9 // A macro for creating new simple data objects
10 #define FAST_SIMPLE_DATA_OBJECT(NAME, DATA_TYPE) \
11  class NAME : public SimpleDataObject<DATA_TYPE > { \
12  FAST_OBJECT(NAME) \
13 public: \
14  typedef DataAccess<DATA_TYPE >::pointer access; \
15 private: \
16  NAME() {}; \
17 }; \
18 
19 // Forward declarations
20 template <class DataType>
21 class DataAccess;
22 
23 template <class DataType, typename AccessObject = DataAccess<DataType> >
25 
26 
27 template <class DataType>
28 class DataAccess {
29 public:
30  DataAccess(DataType* data, std::shared_ptr<SimpleDataObject<DataType> > dataObject);
31  DataType getData();
32  void setData(const DataType& data);
33  void release();
34  ~DataAccess();
35 
36  typedef std::unique_ptr<DataAccess<DataType> > pointer;
37 protected:
39  std::shared_ptr<SimpleDataObject<DataType> > mDataObject;
40 };
41 
42 
43 template <class DataType>
45  mData = data;
46  mDataObject = dataObject;
47 }
48 
49 template <class DataType>
51  return *mData;
52 }
53 
54 template <class DataType>
56  *mData = data;
57 }
58 
59 template <class DataType>
61  mDataObject->accessFinished();
62 }
63 
64 template <class DataType>
66  release();
67 }
68 
69 
70 
71 template <class DataType, typename AccessObject>
72 class SimpleDataObject : public DataObject {
73 public:
74  void create(DataType data);
75  typename AccessObject::pointer getAccess(accessType type);
76 protected:
78 
79  virtual void free(ExecutionDevice::pointer device) override;
80 
81  virtual void freeAll() override;
82 
84 
85 private:
86  // AccessObject needs to be friends with SimpleDataObject, so that it can reach the accessFinished method
87  friend AccessObject;
88 
89 };
90 
91 
92 template <class DataType, class AccessObject>
94 
95 }
96 
97 template <class DataType, class AccessObject>
99 
100 }
101 
102 template <class DataType, class AccessObject>
104  mData = data;
105 }
106 
107 template <class DataType, class AccessObject>
109 
110  blockIfBeingWrittenTo();
111 
112  if(type == ACCESS_READ_WRITE) {
113  blockIfBeingAccessed();
114  {
115  std::lock_guard<std::mutex> lock(mDataIsBeingWrittenToMutex);
116  mDataIsBeingWrittenTo = true;
117  }
118  updateModifiedTimestamp();
119  }
120 
121 
122  {
123  std::lock_guard<std::mutex> lock(mDataIsBeingAccessedMutex);
124  mDataIsBeingAccessed = true;
125  }
126 
127  typename AccessObject::pointer accessObject(new AccessObject(&mData, std::static_pointer_cast<SimpleDataObject<DataType>>(mPtr.lock())));
128  return std::move(accessObject);
129 }
130 
131 template <class DataType, class AccessObject>
133 
134 }
135 
136 
137 
138 } // end namespace
139 
140 #endif //SIMPLEDATAOBJECT_HPP
fast::ExecutionDevice::pointer
std::shared_ptr< ExecutionDevice > pointer
Definition: ExecutionDevice.hpp:11
fast::DataType
DataType
Definition: DataTypes.hpp:38
fast::DataAccess::release
void release()
Definition: SimpleDataObject.hpp:60
fast::SimpleDataObject
Definition: SimpleDataObject.hpp:24
fast::DataAccess
Definition: SimpleDataObject.hpp:21
fast
Definition: AffineTransformation.hpp:7
ACCESS_READ_WRITE
@ ACCESS_READ_WRITE
Definition: Access.hpp:5
fast::DataAccess::mData
DataType * mData
Definition: SimpleDataObject.hpp:38
fast::DataAccess::setData
void setData(const DataType &data)
Definition: SimpleDataObject.hpp:55
Access.hpp
fast::DataAccess::pointer
std::unique_ptr< DataAccess< DataType > > pointer
Definition: SimpleDataObject.hpp:36
fast::DataAccess::getData
DataType getData()
Definition: SimpleDataObject.hpp:50
fast::DataAccess::~DataAccess
~DataAccess()
Definition: SimpleDataObject.hpp:65
fast::SimpleDataObject::getAccess
AccessObject::pointer getAccess(accessType type)
Definition: SimpleDataObject.hpp:108
fast::SimpleDataObject::SimpleDataObject
SimpleDataObject()
Definition: SimpleDataObject.hpp:132
fast::SimpleDataObject::free
virtual void free(ExecutionDevice::pointer device) override
Definition: SimpleDataObject.hpp:93
fast::DataObject
Definition: DataObject.hpp:13
accessType
accessType
Definition: Access.hpp:5
fast::SimpleDataObject::create
void create(DataType data)
Definition: SimpleDataObject.hpp:103
fast::DataAccess::DataAccess
DataAccess(DataType *data, std::shared_ptr< SimpleDataObject< DataType > > dataObject)
Definition: SimpleDataObject.hpp:44
fast::DataAccess::mDataObject
std::shared_ptr< SimpleDataObject< DataType > > mDataObject
Definition: SimpleDataObject.hpp:39
fast::SimpleDataObject::freeAll
virtual void freeAll() override
Definition: SimpleDataObject.hpp:98
DataObject.hpp
fast::SimpleDataObject::mData
DataType mData
Definition: SimpleDataObject.hpp:83