The Sparta Modeling Framework
Loading...
Searching...
No Matches
ResourceFactory.hpp
1// <ResourceFactory> -*- C++ -*-
2
3
4#pragma once
5
6#include <iostream>
7#include <string>
8#include <ostream>
9#include <stdexcept>
10#include <vector>
11#include <memory>
12#include <map>
13
16#include "sparta/utils/Utils.hpp"
17
18
19namespace sparta
20{
21
22 class ResourceTreeNode;
23
48 {
49 public:
50
55
61 virtual std::string getResourceType() const = 0;
62
68 virtual std::string getResourceTypeRaw() const = 0;
69
82
88 virtual void deleteParameters(ParameterSet* params) = 0;
89
99
109
116
123
137 const ParameterSet* params) = 0;
138
144 virtual void deleteResource(Resource* res) = 0;
145
156 virtual void bindEarly(TreeNode* node) = 0;
157
166 virtual void bindLate(TreeNode* node) = 0;
167 };
168
169 GENERATE_HAS_ATTR(name)
170 GENERATE_HAS_ATTR(ParameterSet)
171
172
212 template<typename ResourceT, typename ParamsT=typename ResourceT::ParameterSet>
214 {
215 public:
216
217 typedef ResourceT resource_type;
218 typedef ParamsT params_type;
219
220 ResourceFactory(const ResourceFactory& rhp) = delete;
221 ResourceFactory& operator=(const ResourceFactory& rhp) = delete;
222
224 // ParamsT must be a subclass of sparta::ParameterSet.
225 // This check will fail if ParamsT is not a subclass of
226 // sparta::ParameterSet.
227 sparta::ParameterSet* rps = (ParamsT*)0;
228 (void) rps;
229 };
230 ~ResourceFactory() {};
231
232 virtual std::string getResourceType() const override {
233 return getResourceType_<ResourceT>();
234 }
235
236 virtual std::string getResourceTypeRaw() const override {
237 return typeid(ResourceT).name();
238 }
239
240 virtual ParameterSet* createParameters(TreeNode* node) override {
241 sparta_assert(node);
242 return new ParamsT(node);
243 }
244
245 virtual void deleteParameters(ParameterSet* params) override {
246 sparta_assert(params, "It is invalid to call deleteParameters with null params");
247 delete params;
248 }
249
250 // This can easily be overridden to add a subtree
251 virtual void createSubtree(sparta::ResourceTreeNode* n) override {
252 (void) n;
253 }
254
255 virtual void deleteSubtree(sparta::ResourceTreeNode* n) override {
256 (void) n;
257 }
258
259 // This can easily be overridden for additional building or configuring
260 virtual void onBuilding(sparta::ResourceTreeNode* n) override {
261 (void) n;
262 }
263
264 virtual void onConfiguring(sparta::ResourceTreeNode* n) override {
265 (void) n;
266 }
267
276 const ParameterSet* params) override {
277 // Cast Parameters to expected type
278 ParamsT const * sps; // specific parameter set
279 sps = dynamic_cast<ParamsT const *>(params);
280 if(nullptr == sps){ // Specific parameter set must have casted correctly
281 throw SpartaException("Failed to cast ParameterSet ")
282 << params << " to type " << typeid(ParamsT).name()
283 << " when constructing resource for node " << node->getLocation();
284 }
285
286 return new ResourceT(node, sps); // Construct the new resource
287 }
288
289 void deleteResource(Resource* res) override final {
290 sparta_assert(res, "It is invalid to call deleteResource with a null resource");
291 delete res;
292 }
293
294 void bindEarly(TreeNode*) override {;}
295
296 void bindLate(TreeNode*) override {;}
297
298 private:
299
300 template <typename RT>
301 typename std::enable_if<has_attr_name<RT>::value, std::string>::type
302 getResourceType_() const {
303 return ResourceT::name;
304 }
305
306 template <typename RT>
307 typename std::enable_if<!has_attr_name<RT>::value, std::string>::type
308 getResourceType_() const {
309 // Generate a friendly verbose error
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 "
314 "this Resource.");
315 return "";
316 }
317
318 };
319
324 {
325 public:
326 // Typedef for the internal map type
327 typedef std::map<std::string, std::unique_ptr<sparta::ResourceFactoryBase>> ResourceFactoryMap;
328
336 template <typename ResourceFactoryT>
338 std::unique_ptr<sparta::ResourceFactoryBase> fact(new ResourceFactoryT());
339 sparta_assert(fact != nullptr);
340 std::string name = fact->getResourceType();
341 auto itr = factories_.find(name);
342 if(itr != factories_.end()){
343 throw sparta::SpartaException("Cannot reregister ResourceFactory named \"")
344 << name << "\" because there is already a resource registered by that name";
345 }
346 factories_[name] = std::move(fact);
347 max_res_name_len_ = std::max<uint32_t>(max_res_name_len_, name.size());
348 }
349
354
357 bool hasResource(const std::string& name) const noexcept;
358
365 std::string renderResources(bool one_per_line=true);
366
373 ResourceFactoryMap::const_iterator begin() const {
374 return factories_.begin();
375 }
376
380 ResourceFactoryMap::const_iterator end() const {
381 return factories_.end();
382 }
383
384 private:
385
387 std::map<std::string, std::unique_ptr<sparta::ResourceFactoryBase>> factories_;
388 uint32_t max_res_name_len_ = 0;
389 };
390
391} // namespace sparta
392
393// __RESOURCE_FACTORY__
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.
Definition Resource.hpp:44
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::string getLocation() const override final
Macros for handling exponential backoff.