16#include "sparta/utils/Utils.hpp"
22 class ResourceTreeNode;
169 GENERATE_HAS_ATTR(name)
212 template<
typename ResourceT,
typename ParamsT=
typename ResourceT::ParameterSet>
217 typedef ResourceT resource_type;
218 typedef ParamsT params_type;
233 return getResourceType_<ResourceT>();
237 return typeid(ResourceT).name();
242 return new ParamsT(node);
246 sparta_assert(params,
"It is invalid to call deleteParameters with null params");
279 sps =
dynamic_cast<ParamsT
const *
>(params);
282 << params <<
" to type " <<
typeid(ParamsT).name()
283 <<
" when constructing resource for node " << node->
getLocation();
286 return new ResourceT(node, sps);
290 sparta_assert(res,
"It is invalid to call deleteResource with a null resource");
300 template <
typename RT>
301 typename std::enable_if<has_attr_name<RT>::value, std::string>::type
302 getResourceType_()
const {
303 return ResourceT::name;
306 template <
typename RT>
307 typename std::enable_if<!has_attr_name<RT>::value, std::string>::type
308 getResourceType_()
const {
310 static_assert(has_attr_name<RT>::value,
"When instantiating a subclass of sparta::ResourceFactory, "
311 "template argument ResourceT is not a complete sparta::Resource subclass: ResourceT type "
312 "template argument to sparta::ResourceFactory must have a public static \"name\" member "
313 "which is a std::string or const char* that contains an alphanumeric name representing "
327 typedef std::map<std::string, std::unique_ptr<sparta::ResourceFactoryBase>> ResourceFactoryMap;
336 template <
typename ResourceFactoryT>
338 std::unique_ptr<sparta::ResourceFactoryBase> fact(
new ResourceFactoryT());
340 std::string name = fact->getResourceType();
341 auto itr = factories_.find(name);
342 if(itr != factories_.end()){
344 << name <<
"\" because there is already a resource registered by that name";
346 factories_[name] = std::move(fact);
347 max_res_name_len_ = std::max<uint32_t>(max_res_name_len_, name.size());
373 ResourceFactoryMap::const_iterator
begin()
const {
374 return factories_.begin();
380 ResourceFactoryMap::const_iterator
end()
const {
381 return factories_.end();
387 std::map<std::string, std::unique_ptr<sparta::ResourceFactoryBase>> factories_;
388 uint32_t max_res_name_len_ = 0;
A set of sparta::Parameters per sparta::ResourceTreeNode.
File that defines the Resource class. Consider using sparta::Unit instead.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Generic container of Parameters.
Factory which can create Resources as well as the Parameter sets that can be modified before actually...
virtual void deleteSubtree(sparta::ResourceTreeNode *n)=0
Optionally deletes the TreeNodes created by createSubtee (if any).
virtual void deleteResource(Resource *res)=0
Deletes a resource created by the createResource method of this ResourceFactory.
virtual std::string getResourceType() const =0
Returns the resource type-name for this resource, demangled.
virtual void onBuilding(sparta::ResourceTreeNode *n)=0
Hook for additional building on ResourceTreeNode constructing.
virtual void deleteParameters(ParameterSet *params)=0
Deletes a ParameterSet created by the createParameters method of this ResourceFactory.
virtual Resource * createResource(TreeNode *node, const ParameterSet *params)=0
Instanitates a new Resource of the type described by this factory.
virtual void createSubtree(sparta::ResourceTreeNode *n)=0
Optionally creates a subtree of TreeNodes for this TreeNode by attaching children to this node....
virtual void bindLate(TreeNode *node)=0
Allows contents to be bound together if desired.
virtual void onConfiguring(sparta::ResourceTreeNode *n)=0
Hook for additional configuring on resource node configure.
virtual std::string getResourceTypeRaw() const =0
Returns the resource type-name for this resource as in the typeid.
virtual ~ResourceFactoryBase()
Destructor.
virtual void bindEarly(TreeNode *node)=0
Allows contents to be bound together if desired.
virtual ParameterSet * createParameters(TreeNode *node)=0
Creates a new set of parameters associated with the resource that can be created by this factory.
Templated ResourceFactoryBase implementation which can be used to trivially define Resource Factories...
virtual void createSubtree(sparta::ResourceTreeNode *n) override
Optionally creates a subtree of TreeNodes for this TreeNode by attaching children to this node....
virtual void onConfiguring(sparta::ResourceTreeNode *n) override
Hook for additional configuring on resource node configure.
virtual void deleteSubtree(sparta::ResourceTreeNode *n) override
Optionally deletes the TreeNodes created by createSubtee (if any).
virtual std::string getResourceType() const override
Returns the resource type-name for this resource, demangled.
virtual void onBuilding(sparta::ResourceTreeNode *n) override
Hook for additional building on ResourceTreeNode constructing.
virtual void deleteParameters(ParameterSet *params) override
Deletes a ParameterSet created by the createParameters method of this ResourceFactory.
Resource * createResource(TreeNode *node, const ParameterSet *params) override
Finally instantiates the resource with its set of Parameters.
void bindLate(TreeNode *) override
Allows contents to be bound together if desired.
void deleteResource(Resource *res) override final
Deletes a resource created by the createResource method of this ResourceFactory.
void bindEarly(TreeNode *) override
Allows contents to be bound together if desired.
virtual ParameterSet * createParameters(TreeNode *node) override
Creates a new set of parameters associated with the resource that can be created by this factory.
virtual std::string getResourceTypeRaw() const override
Returns the resource type-name for this resource as in the typeid.
Set of published ResourceFactories which can be referenced by name.
void addResourceFactory()
Add a resource factory by its template type.
bool hasResource(const std::string &name) const noexcept
Checks for a resource with the given resource type name.
std::string renderResources(bool one_per_line=true)
Returns a string containing all resource names known by this object separated by newlines.
sparta::ResourceFactoryBase * getResourceFactory(const std::string &name)
Returns a ResourceFactory with the given resource type name.
ResourceFactoryMap::const_iterator end() const
Get a constant end iterator to the ResourceSet.
ResourceFactoryMap::const_iterator begin() const
Get a constant begin iterator to the ResourceSet.
TreeNode subclass representing a node in the device tree which contains a single ResourceFactory and ...
The is the base class for all types of resources used by the SPARTA framework.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
std::string getLocation() const override final
Macros for handling exponential backoff.