mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-13 09:35:23 -04:00
Add simpler NextOf function.
This commit is contained in:
parent
988b238501
commit
4929075212
@ -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() {
|
||||||
|
@ -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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user