Iuvo Unity 0.5.1
This is library containing a variety of helper classes and extension methods for the Unity gane engine.
GitHub | Iuvo Unity Updates | Creator
Loading...
Searching...
No Matches
IuvoUnity.DataStructs.Context Class Reference
Inheritance diagram for IuvoUnity.DataStructs.Context:
IuvoUnity.BaseClasses.IDataStructBase IuvoUnity.Interfaces.IContext IuvoUnity.BaseClasses.IDataStructBase IuvoUnity.BaseClasses.IuvoInterfaceBase

Public Member Functions

 Context ()
 Context (ref ConcurrentDictionary< Enum, object > contextDictionary)
 Context (ref ConcurrentDictionary< Enum, object > contextDictionary, ref ConcurrentDictionary< Enum, object > eventDictionary)
void Set< T > (Enum key, T value)
Get< T > (Enum key)
bool TryGet< T > (Enum key, out T value)
bool Has (Enum key)
bool Remove (Enum key)
void ClearData ()
Context CloneContext ()
void MergeContexts (Context other, bool overwrite=true)
void RegisterEvent (Enum key, FlexibleEvent evt)
void RegisterEvent< T > (Enum key, FlexibleEvent< T > evt)
bool TryGetEvent (Enum key, out FlexibleEvent evt)
bool TryGetEvent< T > (Enum key, out FlexibleEvent< T > evt)
void InvokeEvent (Enum key)
void InvokeEvent< T > (Enum key, T value)
bool HasEvent (Enum key)
bool RemoveEvent (Enum key)
void ClearEvents ()
void Dispose ()

Private Attributes

ConcurrentDictionary< Enum, object > _data = new ConcurrentDictionary<Enum, object>()
ConcurrentDictionary< Enum, object > _eventMap = new ConcurrentDictionary<Enum, object>()

Constructor & Destructor Documentation

◆ Context() [1/3]

IuvoUnity.DataStructs.Context.Context ( )
97 {
98
99 }

Referenced by CloneContext(), and MergeContexts().

◆ Context() [2/3]

IuvoUnity.DataStructs.Context.Context ( ref ConcurrentDictionary< Enum, object > contextDictionary)
102 {
103 _data = new ConcurrentDictionary<Enum, object>(contextDictionary);
104 }

References _data.

◆ Context() [3/3]

IuvoUnity.DataStructs.Context.Context ( ref ConcurrentDictionary< Enum, object > contextDictionary,
ref ConcurrentDictionary< Enum, object > eventDictionary )
107 {
108 _data = new ConcurrentDictionary<Enum, object>(contextDictionary);
109 _eventMap = new ConcurrentDictionary<Enum, object>(eventDictionary);
110 }

References _data, and _eventMap.

Member Function Documentation

◆ ClearData()

void IuvoUnity.DataStructs.Context.ClearData ( )
155=> _data.Clear();

References _data.

Referenced by Dispose().

◆ ClearEvents()

void IuvoUnity.DataStructs.Context.ClearEvents ( )
242 {
243 foreach (var kvp in _eventMap)
244 {
245 if (kvp.Value is FlexibleEvent fe)
246 {
247 fe.RemoveAllFlexibleEventListeners();
248 }
249 else if (kvp.Value is System.IDisposable disposable)
250 {
251 disposable.Dispose();
252 }
253 }
254 _eventMap.Clear();
255
256 }

References _eventMap.

Referenced by Dispose().

◆ CloneContext()

Context IuvoUnity.DataStructs.Context.CloneContext ( )
159 {
160 var newDict = new ConcurrentDictionary<Enum, object>(_data);
161 var newEventDict = new ConcurrentDictionary<Enum, object>(_eventMap);
162 return new Context(ref newDict, ref newEventDict);
163 }

References _data, _eventMap, and Context().

◆ Dispose()

void IuvoUnity.DataStructs.Context.Dispose ( )

Implements IuvoUnity.Interfaces.IContext.

259 {
260 ClearData();
261 ClearEvents();
262 }

References ClearData(), and ClearEvents().

◆ Get< T >()

T IuvoUnity.DataStructs.Context.Get< T > ( Enum key)
125 {
126 if (!ContextKeyGroups.IsValidKey(key))
127 throw new InvalidOperationException($"Invalid key type: {key.GetType().Name}");
128
129 if (_data.TryGetValue(key, out var value) && value is T t)
130 return t;
131
132 throw new KeyNotFoundException($"Key {key} not found or wrong type");
133 }

References _data, and IuvoUnity.DataStructs.ContextKeyGroups.IsValidKey().

◆ Has()

bool IuvoUnity.DataStructs.Context.Has ( Enum key)
153=> _data.ContainsKey(key);

References _data.

◆ HasEvent()

