SST  15.1.0
StructuralSimulationToolkit
timeConverter.h
1 // Copyright 2009-2025 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2025, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_TIMECONVERTER_H
13 #define SST_CORE_TIMECONVERTER_H
14 
15 #include "sst/core/serialization/serialize_impl_fwd.h"
16 #include "sst/core/sst_types.h"
17 #include "sst/core/unitAlgebra.h"
18 
19 namespace SST {
20 
21 class TimeLord;
22 
23 /**
24  A class to convert between a component's view of time and the
25  core's view of time.
26 */
28 {
29 
30  friend class TimeLord;
33 
34 public:
35  /**
36  Create a new TimeConverter object from a TimeConverter*
37  Use this to create a local TimeConverter from a TimeConverter*
38  returned by the BaseComponent and other public APIs.
39  @param tc TimeConverter to initialize factor from
40  */
41  TimeConverter(TimeConverter* tc) { factor = tc->factor; }
42 
43  [[deprecated("Use of shared TimeConverter objects is deprecated. If you're seeing this message, you likely have "
44  "changed a TimeConverter* to TimeConverter, but are still assigning it to be nullptr at the point of "
45  "this warning.")]]
46  TimeConverter(std::nullptr_t UNUSED(tc))
47  {
48  factor = 0;
49  }
50 
51  /**
52  Do not directly invoke this constructor from Components to get
53  a TimeConverter. Instead, use the BaseComponent API functions and the constructor
54  that uses a TimeConverter* to create a TimeConverter.
55  */
57 
58  /**
59  Converts from the component's view to the core's view of time.
60  @param time time to convert to core time
61  */
62  SimTime_t convertToCoreTime(SimTime_t time) const { return time * factor; }
63 
64  /**
65  Converts from the core's view to the components's view of time.
66  The result is truncated, not rounded.
67  @param time time to convert from core time
68  */
69  SimTime_t convertFromCoreTime(SimTime_t time) const { return time / factor; }
70 
71  /**
72  * @return The factor used for conversions with Core Time
73  */
74  SimTime_t getFactor() const { return factor; }
75 
76  /**
77  Resets a TimeConverter to uninitialized state (factor = 0)
78  */
79  void reset() { factor = 0; }
80 
81  /**
82  @return The period represented by this TimeConverter as a UnitAlgebra
83  */
84  UnitAlgebra getPeriod() const; // Implemented in timeLord.cc
85 
86  /**
87  * TimeConverter* returned by the core should *never* be deleted by
88  * Elements. This was moved to public due to needing to support ObjectMaps.
89  */
91 
92  /**
93  Function to check to see if the TimeConverter is initialized
94  (non-zero factor)
95 
96  @return true if TimeConverter is initialized (factor is
97  non-zero), false otherwise
98  */
99  bool isInitialized() const { return factor != 0; }
100 
101  /**
102  Conversion to bool. This will allow !tc to work to check if it
103  has been initialized (has a non-zero factor).
104 
105  @return true if TimeConverter is initialized (factor is
106  non-zero)
107  */
108  explicit operator bool() const { return factor != 0; }
109 
110 private:
111  /**
112  Factor for converting between core and component time
113  */
114  SimTime_t factor = 0;
115 
116  explicit TimeConverter(SimTime_t fact) { factor = fact; }
117 };
118 
119 template <>
121 {
122  // Function implemented in timeLord.cc
123  void operator()(TimeConverter& s, serializer& ser, ser_opt_t options);
124 
125  SST_FRIEND_SERIALIZE();
126 };
127 
128 template <>
130 {
131  // Function implemented in timeLord.cc
132  void operator()(TimeConverter*& s, serializer& ser, ser_opt_t options);
133 
134  SST_FRIEND_SERIALIZE();
135 };
136 
137 } // namespace SST
138 
139 #endif // SST_CORE_TIMECONVERTER_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
SimTime_t getFactor() const
Definition: timeConverter.h:74
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:27
~TimeConverter()
TimeConverter* returned by the core should never be deleted by Elements.
Definition: timeConverter.h:90
Base serialize class.
Definition: serialize.h:113
Definition: action.cc:18
SimTime_t convertFromCoreTime(SimTime_t time) const
Converts from the core&#39;s view to the components&#39;s view of time.
Definition: timeConverter.h:69
void reset()
Resets a TimeConverter to uninitialized state (factor = 0)
Definition: timeConverter.h:79
SimTime_t convertToCoreTime(SimTime_t time) const
Converts from the component&#39;s view to the core&#39;s view of time.
Definition: timeConverter.h:62
UnitAlgebra getPeriod() const
Definition: timeLord.cc:179
TimeConverter(TimeConverter *tc)
Create a new TimeConverter object from a TimeConverter* Use this to create a local TimeConverter from...
Definition: timeConverter.h:41
Class for creating and managing TimeConverter objects.
Definition: timeLord.h:39
TimeConverter()
Do not directly invoke this constructor from Components to get a TimeConverter.
Definition: timeConverter.h:56
Performs Unit math in full precision.
Definition: unitAlgebra.h:105
bool isInitialized() const
Function to check to see if the TimeConverter is initialized (non-zero factor)
Definition: timeConverter.h:99