mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-12 09:06:55 -04:00
Add simpler NextOf function.
This commit is contained in:
parent
988b238501
commit
4929075212
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace ClassicalSharp.Commands {
|
||||
|
||||
@ -60,23 +59,10 @@ namespace ClassicalSharp.Commands {
|
||||
}
|
||||
}
|
||||
|
||||
public bool NextOf<T>( out T value ) {
|
||||
string next = Next();
|
||||
if( next == null ) {
|
||||
value = default( T );
|
||||
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;
|
||||
public bool NextOf<T>( out T value, TryParseFunc<T> parser ) {
|
||||
bool success = parser( Next(), out value );
|
||||
if( !success ) value = default( T);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool MoveNext() {
|
||||
|
@ -15,6 +15,7 @@ namespace ClassicalSharp {
|
||||
public delegate TResult Func<TResult>();
|
||||
public delegate TResult Func<T1, TResult>( T1 arg1 );
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user