The Sparta Modeling Framework
Loading...
Searching...
No Matches
sparta::ResourceContainer Class Referenceabstract

PhasedObject which can hold 0 or 1 Resource pointers to an associatedresource. Contains logic for setting and getting the associated Resource. More...

#include <ResourceContainer.hpp>

Inheritance diagram for sparta::ResourceContainer:
Collaboration diagram for sparta::ResourceContainer:

Public Member Functions

 ResourceContainer ()
 Consturct with a null, unlocked resource.
 
 ResourceContainer (const ResourceContainer &)=delete
 Copy construction disbled.
 
 ResourceContainer (ResourceContainer &&)=default
 Move constructor.
 
virtual ~ResourceContainer ()
 Destructor.
 
ResourcegetResource ()
 Gets the resource contained by this node if any. May only be called after finalization begins or during teardown.
 
const ResourcegetResource () const
 Const variant of getResource.
 
bool hasResource () const
 Determines if this node has a resource. This method exists in case the TreeNode is being explored by a tool or interactive UI. Typical TreeNode clients (such as Resources) will assume that there is a resource if they are expecting one.
 
template<class T , typename = typename std::enable_if<std::is_pointer<T>::value>::type>
const T getResourceAs () const
 Gets the resource contained by this node (if any) as the given type.
 
template<class T , typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
const T * getResourceAs () const
 Overload of getResourceAs for const access with a non-pointer template type.
 
template<class T , typename = typename std::enable_if<std::is_pointer<T>::value>::type>
getResourceAs ()
 Non-const overload of getResourceAs.
 
template<class T , typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
T * getResourceAs ()
 Non-const overload of getResourceAs.
 
virtual std::string getResourceType () const
 Gets the typename of the resource that this node will eventually contain.
 
virtual std::string getResourceTypeRaw () const
 Gets the typename of the resource that this node will eventually contain.
 
virtual const ClockgetClock ()=0
 Gets the clock associated with this ResourceContainer, if any.
 
- Public Member Functions inherited from sparta::PhasedObject
 PhasedObject (PhasedObject &&)=default
 
virtual const std::string & getName () const =0
 Gets the name of this node.
 
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 methods such as getChild if this is fully attached to the device tree.
 
TreePhase getPhase () const
 Gets the trees current phase.
 
virtual bool isBuilding () const
 Is this node (and thus the entire tree above and below it) currently in the TREE_BUILDING phase.
 
virtual bool isBuilt () const
 Is this node (and thus the entire tree above it) "built". Checks that getPhase has passed TREE_BUILDING.
 
virtual bool isConfigured () const
 Is this node (and thus the entire tree above it) "configured". Checks that getPhase has passed TREE_CONFIGURING (i.e. FINALIZED, FINALIZING, TEARDOWN, etc).
 
virtual bool isConfiguring () const
 Is this node (and thus the entire tree above it) currently in the TREE_CONFIGURING phase.
 
virtual bool isFinalizing () const
 Is this node (and thus the entire tree above it) "finalized".
 
virtual bool isFinalized () 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.
 

Protected Member Functions

std::string getResourceTypeName_ () const
 Gets the rtti type name (demangled) of the resource type held by this container. If there is no resource held, returns empty string.
 
void setResource_ (Resource *r)
 Allows subclasses to assign the resource associated with this node.
 
void unsetResource_ ()
 Allows a resource to unset the resource set with setResource_.
 
void lockResource_ ()
 Allows subclasses to assign the resource associated with this node.
 
ResourcegetResource_ () noexcept
 Returns the currently held resource of this node (if any). This method can be called at any time.
 
const ResourcegetResource_ () const noexcept
 Const variant of getResource_.
 
- Protected Member Functions inherited from sparta::PhasedObject
void setPhase_ (TreePhase phase)
 Sets the current phase.
 

Friends

class Resource
 Allow resource to invoke setResource_ and unsetResource_.
 

Additional Inherited Members

