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

View file

@ -36,9 +36,7 @@ namespace Blarg.GameFramework.Events
throw new ArgumentNullException("listener");
var type = typeof(T);
EventListenerTable listenerTable = null;
_registry.TryGetValue(type, out listenerTable);
EventListenerTable listenerTable = _registry.Get(type);
if (listenerTable == null)
{
// need to register this listener for the given type
@ -67,8 +65,7 @@ namespace Blarg.GameFramework.Events
var type = typeof(T);
// get the list of listeners for the given event type
EventListenerTable listenersForType;
_registry.TryGetValue(type, out listenersForType);
EventListenerTable listenersForType = _registry.Get(type);
if (listenersForType == null)
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();
// find the listeners for the event type provided
EventListenerTable listenersForType;
_registry.TryGetValue(type, out listenersForType);
EventListenerTable listenersForType = _registry.Get(type);
if (listenersForType == null)
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();
EventListenerTable listenersForType;
_registry.TryGetValue(type, out listenersForType);
EventListenerTable listenersForType = _registry.Get(type);
if (listenersForType != null)
{
foreach (var listener in listenersForType)

View file

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

View file

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

View file

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