Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Instrument.cs
Go to the documentation of this file.
4
6
7public abstract class Instrument
8{
10
12
13 internal static object SyncObject { get; } = new object();
14
15
16 public Meter Meter { get; }
17
18 public string Name { get; }
19
20 public string? Description { get; }
21
22 public string? Unit { get; }
23
24 public bool Enabled => _subscriptions.First != null;
25
26 public virtual bool IsObservable => false;
27
28 protected Instrument(Meter meter, string name, string? unit, string? description)
29 {
30 if (meter == null)
31 {
32 throw new ArgumentNullException("meter");
33 }
34 if (name == null)
35 {
36 throw new ArgumentNullException("name");
37 }
38 Meter = meter;
39 Name = name;
40 Description = description;
41 Unit = unit;
42 }
43
44 protected void Publish()
45 {
48 {
49 if (Meter.Disposed || !Meter.AddInstrument(this))
50 {
51 return;
52 }
54 }
55 if (list == null)
56 {
57 return;
58 }
59 foreach (MeterListener item in list)
60 {
61 item.InstrumentPublished?.Invoke(this, item);
62 }
63 }
64
66 {
68 {
69 diagNode.Value.Listener.DisableMeasurementEvents(this);
70 }
72 }
73
74 internal static void ValidateTypeParameter<T>()
75 {
77 if (typeFromHandle != typeof(byte) && typeFromHandle != typeof(short) && typeFromHandle != typeof(int) && typeFromHandle != typeof(long) && typeFromHandle != typeof(double) && typeFromHandle != typeof(float) && typeFromHandle != typeof(decimal))
78 {
80 }
81 }
82
84 {
85 oldStateStored = false;
86 if (!_subscriptions.AddIfNotExist(subscription, (ListenerSubscription s1, ListenerSubscription s2) => s1.Listener == s2.Listener))
87 {
89 _subscriptions.AddIfNotExist(subscription, (ListenerSubscription s1, ListenerSubscription s2) => s1.Listener == s2.Listener);
90 oldStateStored = listenerSubscription.Listener == subscription.Listener;
91 return listenerSubscription.State;
92 }
93 return false;
94 }
95
96 internal object DisableMeasurements(MeterListener listener)
97 {
98 return _subscriptions.Remove(new ListenerSubscription(listener), (ListenerSubscription s1, ListenerSubscription s2) => s1.Listener == s2.Listener).State;
99 }
100
101 internal virtual void Observe(MeterListener listener)
102 {
103 throw new InvalidOperationException();
104 }
105
106 internal object GetSubscriptionState(MeterListener listener)
107 {
109 {
110 if (listener == diagNode.Value.Listener)
111 {
112 return diagNode.Value.State;
113 }
114 }
115 return null;
116 }
117}
118public abstract class Instrument<T> : Instrument where T : struct
119{
120 protected Instrument(Meter meter, string name, string? unit, string? description)
121 : base(meter, name, unit, description)
122 {
123 Instrument.ValidateTypeParameter<T>();
124 }
125
126 protected void RecordMeasurement(T measurement)
127 {
128 RecordMeasurement(measurement, Instrument.EmptyTags.AsSpan());
129 }
130
132 {
134 {
135 diagNode.Value.Listener.NotifyMeasurement(this, measurement, tags, diagNode.Value.State);
136 }
137 }
138
139 protected void RecordMeasurement(T measurement, KeyValuePair<string, object?> tag)
140 {
141 OneTagBag oneTagBag = new OneTagBag(tag);
142 RecordMeasurement(measurement, MemoryMarshal.CreateReadOnlySpan(ref oneTagBag.Tag1, 1));
143 }
144
146 {
148 RecordMeasurement(measurement, MemoryMarshal.CreateReadOnlySpan(ref twoTagsBag.Tag1, 2));
149 }
150
156
157 protected void RecordMeasurement(T measurement, in TagList tagList)
158 {
160 if (tags != null)
161 {
162 RecordMeasurement(measurement, tags.AsSpan().Slice(0, tagList.Count));
163 }
164 else
165 {
166 RecordMeasurement(measurement, MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in tagList.Tag1), tagList.Count));
167 }
168 }
169}
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
object GetSubscriptionState(MeterListener listener)
void RecordMeasurement(T measurement, in TagList tagList)
Instrument(Meter meter, string name, string? unit, string? description)
Definition Instrument.cs:28
virtual void Observe(MeterListener listener)
void RecordMeasurement(T measurement)
void RecordMeasurement(T measurement, KeyValuePair< string, object?> tag1, KeyValuePair< string, object?> tag2)
void RecordMeasurement(T measurement, ReadOnlySpan< KeyValuePair< string, object?> > tags)
object DisableMeasurements(MeterListener listener)
Definition Instrument.cs:96
object EnableMeasurement(ListenerSubscription subscription, out bool oldStateStored)
Definition Instrument.cs:83
static KeyValuePair< string, object?>[] EmptyTags
Definition Instrument.cs:11
readonly DiagLinkedList< ListenerSubscription > _subscriptions
Definition Instrument.cs:9
void RecordMeasurement(T measurement, KeyValuePair< string, object?> tag1, KeyValuePair< string, object?> tag2, KeyValuePair< string, object?> tag3)
void RecordMeasurement(T measurement, KeyValuePair< string, object?> tag)
static List< MeterListener > GetAllListeners()
bool AddInstrument(Instrument instrument)
Definition Meter.cs:101
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string UnsupportedType
Definition SR.cs:40
Definition SR.cs:7