- Public Types inherited from sparta::PhasedObject
enum  TreePhase {
  TREE_BUILDING = 0 , TREE_CONFIGURING = 1 , TREE_FINALIZING = 2 , TREE_FINALIZED = 3 ,
  TREE_TEARDOWN = 4
}
 Current phase of tree construction (applies to node and entire tree). More...
 

Detailed Description

PhasedObject which can hold 0 or 1 Resource pointers to an associatedresource. Contains logic for setting and getting the associated Resource.

The main motivation for decoupling this from TreeNode is to break a dependency loop with Resource, which attaches and detaches itself with a ResourceContainer at construction and destruction respectively.

Has the concept of a locked resource, which means it cannot be unset until the TEARDOWN phase. This prohibits the premature destruction of Resources.

Definition at line 33 of file ResourceContainer.hpp.

Constructor & Destructor Documentation

◆ ResourceContainer()

sparta::ResourceContainer::ResourceContainer ( )
inline

Consturct with a null, unlocked resource.

Definition at line 45 of file ResourceContainer.hpp.

◆ ~ResourceContainer()

virtual sparta::ResourceContainer::~ResourceContainer ( )
inlinevirtual

Destructor.

Note
Does not free resources

Definition at line 65 of file ResourceContainer.hpp.

Member Function Documentation

◆ getClock()

virtual const Clock * sparta::ResourceContainer::getClock ( )
pure virtual

Gets the clock associated with this ResourceContainer, if any.

Returns
The clock associated with this ResourceContainer if there is one. Otherwise returns nullptr.
Warning
Do not call this in performance-critical code. The returned clock should be cached

Implemented in sparta::TreeNode.

◆ getResource() [1/2]

Resource * sparta::ResourceContainer::getResource ( )
inline

Gets the resource contained by this node if any. May only be called after finalization begins or during teardown.

Returns
Resource contained by this ResourceContainer. The same value will always be returned
Precondition
Node is finalized
Exceptions
SpartaExceptionif called before finalizing or finalized or tearing down. Throws if node does not have a resource.
See also
hasResource
getResourceAs

The goal of phase-limited access of a ResourceContainer's resource is to prevent public clients of ResourceContainer from attempting to access a resource before finalization because most resources are not created until finalization. Attempting to get the resource before this point is almost always a misuse.

This method throws if the node has no resource because its intended use is by clients who expect certain nodes to have certain resources available. This usage should not require extra null-checks by the developer. So, to keep such code concise, getResource will throw if the node has no resource. Do not call this unless the node is expected to have a resource.

The hasResource method is available to determine if the node has a resource before querying it

Definition at line 96 of file ResourceContainer.hpp.

Here is the call graph for this function:

◆ getResource() [2/2]

const Resource * sparta::ResourceContainer::getResource ( ) const
inline

Const variant of getResource.

Definition at line 122 of file ResourceContainer.hpp.

Here is the call graph for this function:

◆ getResource_() [1/2]

const Resource * sparta::ResourceContainer::getResource_ ( ) const
inlineprotectednoexcept

Const variant of getResource_.

Definition at line 343 of file ResourceContainer.hpp.

◆ getResource_() [2/2]

Resource * sparta::ResourceContainer::getResource_ ( )
inlineprotectednoexcept

Returns the currently held resource of this node (if any). This method can be called at any time.

Note that this is different from the public getResource in that it can be called at any time without the possibility of throwing an exception.

Definition at line 335 of file ResourceContainer.hpp.

◆ getResourceAs() [1/4]

template<class T , typename = typename std::enable_if<std::is_pointer<T>::value>::type>
T sparta::ResourceContainer::getResourceAs ( )
inline

Non-const overload of getResourceAs.

Returns
Resource of type T (which will be a pointer).
See also
getResourceAs

Definition at line 204 of file ResourceContainer.hpp.

Here is the call graph for this function:

◆ getResourceAs() [2/4]

template<class T , typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
T * sparta::ResourceContainer::getResourceAs ( )
inline

