Skip to content
Browse files

Minor refactoring

  • Loading branch information...
1 parent 5f08c5c commit b1a48035f73ff938bb99fecd25f435fb576edb18 @activa activa committed
View
6 Library/FastReflection/FastPropertyInfo.cs
@@ -147,10 +147,8 @@ private void Init()
private Assembly EmitAssembly()
{
- AssemblyName assemblyName = new AssemblyName();
-
- assemblyName.Name = "PropertyAccessorAssembly";
-
+ AssemblyName assemblyName = new AssemblyName { Name = "PropertyAccessorAssembly" };
+
AssemblyBuilder newAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder newModule = newAssembly.DefineDynamicModule("Module");
TypeBuilder myType = newModule.DefineType("PropertyAccessor", TypeAttributes.Public);
View
88 Library/Json/JSONParser.cs
@@ -42,11 +42,6 @@ public class JsonParser
private Token[] _tokens;
private int _currentToken;
- static JsonParser()
- {
-
- }
-
public T Parse<T>(string json) where T:class, new()
{
Tokenize(json);
@@ -76,20 +71,28 @@ private void Tokenize(string json)
_tokens = tokens.ToArray();
}
- private Token CurrentToken
+ private Token CurrentToken()
{
- get { return _tokens[_currentToken]; }
+ return _currentToken < _tokens.Length ? _tokens[_currentToken] : null;
+ }
+
+ private void NextToken()
+ {
+ _currentToken++;
}
private object ParseObject(Type objectType)
{
- if (CurrentToken.TokenMatcher is NullTokenMatcher)
+ if (CurrentToken().TokenMatcher is NullTokenMatcher)
{
- _currentToken++;
+ NextToken();
return null;
}
+ if (!(CurrentToken().TokenMatcher is ObjectStartTokenMatcher))
+ throw new Exception("Expected {");
+
object obj;
bool isDictionary = false;
@@ -102,24 +105,21 @@ private object ParseObject(Type objectType)
else
obj = Activator.CreateInstance(objectType);
- if (!(CurrentToken.TokenMatcher is ObjectStartTokenMatcher))
- throw new Exception("Expected {");
-
- _currentToken++;
+ NextToken();
for (; ; )
{
- if (!(CurrentToken.TokenMatcher is StringTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is StringTokenMatcher))
throw new Exception("Expected property name");
- string propName = CurrentToken.Text.Substring(1, CurrentToken.Text.Length - 2);
+ string propName = CurrentToken().Text.Substring(1, CurrentToken().Text.Length - 2);
- _currentToken++;
+ NextToken();
- if (!(CurrentToken.TokenMatcher is ColonTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is ColonTokenMatcher))
throw new Exception("Expected colon");
- _currentToken++;
+ NextToken();
if (!isDictionary)
{
@@ -147,16 +147,16 @@ private object ParseObject(Type objectType)
((Dictionary<string, object>) obj)[propName] = ParseValue(typeof (object));
}
- if (!(CurrentToken.TokenMatcher is CommaTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is CommaTokenMatcher))
break;
- _currentToken++;
+ NextToken();
}
- if (!(CurrentToken.TokenMatcher is ObjectEndTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is ObjectEndTokenMatcher))
throw new Exception("Expected }");
- _currentToken++;
+ NextToken();
return obj;
}
@@ -179,46 +179,48 @@ private object ParseValue(Type type)
{
if (type == typeof(object))
{
- if (CurrentToken.TokenMatcher is StringTokenMatcher)
+ if (CurrentToken().TokenMatcher is StringTokenMatcher)
type = typeof(string);
- else if (CurrentToken.TokenMatcher is IntegerTokenMatcher)
+ else if (CurrentToken().TokenMatcher is IntegerTokenMatcher)
type = typeof(Int64);
- else if (CurrentToken.TokenMatcher is FloatTokenMatcher)
+ else if (CurrentToken().TokenMatcher is FloatTokenMatcher)
type = typeof(double);
- else if (CurrentToken.TokenMatcher is TrueTokenMatcher || CurrentToken.TokenMatcher is FalseTokenMatcher)
+ else if (CurrentToken().TokenMatcher is TrueTokenMatcher || CurrentToken().TokenMatcher is FalseTokenMatcher)
type = typeof(bool);
- else if (CurrentToken.TokenMatcher is ArrayStartTokenMatcher)
+ else if (CurrentToken().TokenMatcher is ArrayStartTokenMatcher)
type = typeof(object[]);
- else if (CurrentToken.TokenMatcher is ObjectStartTokenMatcher || CurrentToken.TokenMatcher is NullTokenMatcher)
+ else if (CurrentToken().TokenMatcher is ObjectStartTokenMatcher || CurrentToken().TokenMatcher is NullTokenMatcher)
type = typeof (object);
else
- throw new Exception("Unexpected token " + CurrentToken);
+ throw new Exception("Unexpected token " + CurrentToken());
}
if (type == typeof(string))
return ParseString();
- else if (type == typeof(int) || type == typeof(short) || type == typeof(long) || type == typeof(double) || type == typeof(float) || type == typeof(decimal))
+
+ if (type == typeof(int) || type == typeof(short) || type == typeof(long) || type == typeof(double) || type == typeof(float) || type == typeof(decimal))
return ParseNumber(type);
- else if (IsArray(type))
+
+ if (IsArray(type))
return ParseArray(type);
- else
- return ParseObject(type);
+
+ return ParseObject(type);
}
private object ParseNumber(Type type)
{
- if (!(CurrentToken.TokenMatcher is IntegerTokenMatcher) && !(CurrentToken.TokenMatcher is FloatTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is IntegerTokenMatcher) && !(CurrentToken().TokenMatcher is FloatTokenMatcher))
throw new Exception("Number expected");
object n;
- if (CurrentToken.TokenMatcher is IntegerTokenMatcher)
+ if (CurrentToken().TokenMatcher is IntegerTokenMatcher)
{
- n = Int64.Parse(CurrentToken.Text, NumberFormatInfo.InvariantInfo);
+ n = Int64.Parse(CurrentToken().Text, NumberFormatInfo.InvariantInfo);
}
else
{
- n = Double.Parse(CurrentToken.Text, NumberFormatInfo.InvariantInfo);
+ n = Double.Parse(CurrentToken().Text, NumberFormatInfo.InvariantInfo);
}
_currentToken++;
@@ -230,10 +232,10 @@ private object ParseNumber(Type type)
private object ParseString()
{
- if (!(CurrentToken.TokenMatcher is StringTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is StringTokenMatcher))
throw new Exception("Expected string");
- string s = CurrentToken.Text.Substring(1,CurrentToken.Text.Length-2);
+ string s = CurrentToken().Text.Substring(1,CurrentToken().Text.Length-2);
//TODO: parse dates
@@ -251,7 +253,7 @@ private object ParseArray(Type type)
elementType = type.GetElementType();
}
- if (!(CurrentToken.TokenMatcher is ArrayStartTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is ArrayStartTokenMatcher))
throw new Exception("Expected [");
_currentToken++;
@@ -260,18 +262,18 @@ private object ParseArray(Type type)
for(;;)
{
- if (CurrentToken.TokenMatcher is ArrayEndTokenMatcher)
+ if (CurrentToken().TokenMatcher is ArrayEndTokenMatcher)
break;
list.Add(ParseValue(elementType));
- if (!(CurrentToken.TokenMatcher is CommaTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is CommaTokenMatcher))
break;
_currentToken++;
}
- if (!(CurrentToken.TokenMatcher is ArrayEndTokenMatcher))
+ if (!(CurrentToken().TokenMatcher is ArrayEndTokenMatcher))
throw new Exception("Expected ]");
_currentToken++;
View
15 Library/Logger/Logger.cs
@@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
namespace Vici.Core.Logging
{
@@ -70,12 +71,9 @@ public void LogException(LogLevel level, Exception e)
lock (_instanceLock)
{
- foreach (LoggingProvider provider in _loggingProviders)
+ foreach (var provider in _loggingProviders.Where(provider => (level & provider.LogLevelMask) != 0 && level >= provider.MinimumLogLevel))
{
- if ((level & provider.LogLevelMask) != 0 && level >= provider.MinimumLogLevel)
- {
- provider.LogException(now, level, e);
- }
+ provider.LogException(now, level, e);
}
}
}
@@ -88,12 +86,9 @@ public void Log(LogLevel level, string formatString, params object[] p)
lock (_instanceLock)
{
- foreach (LoggingProvider provider in _loggingProviders)
+ foreach (var provider in _loggingProviders.Where(provider => (level & provider.LogLevelMask) != 0 && level >= provider.MinimumLogLevel))
{
- if ((level & provider.LogLevelMask) != 0 && level >= provider.MinimumLogLevel)
- {
- provider.LogText(now, level, fmt);
- }
+ provider.LogText(now, level, fmt);
}
}
}
View
2 Library/Logger/LoggingProvider.cs
@@ -33,7 +33,7 @@ public abstract class LoggingProvider : IDisposable
private LogLevel _logLevelMask = LogLevel.All;
private LogLevel _minimumLogLevel = LogLevel.Information;
- public LoggingProvider()
+ protected LoggingProvider()
{
TimeFormatString = "yyyy.MM.dd HH:mm:ss.ff";
}
View
17 Library/Notifier/ISubscription.cs
@@ -40,21 +40,4 @@ public interface ISubscription<T> : ISubscription
{
new IEnumerable<Notification<T>> GetNotifications();
}
-
- public class WeakReference<T> : WeakReference
- {
- public WeakReference(T target) : base(target)
- {
- }
-
- public WeakReference(T target, bool trackResurrection) : base(target, trackResurrection)
- {
- }
-
- public new T Target
- {
- get { return (T) base.Target; }
- set { base.Target = value; }
- }
- }
}
View
2 Library/Notifier/Notification.cs
@@ -45,7 +45,7 @@ public Notification(object sender, string name, object payload)
public class Notification<T> : Notification
{
- public Notification(object sender, string name, T payload): base(sender, name, payload)
+ public Notification(object sender, string name, T payload) : base(sender, name, payload)
{
Payload = payload;
}
View
9 Library/Notifier/Notifier.cs
@@ -60,7 +60,6 @@ public Subscription(Notifier notifier, string pattern, NotificationDelegate meth
Method = method;
}
-
public void Unsubscribe()
{
_notifier.Unsubscribe(this);
@@ -97,7 +96,7 @@ public IEnumerable<Notification> GetNotifications()
}
}
- public void EnqueueNotification(string name, Notification notification)
+ public void EnqueueNotification(Notification notification)
{
if (_queuedNotificications == null)
_queuedNotificications = new List<Notification>();
@@ -157,7 +156,7 @@ public void Post(string name, object payload, object sender)
var method = subscription.Method;
if (method == null)
- subscription.EnqueueNotification(name, notification);
+ subscription.EnqueueNotification(notification);
else
method(notification);
}
@@ -166,7 +165,7 @@ public void Post(string name, object payload, object sender)
public void Post<T>(string name, T payload, object sender)
{
- Notification<T> notification = new Notification<T>(null,name,payload);
+ Notification<T> notification = new Notification<T>(sender,name,payload);
foreach (Subscription subscription in _subscriptions)
{
@@ -175,7 +174,7 @@ public void Post<T>(string name, T payload, object sender)
var method = ((Subscription<T>) subscription).Method;
if (method == null)
- subscription.EnqueueNotification(name, notification);
+ subscription.EnqueueNotification(notification);
else
method(notification);
}
View
49 Library/TypedWeakReference.cs
@@ -0,0 +1,49 @@
+#region License
+//=============================================================================
+// Vici Core - Productivity Library for .NET 3.5
+//
+// Copyright (c) 2008-2012 Philippe Leybaert
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+//=============================================================================
+#endregion
+
+using System.Linq;
+using System.Collections.Generic;
+using System;
+
+namespace Vici.Core
+{
+ public class WeakReference<T> : WeakReference
+ {
+ public WeakReference(T target) : base(target)
+ {
+ }
+
+ public WeakReference(T target, bool trackResurrection) : base(target, trackResurrection)
+ {
+ }
+
+ public new T Target
+ {
+ get { return (T) base.Target; }
+ set { base.Target = value; }
+ }
+ }
+}
View
1 Library/Vici.Core.csproj
@@ -77,6 +77,7 @@
<Compile Include="Notifier\ISubscription.cs" />
<Compile Include="Notifier\Notification.cs" />
<Compile Include="Notifier\Notifier.cs" />
+ <Compile Include="TypedWeakReference.cs" />
<Compile Include="Parser\Context\ParserContextBehavior.cs" />
<Compile Include="Parser\ExpressionParser\Expression\ContainerClasses\IMethodDefinition.cs" />
<Compile Include="Parser\ExpressionParser\Expression\DefaultValueExpression.cs" />

0 comments on commit b1a4803

Please sign in to comment.
Something went wrong with that request. Please try again.