bool IuvoUnity.DataStructs.Context.HasEvent ( Enum key)
239=> _eventMap.ContainsKey(key);

References _eventMap.

◆ InvokeEvent()

void IuvoUnity.DataStructs.Context.InvokeEvent ( Enum key)
224 {
225 if (TryGetEvent(key, out FlexibleEvent evt))
226 {
227 evt.Invoke();
228 }
229 }

References TryGetEvent().

◆ InvokeEvent< T >()

void IuvoUnity.DataStructs.Context.InvokeEvent< T > ( Enum key,
T value )
232 {
233 if (TryGetEvent<T>(key, out FlexibleEvent<T> evt))
234 {
235 evt.Invoke(value);
236 }
237 }

References TryGetEvent().

◆ MergeContexts()

void IuvoUnity.DataStructs.Context.MergeContexts ( Context other,
bool overwrite = true )
166 {
167 foreach (var kvp in other._data)
168 {
169 if (overwrite || !_data.ContainsKey(kvp.Key))
170 {
171 _data[kvp.Key] = kvp.Value;
172 }
173 }
174
175 foreach (var kvp in other._eventMap)
176 {
177 if (overwrite || !_eventMap.ContainsKey(kvp.Key))
178 {
179 _eventMap[kvp.Key] = kvp.Value;
180 }
181 }
182
183 }

References _data, _eventMap, and Context().

◆ RegisterEvent()

void IuvoUnity.DataStructs.Context.RegisterEvent ( Enum key,
FlexibleEvent evt )
187 {
188 if (!ContextKeyGroups.IsValidKey(key)) return;
189 _eventMap[key] = evt;
190 }

References _eventMap, and IuvoUnity.DataStructs.ContextKeyGroups.IsValidKey().

◆ RegisterEvent< T >()

193 {
194 if (!ContextKeyGroups.IsValidKey(key)) return;
195 _eventMap[key] = evt;
196 }

References _eventMap, and IuvoUnity.DataStructs.ContextKeyGroups.IsValidKey().

◆ Remove()

bool IuvoUnity.DataStructs.Context.Remove ( Enum key)
154=> _data.TryRemove(key, out _);

References _data.

◆ RemoveEvent()

bool IuvoUnity.DataStructs.Context.RemoveEvent ( Enum key)
240=> _eventMap.TryRemove(key, out _);

References _eventMap.

◆ Set< T >()

void IuvoUnity.DataStructs.Context.Set< T > ( Enum key,
T value )
114 {
115 if (!ContextKeyGroups.AllowedKeys.Contains(key.GetType()))
116 {
117 IuvoDebug.DebugLogWarning($"Key {key} not in approved ContextKey groups.");
118 return;
119 }
120
121 _data[key] = value;
122 }

References _data, IuvoUnity.DataStructs.ContextKeyGroups.AllowedKeys, and IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ TryGet< T >()

bool IuvoUnity.DataStructs.Context.TryGet< T > ( Enum key,
out T value )
136 {
137 if (!ContextKeyGroups.IsValidKey(key))
138 {
139 value = default;
140 return false;
141 }
142
143 if (_data.TryGetValue(key, out var obj) && obj is T t)
144 {
145 value = t;
146 return true;
147 }
148
149 value = default;
150 return false;
151 }

References _data, and IuvoUnity.DataStructs.ContextKeyGroups.IsValidKey().

◆ TryGetEvent()

bool IuvoUnity.DataStructs.Context.TryGetEvent ( Enum key,
out FlexibleEvent evt )
199 {
200 if (_eventMap.TryGetValue(key, out var val) && val is FlexibleEvent e)
201 {
202 evt = e;
203 return true;
204 }
205
206 evt = null;
207 return false;
208 }

References _eventMap.

Referenced by InvokeEvent(), and InvokeEvent< T >().

◆ TryGetEvent< T >()

bool IuvoUnity.DataStructs.Context.TryGetEvent< T > ( Enum key,
out FlexibleEvent< T > evt )
211 {
212 if (_eventMap.TryGetValue(key, out var val) && val is FlexibleEvent<T> e)
213 {
214 evt = e;
215 return true;
216 }
217
218 evt = null;
219 return false;
220 }

References _eventMap.

Member Data Documentation

◆ _data

ConcurrentDictionary<Enum, object> IuvoUnity.DataStructs.Context._data = new ConcurrentDictionary<Enum, object>()
private

◆ _eventMap

ConcurrentDictionary<Enum, object> IuvoUnity.DataStructs.Context._eventMap = new ConcurrentDictionary<Enum, object>()
private

The documentation for this class was generated from the following file:
  • D:/Unity/IuvoUnityCore/Assets/IuvoUnity/Runtime/DataStructs/Context.cs