The Sparta Modeling Framework
Loading...
Searching...
No Matches
Resource.hpp
Go to the documentation of this file.
1// <Resource.hpp> -*- C++ -*-
2
9#pragma once
10
11#include <string>
12#include <ostream>
13#include <type_traits>
14
21#include "sparta/utils/Utils.hpp"
22
23namespace sparta {
24
25 class TreeNode;
26
31 {
32 public:
33 // Empty for now
34 };
35
36
43 {
45 friend class TreeNode;
46
47 public:
48
56
67 const std::string& name);
68
78 Resource(const std::string & name,
79 const Clock * clk);
80
82 virtual ~Resource();
83
87 const Clock * getClock() const;
88
92 Scheduler * getScheduler(const bool must_exist = true) const;
93
97 std::string getName() const;
98
104
109 const TreeNode* getContainer() const;
110
115
120
127 virtual void addLink(TreeNode *node, const std::string &label)
128 {
129 (void)node; (void)label;
130 /* Resource that do not overload this method does not use the links */
131 }
132
138 virtual void activateLink(const std::string &label)
139 {
140 (void)label;
141 /* Resource that do not overload this method does not use the links */
142 }
143
145 Resource(const Resource &) = delete;
146 Resource &operator=(const Resource &) = delete;
147
148 private:
149
158 virtual void simulationTerminating_();
159
174 virtual void validatePostRun_(const PostRunValidationInfo& info) const;
175
196 virtual void dumpDebugContent_(std::ostream& output) const;
197
210 virtual void onStartingTeardown_();
211
217 virtual void onBindTreeEarly_() {;}
218
224 virtual void onBindTreeLate_() {;}
225
229 TreeNode* res_container_;
230
234 std::string name_;
235
239 const Clock * clk_;
240 };
241
246 GENERATE_HAS_ATTR(getClock)
247
248
252 {
256 void appendTickData_(std::ostream& ss, Scheduler * sched)
257 {
258 if(sched) {
259 ss << "tick: " << sched->getCurrentTick();
260 }
261 else {
262 ss << "(no scheduler associated)";
263 }
264 }
265
269 void appendClockData_(std::ostream& ss, const Clock* clk)
270 {
271 Scheduler * sched = nullptr;
272 if(clk){
273 ss << "at cycle: " << clk->currentCycle() << " ";
274 sched = clk->getScheduler();
275 }else{
276 ss << "(no clock associated) ";
277 }
278 // also always include the tick data along with the clock.
279 appendTickData_(ss, sched);
280 }
281 public:
282
283
287 template <class CTXT>
288 std::string getContextDescription(CTXT* ctxt,
289 typename std::enable_if<!std::is_base_of<Resource, CTXT>::value
290 && !std::is_base_of<TreeNode, CTXT>::value
291 && !has_attr_getClock<CTXT>::value>::type* dummy = nullptr) {
292 (void) ctxt;
293 (void) dummy;
294 std::stringstream ss;
295 ss << "(from non-sparta context at ";
296 appendTickData_(ss, nullptr);
297 ss << ")";
298
299 return ss.str();
300 }
301
310 template <class CTXT>
311 std::string getContextDescription(CTXT* ctxt,
312 typename std::enable_if<!std::is_base_of<Resource, CTXT>::value
313 && !std::is_base_of<TreeNode, CTXT>::value
314 && has_attr_getClock<CTXT>::value>::type* dummy = 0) {
315 (void) dummy;
316 std::stringstream ss;
317 static_assert(std::is_pointer<decltype(ctxt->getClock())>::value,
318 "Type of this pointer is a class having a getClock method, but this "
319 "method does not return a pointer to a sparta::Clock");
320 static_assert(std::is_base_of<sparta::Clock,
321 typename std::remove_pointer<typename std::remove_cv<decltype(ctxt->getClock())>::type>::type>::value != false,
322 "Type of this pointer is a class having a getClock method, but this "
323 "method doe not return a sparta::Clock");
324 auto clock = ctxt->getClock();
325 // write the clock and tick to the stream.
326 appendClockData_(ss, clock);
327
328 return ss.str();
329 }
330
334 template <class CTXT>
336 typename std::enable_if<std::is_base_of<Resource, CTXT>::value>::type* dummy = 0) {
337 (void) dummy;
338 std::stringstream ss;
339 const ResourceContainer* rc = ctxt->getResourceContainer();
340 if(!rc){
341 ss << "(within uncontained resource)";
342 }else{
343 ss << "within resource at: " << rc->getLocation() << " ";
344 const sparta::Clock* clock = ctxt->getClock();
345 // write the clock and tick to the stream.
346 appendClockData_(ss, clock);
347
348 }
349 return ss.str();
350 }
351
355 template <class CTXT>
357 typename std::enable_if<std::is_base_of<TreeNode, CTXT>::value>::type* dummy = 0) {
358 (void) dummy;
359 std::stringstream ss;
360 if(!ctxt){
361 ss << "(within null treenode)"; // This should not happen, but could
362 }else{
363 ss << "within TreeNode: " << ctxt->getLocation() << " ";
364 const sparta::Clock* clock = ctxt->getClock();
365 // write the clock and tick to the stream.
366 appendClockData_(ss, clock);
367 }
368 return ss.str();
369 }
370 };
371
378 #define ADD_CONTEXT_INFORMATION(ex, thisptr) \
379 sparta::AssertContext ac; \
380 ex << " " << ac.getContextDescription<typename std::remove_pointer<decltype(thisptr)>::type>(thisptr);
381
401 #define sparta_assert_context(e, insertions) \
402 if(__builtin_expect(!(e), 0)) { sparta::SpartaException ex(std::string(#e) + ": " ); \
403 ex << insertions; \
404 ADD_FILE_INFORMATION(ex, __FILE__, __LINE__); \
405 ADD_CONTEXT_INFORMATION(ex, this); \
406 throw ex; }
407
408
409} // namespace sparta
File that defines the Clock class.
Object with a name which holds a Resource.
A simple time-based, event precedence based scheduler.
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
Creates a helper traits class for determining whehther a type has a member named getClock.
Definition Resource.hpp:252
std::string getContextDescription(const sparta::Resource *ctxt, typename std::enable_if< std::is_base_of< Resource, CTXT >::value >::type *dummy=0)
Handle case where pointer is a subclass of (or is a) sparta::Resource.
Definition Resource.hpp:335
std::string getContextDescription(const sparta::TreeNode *ctxt, typename std::enable_if< std::is_base_of< TreeNode, CTXT >::value >::type *dummy=0)
Handle case where pointer is a subclass of (or is a) sparta::TreeNode.
Definition Resource.hpp:356
std::string getContextDescription(CTXT *ctxt, typename std::enable_if<!std::is_base_of< Resource, CTXT >::value &&!std::is_base_of< TreeNode, CTXT >::value &&!has_attr_getClock< CTXT >::value >::type *dummy=nullptr)
Handle default case where pointer is not a sparta::Resource.
Definition Resource.hpp:288
std::string getContextDescription(CTXT *ctxt, typename std::enable_if<!std::is_base_of< Resource, CTXT >::value &&!std::is_base_of< TreeNode, CTXT >::value &&has_attr_getClock< CTXT >::value >::type *dummy=0)
Handle case where pointer is a not subclass of (and not a) sparta::Resource, but still has a getClock...
Definition Resource.hpp:311
A representation of simulated time.
Definition Clock.hpp:44
Cycle currentCycle() const
Get the current cycle (uses current tick from the Scheduler)
Definition Clock.hpp:169
Scheduler * getScheduler() const
Definition Clock.hpp:289
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...
Information describing the type of validation being done.
Definition Resource.hpp:31
PhasedObject which can hold 0 or 1 Resource pointers to an associatedresource. Contains logic for set...
The is the base class for all types of resources used by the SPARTA framework.
Definition Resource.hpp:43
Scheduler * getScheduler(const bool must_exist=true) const
virtual void addLink(TreeNode *node, const std::string &label)
Definition Resource.hpp:127
ResourceContainer * getResourceContainer()
Gets the ResourceContainer for this resource (if any)
const Clock * getClock() const
TreeNode * getContainer()
Gets the TreeNode (container) for this resource (if any)
std::string getName() const
virtual ~Resource()
Destroy!
Resource(const Resource &)=delete
Disallow copying.
Resource(const std::string &name, const Clock *clk)
Construct a Resource with the given name and clock having NO association with a resource container....
const ResourceContainer * getResourceContainer() const
Gets the ResourceContainer for this resource (if any)
Resource(TreeNode *rc, const std::string &name)
Construct resource with a specific name and resource container.
friend class TreeNode
Allow TreeNode access to onStartingTeardown_.
Definition Resource.hpp:45
Resource(TreeNode *rc)
Construct resource with a resource container.
virtual void activateLink(const std::string &label)
Definition Resource.hpp:138
const TreeNode * getContainer() const
Gets the TreeNode (container) for this resource (if any)
A class that lets you schedule events now and in the future.
Tick getCurrentTick() const noexcept
The current tick the Scheduler is working on or just finished.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::string getLocation() const override final
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
Macros for handling exponential backoff.