replace uses of TryGetValue with the Get extension method for readability

This commit is contained in:
Gered 2013-08-22 18:56:51 -04:00
parent aac2663b77
commit 42d98d4261
5 changed files with 24 additions and 47 deletions

View file

@ -93,8 +93,7 @@ namespace Blarg.GameFramework.Entities
public Entity GetFirstWith<T>() where T : Component public Entity GetFirstWith<T>() where T : Component
{ {
EntityToComponentMap componentEntities; EntityToComponentMap componentEntities = _components.Get(typeof(T));
_components.TryGetValue(typeof(T), out componentEntities);
if (componentEntities == null) if (componentEntities == null)
return null; return null;
@ -109,8 +108,7 @@ namespace Blarg.GameFramework.Entities
{ {
if (list == null) if (list == null)
throw new ArgumentNullException("list", "Must provide a list to store matching Entity's in."); throw new ArgumentNullException("list", "Must provide a list to store matching Entity's in.");
EntityToComponentMap componentEntities; EntityToComponentMap componentEntities = _components.Get(typeof(T));
_components.TryGetValue(typeof(T), out componentEntities);
if (componentEntities == null) if (componentEntities == null)
return; return;
else else
@ -149,8 +147,7 @@ namespace Blarg.GameFramework.Entities
if (GetComponent<T>(entity) != null) if (GetComponent<T>(entity) != null)
throw new InvalidOperationException("Component of that type has been added to this entity already."); throw new InvalidOperationException("Component of that type has been added to this entity already.");
EntityToComponentMap componentEntities; EntityToComponentMap componentEntities = _components.Get(typeof(T));
_components.TryGetValue(typeof(T), out componentEntities);
if (componentEntities == null) if (componentEntities == null)
{ {
// need to create the component-to-entity list // need to create the component-to-entity list
@ -166,13 +163,11 @@ namespace Blarg.GameFramework.Entities
public T GetComponent<T>(Entity entity) where T : Component public T GetComponent<T>(Entity entity) where T : Component
{ {
EntityToComponentMap componentEntities; EntityToComponentMap componentEntities = _components.Get(typeof(T));
_components.TryGetValue(typeof(T), out componentEntities);
if (componentEntities == null) if (componentEntities == null)
return null; return null;
Component existing; Component existing = componentEntities.Get(entity);
componentEntities.TryGetValue(entity, out existing);
if (existing == null) if (existing == null)
return null; return null;
else else
@ -181,13 +176,11 @@ namespace Blarg.GameFramework.Entities
public void RemoveComponent<T>(Entity entity) where T : Component public void RemoveComponent<T>(Entity entity) where T : Component
{ {
EntityToComponentMap componentEntities; EntityToComponentMap componentEntities = _components.Get(typeof(T));
_components.TryGetValue(typeof(T), out componentEntities);
if (componentEntities == null) if (componentEntities == null)
return; return;
Component existing; Component existing = componentEntities.Get(entity);
componentEntities.TryGetValue(entity, out existing);
if (existing == null) if (existing == null)
throw new InvalidOperationException("Entity does not have this component."); throw new InvalidOperationException("Entity does not have this component.");
componentEntities.Remove(entity); componentEntities.Remove(entity);
@ -196,8 +189,7 @@ namespace Blarg.GameFramework.Entities
public bool HasComponent<T>(Entity entity) where T : Component public bool HasComponent<T>(Entity entity) where T : Component
{ {
EntityToComponentMap componentEntities; EntityToComponentMap componentEntities = _components.Get(typeof(T));
_components.TryGetValue(typeof(T), out componentEntities);
if (componentEntities == null) if (componentEntities == null)
return false; return false;
@ -212,8 +204,7 @@ namespace Blarg.GameFramework.Entities
foreach (var i in _components) foreach (var i in _components)
{ {
EntityToComponentMap entitiesWithComponent = i.Value; EntityToComponentMap entitiesWithComponent = i.Value;
Component component; Component component = entitiesWithComponent.Get(entity);
entitiesWithComponent.TryGetValue(entity, out component);
if (component != null) if (component != null)
list.Add(component); list.Add(component);
} }
@ -236,8 +227,7 @@ namespace Blarg.GameFramework.Entities
public T GetGlobalComponent<T>() where T : Component public T GetGlobalComponent<T>() where T : Component
{ {
Component existing; Component existing = _globalComponents.Get(typeof(T));
_globalComponents.TryGetValue(typeof(T), out existing);
if (existing == null) if (existing == null)
return null; return null;
else else
@ -246,8 +236,7 @@ namespace Blarg.GameFramework.Entities
public void RemoveGlobalComponent<T>() where T : Component public void RemoveGlobalComponent<T>() where T : Component
{ {
Component existing; Component existing = _globalComponents.Get(typeof(T));
_globalComponents.TryGetValue(typeof(T), out existing);
if (existing == null) if (existing == null)
throw new InvalidOperationException("No global component of that type exists."); throw new InvalidOperationException("No global component of that type exists.");
else else
@ -319,8 +308,7 @@ namespace Blarg.GameFramework.Entities
foreach (var i in _components) foreach (var i in _components)
{ {
var entitiesWithComponent = i.Value; var entitiesWithComponent = i.Value;
Component component; Component component = entitiesWithComponent.Get(entity);
entitiesWithComponent.TryGetValue(entity, out component);
if (component != null) if (component != null)
{ {
ObjectPools.Free(i.Key, component); ObjectPools.Free(i.Key, component);

View file

@ -36,9 +36,7 @@ namespace Blarg.GameFramework.Events
throw new ArgumentNullException("listener"); throw new ArgumentNullException("listener");
var type = typeof(T); var type = typeof(T);
EventListenerTable listenerTable = null; EventListenerTable listenerTable = _registry.Get(type);
_registry.TryGetValue(type, out listenerTable);
if (listenerTable == null) if (listenerTable == null)
{ {
// need to register this listener for the given type // need to register this listener for the given type
@ -67,8 +65,7 @@ namespace Blarg.GameFramework.Events
var type = typeof(T); var type = typeof(T);
// get the list of listeners for the given event type // get the list of listeners for the given event type
EventListenerTable listenersForType; EventListenerTable listenersForType = _registry.Get(type);
_registry.TryGetValue(type, out listenersForType);
if (listenersForType == null) if (listenersForType == null)
return false; // either no listeners for this type, or the listener wasn't registered with us return false; // either no listeners for this type, or the listener wasn't registered with us
@ -96,8 +93,7 @@ namespace Blarg.GameFramework.Events
var type = e.GetType(); var type = e.GetType();
// find the listeners for the event type provided // find the listeners for the event type provided
EventListenerTable listenersForType; EventListenerTable listenersForType = _registry.Get(type);
_registry.TryGetValue(type, out listenersForType);
if (listenersForType == null) if (listenersForType == null)
return false; // no listeners for this event type have been registered -- we can't handle the event return false; // no listeners for this event type have been registered -- we can't handle the event
@ -196,8 +192,7 @@ namespace Blarg.GameFramework.Events
var type = e.GetType(); var type = e.GetType();
EventListenerTable listenersForType; EventListenerTable listenersForType = _registry.Get(type);
_registry.TryGetValue(type, out listenersForType);
if (listenersForType != null) if (listenersForType != null)
{ {
foreach (var listener in listenersForType) foreach (var listener in listenersForType)

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using PortableGL; using PortableGL;
using Blarg.GameFramework.Support;
namespace Blarg.GameFramework.Graphics namespace Blarg.GameFramework.Graphics
{ {
@ -46,16 +47,12 @@ namespace Blarg.GameFramework.Graphics
public Renderbuffer GetAttachedRenderbuffer(FramebufferRenderbufferFormat format) public Renderbuffer GetAttachedRenderbuffer(FramebufferRenderbufferFormat format)
{ {
Renderbuffer result; return _attachedRenderbuffers.Get(format);
_attachedRenderbuffers.TryGetValue(format, out result);
return result;
} }
public Texture GetAttachedTexture(FramebufferTextureFormat format) public Texture GetAttachedTexture(FramebufferTextureFormat format)
{ {
Texture result; return _attachedTextures.Get(format);
_attachedTextures.TryGetValue(format, out result);
return result;
} }
public Framebuffer(GraphicsDevice graphicsDevice) public Framebuffer(GraphicsDevice graphicsDevice)
@ -270,8 +267,7 @@ namespace Blarg.GameFramework.Graphics
if (IsInvalidated) if (IsInvalidated)
throw new InvalidOperationException(); throw new InvalidOperationException();
Texture existing; Texture existing = _attachedTextures.Get(format);
_attachedTextures.TryGetValue(format, out existing);
if (existing == null) if (existing == null)
return; return;
@ -298,8 +294,7 @@ namespace Blarg.GameFramework.Graphics
if (IsInvalidated) if (IsInvalidated)
throw new InvalidOperationException(); throw new InvalidOperationException();
Renderbuffer existing; Renderbuffer existing = _attachedRenderbuffers.Get(format);
_attachedRenderbuffers.TryGetValue(format, out existing);
if (existing == null) if (existing == null)
return; return;

View file

@ -4,6 +4,7 @@ using PortableGL;
using Blarg.GameFramework; using Blarg.GameFramework;
using Blarg.GameFramework.Graphics.BuiltinShaders; using Blarg.GameFramework.Graphics.BuiltinShaders;
using Blarg.GameFramework.Resources; using Blarg.GameFramework.Resources;
using Blarg.GameFramework.Support;
namespace Blarg.GameFramework.Graphics namespace Blarg.GameFramework.Graphics
{ {
@ -267,8 +268,7 @@ namespace Blarg.GameFramework.Graphics
public Texture GetSolidColorTexture(ref Color color) public Texture GetSolidColorTexture(ref Color color)
{ {
int rgba = color.RGBA; int rgba = color.RGBA;
Texture result; Texture result = _solidColorTextures.Get(rgba);
_solidColorTextures.TryGetValue(rgba, out result);
if (result == null) if (result == null)
{ {
result = CreateSolidColorTexture(ref color); result = CreateSolidColorTexture(ref color);

View file

@ -44,8 +44,7 @@ namespace Blarg.GameFramework.Support
static ObjectObjectPool GetPool(Type type) static ObjectObjectPool GetPool(Type type)
{ {
ObjectObjectPool pool; ObjectObjectPool pool = _pools.Get(type);
_pools.TryGetValue(type, out pool);
if (pool == null) if (pool == null)
{ {
// why do we do this instead of just using a "new()" generic type constraint? // why do we do this instead of just using a "new()" generic type constraint?