The Sparta Modeling Framework
Loading...
Searching...
No Matches
ResourceContainer.hpp
Go to the documentation of this file.
1// <ResourceContainer> -*- C++ -*-
2
8#pragma once
9
13
14namespace sparta
15{
16 class Resource;
17 class PostRunValidationInfo;
18 class Clock;
19
34 {
35 public:
36
40 friend class Resource;
41
46 resource_(nullptr),
47 resource_locked_(false),
48 num_resource_gets_(0)
49 { }
50
55
60
66 { }
67
97 if(isFinalized() == false
98 && isFinalizing() == false
99 && isTearingDown() == false){
100 throw SpartaException("Cannot call getResource on TreeNode ")
101 << getLocation() << " because it is not finalizing, finalized, or tearing down";
102 }
103 if(nullptr == resource_){
104 SpartaException ex("Cannot call getResource on TreeNode: ");
105 ex << getLocation() << " which does not have a resource.";
106 if(isFinalized()){
107 ex << " TreeNode is finalized, so it cannot possibly have a resource";
108 }else if(isFinalizing()){
109 ex << " TreeNode is finalizing, and might not have created its' resource yet."
110 << " If this TreeNode is expected to have a resource, it just hasn't been finalized yet."
111 << " If this is a DynamicResourceTreeNode, explicitly invoke finalize() on it to"
112 << " immediately create the resource";
113 }
114 throw ex;
115 }
116 return getResource_();
117 }
118
122 const Resource* getResource() const {
123 if(isFinalized() == false
124 && isFinalizing() == false
125 && isTearingDown() == false){
126 throw SpartaException("Cannot call getResource on TreeNode ")
127 << getLocation() << " because it is not finalizing, finalized, or tearing down";
128 }
129 if(nullptr == resource_){
130 throw SpartaException("Cannot call getResource on TreeNode: ")
131 << getLocation() << " which does not have a resource";
132 }
133 return getResource_();
134 }
135
147 bool hasResource() const {
148 if(isFinalized() == false
149 && isFinalizing() == false
150 && isTearingDown() == false){
151 throw SpartaException("Cannot call hasResource on TreeNode ")
152 << getLocation() << " because it is not finalizing, finalized, or tearing down";
153 }
154 return getResource_() != nullptr;
155 }
156
168 template <class T, typename = typename std::enable_if<std::is_pointer<T>::value>::type>
169 const T getResourceAs() const {
170 if(isFinalized() == false
171 && isFinalizing() == false
172 && isTearingDown() == false){
173 throw SpartaException("Cannot call getResource on TreeNode ")
174 << getLocation() << " because it is not finalizing, finalized, or tearing down";
175 }
176 if(nullptr == resource_){
177 throw SpartaException("Could not get Resource from TreeNode ")
178 << getLocation() << " because it was null. Exepcted type: " << demangle(typeid(T).name());
179 }
180 const T r = dynamic_cast<T>(resource_);
181 if(nullptr == r){
182 throw SpartaException("Could not get Resource from TreeNode ")
183 << getLocation() << " because it (" << getResourceTypeName_() << ") could not be cast to type: " << demangle(typeid(T).name());
184 }
185 return r;
186 }
187
193 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
194 const T* getResourceAs() const {
195 return getResourceAs<T*>();
196 }
197
203 template <class T, typename = typename std::enable_if<std::is_pointer<T>::value>::type>
205 if(isFinalized() == false
206 && isFinalizing() == false
207 && isTearingDown() == false){
208 throw SpartaException("Cannot call getResource on TreeNode ")
209 << getLocation() << " because it is not finalizing, finalized, or tearing down";
210 }
211 if(nullptr == resource_){
212 throw SpartaException("Could not get Resource from ResourceTreeNode \"")
213 << getLocation() << "\" because it was null. Exepcted type: " << demangle(typeid(T).name());
214 }
215 T r = dynamic_cast<T>(resource_);
216 if(nullptr == r){
217 throw SpartaException("Could not get Resource from ResourceTreeNode \"")
218 << getLocation() << "\" because it (" << getResourceTypeName_() << ") could not be cast to type: " << demangle(typeid(T).name());
219 }
220 return r;
221 }
222
228 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
230 return getResourceAs<T*>();
231 }
232
239 virtual std::string getResourceType() const;
240
247 virtual std::string getResourceTypeRaw() const;
248
256 virtual const Clock* getClock() = 0;
257
258 protected:
259
264 std::string getResourceTypeName_() const;
265
277 if(resource_locked_){
279 "Resource pointer on " << getLocation()
280 << " has been locked. It cannot be set");
281 }
282 if(resource_ != nullptr){
284 "Resource pointer on " << getLocation()
285 << " has already been set. It cannot be replaced. ");
286 }
287 if(r == nullptr){
289 "Resource pointer on " << getLocation()
290 << " cannot be assigned to nullptr. ");
291 }
292
293 resource_ = r;
294 }
295
306 if(resource_locked_ && false == isTearingDown()){
308 "Resource pointer on " << getLocation()
309 << " has been locked. It cannot be unset until teardown");
310 }
311 resource_ = nullptr;
312 }
313
324 resource_locked_ = true;
325 }
326
335 Resource* getResource_() noexcept {
336 ++num_resource_gets_;
337 return resource_;
338 }
339
343 const Resource* getResource_() const noexcept {
344 ++num_resource_gets_;
345 return resource_;
346 }
347
348 private:
349
353 Resource* resource_;
354
358 bool resource_locked_;
359
360
364
369 mutable uint32_t num_resource_gets_;
370
373 };
374
375} // namespace sparta
376
Basic Node framework in sparta device tree composite pattern.
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
#define THROW_IF_NOT_UNWINDING(exclass, msg_construct)
Utility for throwing an exception ONLY if there is not already an uncaught exception causing the stac...
A representation of simulated time.
Definition Clock.hpp:51
Object having a specific phase in the sparta construction paradigm.
virtual std::string getLocation() const =0
Returns the location of this node in device tree which can be used to navigate the device tree in met...
virtual bool isFinalizing() const
Is this node (and thus the entire tree above it) "finalized".
virtual bool isTearingDown() const
Is this node (and thus the entire tree above it) in the "teardown" phase.
virtual bool isFinalized() const
Is this node (and thus the entire tree above it) "finalized".
PhasedObject which can hold 0 or 1 Resource pointers to an associatedresource. Contains logic for set...
virtual const Clock * getClock()=0
Gets the clock associated with this ResourceContainer, if any.
ResourceContainer(ResourceContainer &&)=default
Move constructor.
T * getResourceAs()
Non-const overload of getResourceAs.
T getResourceAs()
Non-const overload of getResourceAs.
void lockResource_()
Allows subclasses to assign the resource associated with this node.
void unsetResource_()
Allows a resource to unset the resource set with setResource_.
virtual std::string getResourceType() const
Gets the typename of the resource that this node will eventually contain.
const Resource * getResource_() const noexcept
Const variant of getResource_.
const T * getResourceAs() const
Overload of getResourceAs for const access with a non-pointer template type.
const Resource * getResource() const
Const variant of getResource.
Resource * getResource_() noexcept
Returns the currently held resource of this node (if any). This method can be called at any time.
ResourceContainer()
Consturct with a null, unlocked resource.
const T getResourceAs() const
Gets the resource contained by this node (if any) as the given type.
virtual ~ResourceContainer()
Destructor.
Resource * getResource()
Gets the resource contained by this node if any. May only be called after finalization begins or duri...
bool hasResource() const
Determines if this node has a resource. This method exists in case the TreeNode is being explored by ...
ResourceContainer(const ResourceContainer &)=delete
Copy construction disbled.
virtual std::string getResourceTypeRaw() const
Gets the typename of the resource that this node will eventually contain.
void setResource_(Resource *r)
Allows subclasses to assign the resource associated with this node.
std::string getResourceTypeName_() const
Gets the rtti type name (demangled) of the resource type held by this container. If there is no resou...
The is the base class for all types of resources used by the SPARTA framework.
Definition Resource.hpp:44
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Macros for handling exponential backoff.
std::string demangle(const std::string &name) noexcept
Demangles a C++ symbol.
Definition Utils.hpp:203