Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Instrument.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5namespace Stroika::Frameworks::SystemPerformance {
6
7 /*
8 ********************************************************************************
9 ************************ SystemPerformance::Instrument *************************
10 ********************************************************************************
11 */
12 inline Instrument::Instrument (Instrument&& src) noexcept
13 : Instrument{move (src.fInstrumentName_), move (src.fCaptureRep_), move (src.fCapturedMeasurementTypes_),
14 move (src.fType2MeasurementTypes_), move (src.fObjectVariantMapper_)}
15 {
16 }
17 inline Instrument::Instrument (const Instrument& src)
18 : Instrument{src.fInstrumentName_, src.fCaptureRep_->Clone (), src.fCapturedMeasurementTypes_, src.fType2MeasurementTypes_, src.fObjectVariantMapper_}
19 {
20 }
21 inline MeasurementSet Instrument::Capture ()
22 {
23 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
24 AssertNotNull (fCaptureRep_);
25 return fCaptureRep_->Capture ();
26 }
27 template <>
28 inline VariantValue Instrument::CaptureOneMeasurement (Range<TimePointSeconds>* measurementTimeOut)
29 {
30 MeasurementSet ms = Capture ();
31 if (measurementTimeOut != nullptr) {
32 *measurementTimeOut = ms.fMeasuredAt;
33 }
34 for (const auto& ii : ms.fMeasurements) {
35 return ii.fValue;
36 }
37 AssertNotReached (); // only use this on insturments with one result returned
38 return VariantValue{};
39 }
40 template <typename T>
41 inline T Instrument::CaptureOneMeasurement (Range<TimePointSeconds>* measurementTimeOut)
42 {
43 // This function is typically template specialized by Instruments to avoid the round trip through VariantValues, but this is
44 // logically correct (just slower).
45 return MeasurementAs<T> (CaptureOneMeasurement<VariantValue> (measurementTimeOut));
46 }
47 template <typename T>
48 inline T Instrument::MeasurementAs (const Measurement& m) const
49 {
50 Debug::AssertExternallySynchronizedMutex::ReadContext readLock{fThisAssertExternallySynchronized_};
51 Require (fType2MeasurementTypes_.ContainsKey (typeid (decay_t<T>)));
52 Require (m.fType == fType2MeasurementTypes_[typeid (decay_t<T>)]);
53 return fObjectVariantMapper_.ToObject<T> (m.fValue);
54 }
55 template <typename T>
56 optional<T> Instrument::MeasurementAs (const MeasurementSet& m) const
57 {
58 Debug::AssertExternallySynchronizedMutex::ReadContext readLock{fThisAssertExternallySynchronized_};
59 Require (fType2MeasurementTypes_.ContainsKey (typeid (decay_t<T>)));
60 MeasurementType mt = fType2MeasurementTypes_[typeid (decay_t<T>)];
61 if (auto i = m.fMeasurements.Find ([=] (const Measurement& m) { return m.fType == mt; })) {
62 return fObjectVariantMapper_.ToObject<T> (i->fValue);
63 }
64 return nullopt;
65 }
66 inline bool Instrument::operator== (const Instrument& rhs) const
67 {
68 Debug::AssertExternallySynchronizedMutex::ReadContext readLock{fThisAssertExternallySynchronized_};
69 return fInstrumentName_ == rhs.fInstrumentName_;
70 }
71
72}
#define AssertNotNull(p)
Definition Assertions.h:333
#define AssertNotReached()
Definition Assertions.h:355
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...