Add simpler NextOf function.

This commit is contained in:
UnknownShadow200 2015-04-18 08:17:07 +10:00
parent 988b238501
commit 4929075212
2 changed files with 5 additions and 18 deletions

View File

@ -1,5 +1,4 @@
using System; using System;
using System.ComponentModel;
namespace ClassicalSharp.Commands { namespace ClassicalSharp.Commands {
@ -60,23 +59,10 @@ namespace ClassicalSharp.Commands {
} }
} }
public bool NextOf<T>( out T value ) { public bool NextOf<T>( out T value, TryParseFunc<T> parser ) {
string next = Next(); bool success = parser( Next(), out value );
if( next == null ) { if( !success ) value = default( T);
value = default( T ); return success;
return false;
}
TypeConverter converter = TypeDescriptor.GetConverter( typeof( T ) );
try {
value = (T)converter.ConvertFromInvariantString( next );
return true;
} catch( FormatException ) {
value = default( T );
} catch( NotSupportedException ) {
value = default( T );
}
return false;
} }
bool MoveNext() { bool MoveNext() {

View File

@ -15,6 +15,7 @@ namespace ClassicalSharp {
public delegate TResult Func<TResult>(); public delegate TResult Func<TResult>();
public delegate TResult Func<T1, TResult>( T1 arg1 ); public delegate TResult Func<T1, TResult>( T1 arg1 );
public delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2 ); public delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2 );
public delegate bool TryParseFunc<T>( string s, out T value );
// ################################################################ // ################################################################
public static class Utils { public static class Utils {