Non-const overload of getResourceAs.

Returns
Pointer to a const resource of type T (T*)
See also
getResourceAs

Definition at line 229 of file ResourceContainer.hpp.

◆ getResourceAs() [3/4]

template<class T , typename = typename std::enable_if<std::is_pointer<T>::value>::type>
const T sparta::ResourceContainer::getResourceAs ( ) const
inline

Gets the resource contained by this node (if any) as the given type.

Template Parameters
TType of resource to cast to. Should be sparta::Resource or a subclass
Returns
Pointer to a resource of type T. This class' resource is nullptr before node is finalizing and becomes non-null after finalization (isFinalized).
Exceptions
SpartaExceptionif called before finalizing or finalized or tearing down

Definition at line 169 of file ResourceContainer.hpp.

Here is the call graph for this function:

◆ getResourceAs() [4/4]

template<class T , typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
const T * sparta::ResourceContainer::getResourceAs ( ) const
inline

Overload of getResourceAs for const access with a non-pointer template type.

Returns
Pointer to a const resource of type T (T const *).
See also
getResourceAs

Definition at line 194 of file ResourceContainer.hpp.

◆ getResourceType()

virtual std::string sparta::ResourceContainer::getResourceType ( ) const
virtual

Gets the typename of the resource that this node will eventually contain.

Returns
non-empty string.

Returns a demangled typeid of the internal resource type

Reimplemented in sparta::ResourceTreeNode.

◆ getResourceTypeRaw()

virtual std::string sparta::ResourceContainer::getResourceTypeRaw ( ) const
virtual

Gets the typename of the resource that this node will eventually contain.

Returns
non-empty string.

Returns a raw typeid of the internal resource type

Reimplemented in sparta::ResourceTreeNode.

◆ hasResource()

bool sparta::ResourceContainer::hasResource ( ) const
inline

Determines if this node has a resource. This method exists in case the TreeNode is being explored by a tool or interactive UI. Typical TreeNode clients (such as Resources) will assume that there is a resource if they are expecting one.

Returns
true if this node has a resource that will be returned by getResource, false if not.
Exceptions
SpartaExceptionif called before finalizing or finalized or tearing down.
See also
getResource

Definition at line 147 of file ResourceContainer.hpp.

Here is the call graph for this function:

◆ lockResource_()

void sparta::ResourceContainer::lockResource_ ( )
inlineprotected

Allows subclasses to assign the resource associated with this node.

Note
There is no harm in calling this more than once
Current resource pointer (getResource) should ne non=null, but this is not required
Postcondition
resource can no longer be cleared except during teardown phase
See also
unsetResource_

Definition at line 323 of file ResourceContainer.hpp.

◆ setResource_()

void sparta::ResourceContainer::setResource_ ( Resource r)
inlineprotected

Allows subclasses to assign the resource associated with this node.

Parameters
rResource pointer to store internally. Must not be nullptr. Caller owns this resource. This class method just stores a reference to be accessed through getResource_
Precondition
Current resource pointer (getResource) must be nullptr.
Postcondition
Current resource pointer will not be nullptr
Note
Expected to be called from a sparta::Resource being constructed

Definition at line 276 of file ResourceContainer.hpp.

Here is the call graph for this function:

◆ unsetResource_()

void sparta::ResourceContainer::unsetResource_ ( )
inlineprotected

Allows a resource to unset the resource set with setResource_.

Note
This should happen during destruction
Precondition
This node has either not locked its resource or this node is in the TEARDOWN phase.
Parameters
rResource pointer to store internally. Must not be nullptr.
Postcondition
isResourceLocked_ will return true
Note
Expected to be called from a sparta::Resource being destructed

Definition at line 305 of file ResourceContainer.hpp.

Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ Resource

friend class Resource
friend

Allow resource to invoke setResource_ and unsetResource_.

Definition at line 40 of file ResourceContainer.hpp.


The documentation for this class was generated from the following file: