How to convert optional pointer argument from C++ code to C# - c#

I have function in c++ something like this:
// C++
bool Foo(int* retVal = NULL)
{
// ...
if (retVal != NULL)
*retVal = 5;
// ...
return true;
}
And I can use functions in two ways:
int ret;
Foo(&ret);
Foo();
When I write code in C# I used ref keyword:
// C#
bool Foo(ref int retVal = null)
{
// ...
if (retVal != null)
{
retVal = 5;
}
// ...
return true;
}
but compiler said:
A ref or out parameter cannot have a default value.
How can I solve this problem?

The simplest way would be to just write an overload:
bool Foo()
{
int notUsed;
return Foo(ref notUsed);
}
bool Foo(ref int retVal)
{
// ...
retVal = 5;
// ...
return true;
}
If you actually need to know whether the ref value is needed, well, you can still use pointers, but you'll need an unsafe context:
unsafe bool Foo()
{
return Foo(null);
}
unsafe bool Foo(ref int retVal)
{
return Foo(&retVal);
}
private unsafe bool Foo(int* retVal)
{
// ...
if (retVal != null)
{
*retVal = 5;
}
// ...
return true;
}
Or, without unsafe as suggested in the comments, as pointers in C# may be considered as heavy artillery:
bool Foo()
{
var notNeeded = 0;
return Foo(ref notNeeded, false);
}
bool Foo(ref int retVal)
{
return Foo(ref retVal, true);
}
private bool Foo(ref int retVal, bool retValNeeded)
{
// ...
if (retValNeeded)
{
retVal = 5;
}
// ...
return true;
}

The ref of C# is more similar to the & (reference) of C++ than to the * of C and C++ (pointer). As C++ references, they must reference something.
Now, you could:
public class OptionalRef<T>
{
public T Value { get; set; }
public static implicit operator OptionalRef<T>(T value)
{
return new OptionalRef<T> { Value = value };
}
public static implicit operator T(OptionalRef<T> optional)
{
return optional.Value;
}
public override string ToString()
{
return Value != null ? Value.ToString() : null;
}
}
then
static bool Foo(OptionalRef<int> retVal = null)
{
// ...
if (retVal != null)
{
retVal.Value = 5;
}
// ...
return true;
}
and you use it like:
Foo(); // null passed
Foo(null); // same
Foo(5); // not interested if the value is changed
// Full use
OptionalRef<int> val = 5;
Foo(val);
int ret = val;
note that I'm not fully endorsing the semantics I wrote
It is more a case of you asked for something, I gave you something, no questions asked

I'd go about this one of two ways. If you REALLY want to have a reference to your object you can wrap that in a class. Those are always passed by reference so you'll get your modification the way you want and you can instantiate it to null.
Here is an example.
public class HolderObject
{
public string myStr {get; set;}
}
public class Program
{
public static void Main()
{
var xyz = new HolderObject() {
myStr = "1234"
};
Console.WriteLine(xyz.myStr);
FixString(xyz);
Console.WriteLine(xyz.myStr);
FixString();
Console.WriteLine(xyz.myStr);
}
private static bool FixString(HolderObject input = null)
{
if (input != null)
input.myStr = "test";
return true;
}
}
prints
1234
test
Another solution is to overload your function.
bool Foo()
{
// ...
return true;
}
bool Foo(ref int retVal = null)
{
// ...
if (retVal != null)
{
retVal = 5;
}
return Foo();
}
I REALLY don't like this. I'm actually in the middle of working on code in C# that has been pulled directly from C++. Functions that are nested 6 or 7 layers deep modifying an object that was passed by reference. That is hard to read and if you look at the code analysis warnings, it will suggest you don't use ref values.
If you can I'd move away from passing by ref as much as possible and return the value back that has been modified. Or pass an object back that contains both your bool and your new value.

Related

c# generic method take function as param

I'm trying to understand a way to pass in two or more extension methods to a another method as parameters and return the value back. I have extension methods for each datatype to return a value or default value and a value or a null value and also a value or throw an error. The code has scenarios that would require each of these, but it also has scenarios combining the results from each of these in a ternary test, examples below.
public static int IntOrError(this object val, string fieldName)
{
int test;
if (string.IsNullOrEmpty(val.ToString()))
{
throw new Exception("I threw it");
}
else if (int.TryParse(val.ToString(), out test) == false)
{
throw new Exception("Bad Int Value");
}
else
{
return test;
}
}
public static int IntOrDefault(this object val)
{
int test;
if (int.TryParse(val.ToString(), out test))
{
return test;
}
else
{
return -1;
}
}
public static int? IntOrNull(this object val)
{
int test;
if (int.TryParse(val.ToString(), out test))
{
return test;
}
else
{
return -1;
}
}
I've been trying to make a reusable method that could process taking in in this example IntOrNull, IntOrDefault and IntOrError and pass back the int or throw an error. So far I've only been able to get this to work. I'm trying to avoid creating this method for every datatype also.
public static int IntDefaultValidated(this object val, string fieldName)
{
return val.IntOrNUll() != null
? val.IntOrDefaultError(fieldName)
: val.IntOrDefault();
}
I am trying to get a generic method or a functional style of method that will take in the extension methods as params and return back the value.
I'm hoping to avoid reflection if possible.
//just a psuedo example
var intVal = "";
var valRet = DefaultValidated(intVal.IntOrNull(), intVal.IntOrdefault(), intVal.IntOrDefaultError("intVal"));
//or maybe like this, or some combination of extension, generic, functional
var valRet = intVal.DefaultOrValidated(IntOrNull(intVal), IntOrDefault(intVal), IntOrDefaultError(intVal, "intVal"));
Your logic of IntOrDefault, IntOrNull and IntDefaultValidated does not really make sense, so I think it's just an usage example.
Beside this - my advice is to implement your functions as generic extensions.
public static class MyExtensions
{
public static T ValueOrError<T>(this object val)
{
try
{
// http://stackoverflow.com/a/8633/2534462
return (T)Convert.ChangeType(val, typeof(T));
} catch
{
throw new Exception("Throw your own exception if you really want to");
}
}
public static T ValueOrDefault<T>(this object val, T defaultValue)
{
try
{
return val.ValueOrError<T>();
}
catch
{
return defaultValue; // usally use: return default(T);
}
}
public static T ValueOrNull<T>(this object val)
{
try
{
return val.ValueOrError<T>();
}
catch
{
// check for nullable type
//https://msdn.microsoft.com/de-de/library/ms366789.aspx
var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return default(T); // null on nullable types
} else {
throw new Exception("Callable only on Nullable types");
// my return another default value ??? .. -1 ???
}
}
}
public static T ValueDefaultValidated<T>(this object val, T defaultValue)
{
return val.ValueOrNull<T>() != null
? val.ValueOrError<T>()
: val.ValueOrDefault<T>(defaultValue);
}
}
Usage
string aNumber = "10";
var intNumber = aNumber.ValueDefaultValidated(-1); // int
var decNumber = aNumber.ValueDefaultValidated(-1m); // decimal
string naNumber = "whatever";
var defaultInt = naNumber.ValueOrDefault(-1); // int
var defaultDecimal = naNumber.ValueDefaultValidated<decimal?>(-1m);
// ValueOrNull ist undefined on Non-Nullable-Type.
var undefined = naNumber.ValueDefaultValidated<decimal>(-1m);
Something like this?
public static T Convert<T>(this object input)
{
if (typeof (T) == input.GetType())
return (T) input;
var stringValue = input.ToString();
var converter = TypeDescriptor.GetConverter(typeof(T));
if (converter.CanConvertFrom(typeof(string)))
{
return (T)converter.ConvertFrom(stringValue);
}
return default(T);
}
This test passes. May be this is not exactly what you need but still.
[Fact]
public void TestMethod1()
{
object testInt = 1;
object testString = "123";
double testDouble = 1.0;
string testWrong = "abc";
int resultInt = testInt.Convert<int>();
string resultString = testString.Convert<string>();
int resultIntString = testString.Convert<int>();
int dbl2int = testDouble.Convert<int>();
Assert.Throws<Exception>(() => testWrong.Convert<int>());
Assert.Equal(1, resultInt);
Assert.Equal("123", resultString);
Assert.Equal(123, resultIntString);
Assert.Equal(1, dbl2int);
}

how to create a custom cast explicit in c #?

Have this code:
string abc = "123456";
To convert to int should I use convert:
int abcInt = Convert.ToInt32(abc);
The problem is that if not a number I have an exception see returning zero so my final code will look like:
try{ int abcInt = Convert.ToInt32(abc); }catch(Exception e){ int abcInt = 0; }
So you see that I decided to create a book that made ​​me an object returning zero numeric without exception if it failed, so could keep most flexible programming without much junk code:
int abcInt = Libs.str.safeInt(abc);
The code is:
public int safeInt(object ob)
{
if ((ob == null) || (String.IsNullOrEmpty(ob.ToString())))
return 0;
try
{
return Convert.ToInt32(
System.Text.RegularExpressions.Regex.Replace(ob.ToString(), #"#[^Ee0-9\.\,]+#i", "").
ToString(CultureInfo.InvariantCulture.NumberFormat)
);
}
catch (FormatException e)
{
return 0;
}
}
But I want to go one step further and do something like this:
int abcInt = (safeInt)abc;
how to do?
Can not convert type 'string' to 'Libs.safeInt.safeInt'
You should just use Int32.TryParse:
int abcInt;
if(!Int32.TryParse(abc, out abcInt)) {
abcInt = 0;
}
// abcInt has been parsed to an int, or defaulted to zero
Note that this can be shortened to
int abcInt;
Int32.TryParse(abc, out abcInt);
if all that you want is the default value to be zero because:
When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
I actually recommend against writing it this way because now you can't distinguish between abc = "0" and abc = "garbage"; both with exhibit exactly the same behavior with the above two lines of code. With the initial version above (i.e., the if, you can distinguish the two cases if you need to; silently ignoring errors is generally a bad idea).
That said, if you absolutely are dying to know how to implement an explicit cast operator, you proceed like this:
class SafeInt32 {
private readonly int value;
public int Value { get { return this.value; } }
private readonly string source;
public string Source { get { return this.source; } }
private readonly bool successful;
public bool Successful { get { return this.successful; } }
public SafeInt32(string source) {
this.source = source;
this.successful = Int32.TryParse(source, out this.value);
}
public static explicit operator SafeInt32(string source) {
return new SafeInt32(source);
}
public static implicit operator int(SafeInt32 safeInt32) {
return safeInt32.Value;
}
}
Usage:
int abcInt = (SafeInt32)"123456";
Note that we had to define an explicit cast operator to cast a string to a SafeInt32, and an implicit cast operator to cast a SafeInt32 to an int to achieve your desired syntax. The latter is necessary so that the compiler can silently convert the result of (SafeInt32)"123456" to an int.
Again, I recommend against this; use Int32.TryParse.
You can leverage implicit and explicit operators to do what you want, yes. You can also use int.TryParse to avoid using exceptions for control flow.
public struct SafeInt
{
public int Value { get; private set; }
public static implicit operator int(SafeInt safeInt)
{
return safeInt.Value;
}
public static explicit operator SafeInt(string obj)
{
return new SafeInt() { Value = SafeParse(obj) };
}
public static int SafeParse(object value)
{
int output;
int.TryParse((value ?? "0").ToString(), out output);
return output;
}
}
I mean, you should use int.TryParse, but if you're dead-set on the cast syntax:
public class SafeInt
{
private int _value;
private SafeInt() {}
public static explicit operator SafeInt(string str)
{
int x;
int.TryParse(str, out x);
SafeInt si = new SafeInt();
si._value = x;
return si;
}
public static implicit operator int(SafeInt x)
{
return x._value;
}
public override string ToString()
{
return _value.ToString();
}
}
You can then use it like this:
int x = (SafeInt)"234234";
First, let me just go on record saying that you may not want to do this.
Silently ignoring problems like this can cause other types of problems, such as a customer asking "Why is this total over here always wrong?".
Having said that, let's see how you can do what you want before I give you a better option:
void Main()
{
int a = (SafeInt)"123";
a.Dump();
int b = (SafeInt)"xyz";
b.Dump();
}
public struct SafeInt
{
private readonly int _Value;
public SafeInt(int value)
{
_Value = value;
}
public SafeInt(int? value)
{
_Value = value ?? 0;
}
public int Value
{
get
{
return _Value;
}
}
public static implicit operator int(SafeInt s)
{
return s.Value;
}
public static implicit operator SafeInt(string s)
{
try
{
return new SafeInt(Convert.ToInt32(s));
}
catch (FormatException)
{
return new SafeInt();
}
}
}
This will print out:
123
0
Now, my advice is to stay away from this. Instead, use this:
void Main()
{
TryParse("123").Dump();
TryParse("xyz").Dump();
}
public static int TryParse(string s, int errorValue = 0)
{
int result;
if (int.TryParse(s, out result))
return result;
return errorValue;
}
Note that if you always want 0 as the value to return upon an error, there's even a much simpler way built into the system, this:
int value;
int.TryParse("123", out value);
Here we disregard the Boolean result from TryParse, because if TryParse fails, it'll set the parameter to 0.
I'd recommend that you do not do this. I find explicit and implicit conversions to be hard to discover, read, and use, compared to simpler static methods and/or constructors. Also, are you aware of the int.TryParse method? That might be a better solution for you:
public static int SafeInt(object value)
{
int i;
int.TryParse(value.ToString(), out i);
return i;
}
Or, more directly to answer your question, you can use explicit and implicit conversions on a SafeInt class to do this:
public class SafeInt
{
public int Value { get; set; }
public static implicit operator int(SafeInt si)
{
return si.Value;
}
public static explicit operator SafeInt(String str)
{
return new SafeInt { Value = Libs.str.safeInt(str) };
}
}
Use like:
int i = (SafeInt)"123";

Is there a conditional abort return technique for C# shorthand if notation

Is there a conditional abort return technique for C# shorthand if notation ?
like:
return (a==b) ? true : abort;
if condition not satisfies, return will be aborted.
You're asking this question, so there might be a reason why you don't want to do this:
if (a==b) return true;
Above is very readable, maintainable, etc.
Anyway you could use a delegate with a lambda and do something like this (although I strongly discourage it):
delegate bool Abort();
bool YourMethod() // not mine ;)
{
int a = 1, b = 2;
return (a == b) || new Abort(() => {
// put the rest of your 'abort' code here
return false; // or throw an exception...
})();
}
public class Program
{
static void Main(string[] args)
{
var res = Test();
}
static bool Test()
{
var a = 5;
var b = 6;
return a.TrueOrAbort(b);
}
}
public static class MyHelper
{
public static bool TrueOrAbort<T>(this T first, T second) where T : struct, IComparable
{
if (first.Equals(second))
return true;
Environment.Exit(0);
return false;
}
}

C#, Copy one bool to another (by ref, not val)

I am at a brick wall here. Is it possible to copy one bool to the ref of another. Consider this code . . .
bool a = false;
bool b = a;
b is now a totally separate bool with a value of false. If I subsequently change a, it will have no effect on b. Is it possible to make a = b by ref? How would I do that?
Many thanks
No. Since bool is a value type, it will always be copied by value.
The best option is to wrap your bool within a class - this will give it reference type semantics:
public class BoolWrapper
{
public bool Value { get; set; }
public BoolWrapper (bool value) { this.Value = value; }
}
BoolWrapper a = new BoolWrapper(false);
BoolWrapper b = a;
b.Value = true;
// a.Value == true
Thanks to #Reed for his answer (+1)! He encouraged me to a more "generic" solution! :)
public class ValueWrapper<T> where T : struct
{
public T Value { get; set; }
public ValueWrapper(T value) { this.Value = value; }
}
Small extension to Andrey's answer... this allows you to assign it to whatever type you want in the end directly. So:
ValueWrapper<bool> wrappedBool = new ValueWrapper<bool>(true);
bool unwrapped = wrappedBool; // you can assign it direclty:
if (wrappedBool) { // or use it how you'd use a bool directly
// ...
}
public class ValueWrapper<T>
{
public T Value { get; set; }
public ValueWrapper() { }
public ValueWrapper(T value) {
this.Value = value;
}
public static implicit operator T(ValueWrapper<T> wrapper)
{
if (wrapper == null) {
return default(T);
}
return wrapper.Value;
}
}
this may not be what you want, but if your scenario were such that you wanted a function that you called to modify your local boolean, you can use the ref or out keyworkd.
bool a = false;
F(ref a);
// a now equals true
...
void F(ref bool x)
{
x = true;
}
So I'm guessing you are needing to pass a reference a bool, that you cannot wrap with a 'BoolWrapper' class, because the bool lives some place that you cannot or do not wish to modify.
It can be done!
First declare what any bool reference will look like
/// <summary> A reference to a bool.</summary>
/// <param name="value">new value</param>
/// <returns>Value of boolean</returns>
public delegate bool BoolRef(bool? value = null);
Now you can make a reference to myBool like this
bool myBool; // A given bool that you cannot wrap or change
private bool myBoolRef(bool? value) {
if (value != null) {
myBool = (bool)value;
}
return myBool;
}
And use it like this:
void myTestCaller() {
foo(myBoolRef);
}
void foo(BoolRef b) {
bool c = b(); // get myBool
b(true); // set myBool to true
}
The same trick works for other value types such as int
A bool is a value type and cannot be copied by reference.
I had a case where I wanted one class to change another class' bool - please note that there are better ways to handle this situation but this is a proof of concept using Actions.
public class Class1
{
bool myBool { get; set; }
void changeBoolFunc(bool val) { myBool = val; }
public Class1()
{
Action<bool> changeBoolAction = changeBoolFunc;
myBool = true;
Console.WriteLine(myBool); // outputs "True"
Class2 c2 = new Class2(changeBoolAction);
Console.WriteLine(myBool); // outputs "False"
}
}
public class Class2
{
public Class2(Action<bool> boolChanger) { boolChanger(false); }
}
void Main()
{
Class1 c1 = new Class1();
}
Just use the flags as Nullable<bool> or bool? and set those in the struct that's passed to the generic method. The ValueWrapper<T> class above is essentially exactly what Nullable<T> does.

C#, Flags Enum, Generic function to look for a flag

I'd like one general purpose function that could be used with any Flags style enum to see if a flag exists.
This doesn't compile, but if anyone has a suggestion, I'd appreciate it.
public static Boolean IsEnumFlagPresent<T>(T value,T lookingForFlag)
where T:enum
{
Boolean result = ((value & lookingForFlag) == lookingForFlag);
return result ;
}
No, you can't do this with C# generics. However, you could do:
public static bool IsEnumFlagPresent<T>(T value, T lookingForFlag)
where T : struct
{
int intValue = (int) (object) value;
int intLookingForFlag = (int) (object) lookingForFlag;
return ((intValue & intLookingForFlag) == intLookingForFlag);
}
This will only work for enums which have an underlying type of int, and it's somewhat inefficient because it boxes the value... but it should work.
You may want to add an execution type check that T is actually an enum type (e.g. typeof(T).BaseType == typeof(Enum))
Here's a complete program demonstrating it working:
using System;
[Flags]
enum Foo
{
A = 1,
B = 2,
C = 4,
D = 8
}
class Test
{
public static Boolean IsEnumFlagPresent<T>(T value, T lookingForFlag)
where T : struct
{
int intValue = (int) (object) value;
int intLookingForFlag = (int) (object) lookingForFlag;
return ((intValue & intLookingForFlag) == intLookingForFlag);
}
static void Main()
{
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.A));
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.B));
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.C));
Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.D));
}
}
You're looking to replace one line of code with a function that wraps one line of code? I'd say to just use the one line of code...
For what its worth, I recently read that this feature will be part of .NET 4.0. Specifically, it is implemented in the Enum.HasFlag() function.
I have used this before:
public static bool In<T>(this T me, T values)
where T : struct, IConvertible
{
return (me.ToInt64(null) & values.ToInt64(null)) > 0;
}
What I like about it is you can use this clean syntax to call it since in 3.5 the compiler will can infer generic parameters.
AttributeTargets a = AttributeTargets.Class;
if (a.In(AttributeTargets.Class | AttributeTargets.Module))
{
// ...
}
You can do this without generics:
static bool ContainsFlags(Enum value, Enum flag)
{
if (Enum.GetUnderlyingType(value.GetType()) == typeof(ulong))
return (Convert.ToUInt64(value) & Convert.ToUInt64(flag)) == Convert.ToUInt64(flag);
else
return (Convert.ToInt64(value) & Convert.ToInt64(flag)) == Convert.ToInt64(flag);
}
I'm converting to Int64 in this case, which should handle every case except ulong, which is why the extra check...
Why not write an extension method for this? I did this in another post
public static class EnumerationExtensions {
public static bool Has<T>(this System.Enum type, T value) {
try {
return (((int)(object)type & (int)(object)value) == (int)(object)value);
}
catch {
return false;
}
}
//... etc...
}
//Then use it like this
bool hasValue = permissions.Has(PermissionTypes.Delete);
It could use a little refinement (since it assumes everything can be cast as an int), but it could get you started...
Worth pointing out that simply providing some static overloads for all the integral types will work so long as you know you are working with a specific enum. They won't work if the consuming code is likewise operating on where t : struct
If you need to deal with arbitrary (struct) T
You cannot currently do a fast conversion of a generically typed struct into some alternate bitwise form (i.e. roughly speaking a reinterpret_cast) without using C++/CLI
generic <typename T>
where T : value class
public ref struct Reinterpret
{
private:
const static int size = sizeof(T);
public:
static int AsInt(T t)
{
return *((Int32*) (void*) (&t));
}
}
This will then let you write:
static void IsSet<T>(T value, T flags) where T : struct
{
if (!typeof(T).IsEnum)
throw new InvalidOperationException(typeof(T).Name +" is not an enum!");
Type t = Enum.GetUnderlyingType(typeof(T));
if (t == typeof(int))
{
return (Reinterpret.AsInt(value) & Reinterpret.AsInt(flags)) != 0
}
else if (t == typeof(byte))
{
return (Reinterpret.AsByte(value) & Reinterpret.AsByte(flags)) != 0
}
// you get the idea...
}
You cannot constrain to enums. But the mathematical validity of these methods do not change if they are used with non enum types so you could allow them if you can determine that they are convertible to a struct of the relevant size.
Question long over, but here's one for reference anyway:
public static bool HasFlag<TEnum>(this TEnum enumeratedType, TEnum value)
where TEnum : struct, IComparable, IFormattable, IConvertible
{
if (!(enumeratedType is Enum))
{
throw new InvalidOperationException("Struct is not an Enum.");
}
if (typeof(TEnum).GetCustomAttributes(
typeof(FlagsAttribute), false).Length == 0)
{
throw new InvalidOperationException("Enum must use [Flags].");
}
long enumValue = enumeratedType.ToInt64(CultureInfo.InvariantCulture);
long flagValue = value.ToInt64(CultureInfo.InvariantCulture);
if ((enumValue & flagValue) == flagValue)
{
return true;
}
return false;
}
Today, you can set the c# language version to >=7.3
and use the next code as the reference:
public static class EnumExt
{
public static IEnumerable<TEnum> Explode<TEnum>(this TEnum enumValue) where TEnum : Enum
{
var res = Enum.GetValues(enumValue.GetType())
.Cast<TEnum>()
.Where(x => enumValue.HasFlag(x));
return res;
}
public static string ExplodeToString<TEnum>(this TEnum enumValue, string delimeter = ",") where TEnum : Enum
{
return string.Join(delimeter, Explode(enumValue));
}
}
Well, I don't believe there is a way to do this, as there are no constraints that apply to bitwise operators.
However... you can just cast your enum to int and do it.
public static Boolean IsEnumFlagPresent(int value,int lookingForFlag)
{
return ((value & lookingForFlag) == lookingForFlag);
}
This works, but may be confusing to someone.
below is code that benchmarks 4 different methods. results are shown in code in comment "BENCHMARK: .. nSec".
"((enum & flag) != 0)' is 10x faster than HasFlag() function. if you are in a tight loop, then i think it is best to accept it.
public static int jumpCtr=0;
public static int ctr=0;
public static TestFlags gTestFlags = TestFlags.C;
[Flags] public enum TestFlags { A=1<<1, B=1<<2, C=1<<3 }
public static void Jump() { jumpCtr++; gTestFlags = (gTestFlags == TestFlags.B) ? TestFlags.C : TestFlags.B; }
// IsEnumFlagPresent() https://stackoverflow.com/questions/987607/c-flags-enum-generic-function-to-look-for-a-flag
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasFlag_Faster<T>(T value, T lookingForFlag)
where T : struct
{
int intValue = (int) (object) value;
int intLookingForFlag = (int) (object) lookingForFlag;
return ((intValue & intLookingForFlag) != 0);
}
// IsEnumFlagPresent() https://stackoverflow.com/questions/987607/c-flags-enum-generic-function-to-look-for-a-flag
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool HasFlag_Faster_Integer(int intValue, int intLookingForFlag)
{
return ((intValue & intLookingForFlag) != 0);
}
public static void Benchmark_HasFlag( )
{
if ( ! hwDvr._weAreOnGswCpu) { return; }
DateTime timer = DateTime.Now;
string a, b, c, d, e;
double base_nSecPerLoop, b_nSecPerLoop, c_nSecPerLoop, d_nSecPerLoop, e_nSecPerLoop;
int numOfLoops = (int) 1.0e6;
// ------------------------------------------------------
for (int i=0; i<numOfLoops;i++) {
Jump();
}
a = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out base_nSecPerLoop);
// ------------------------------------------------------
// BENCHMARK: 50 nSec
for (int i=0; i<numOfLoops;i++) {
if (gTestFlags.HasFlag((TestFlags) TestFlags.C)) {
ctr++;
}
Jump();
}
b = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out b_nSecPerLoop );
double b_diff = b_nSecPerLoop - base_nSecPerLoop;
// ------------------------------------------------------
// BENCHMARK: 3 nSec
for (int i=0; i<numOfLoops;i++) {
if ((gTestFlags & TestFlags.C) != 0) {
ctr++;
}
Jump();
}
c = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out c_nSecPerLoop );
double c_diff = c_nSecPerLoop - base_nSecPerLoop;
// ------------------------------------------------------
// BENCHMARK: 64 nSec
for (int i=0; i<numOfLoops;i++) {
if (HasFlag_Faster<TestFlags>(value:gTestFlags, lookingForFlag: TestFlags.C)) {
ctr++;
}
Jump();
}
d = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out d_nSecPerLoop );
double d_diff = d_nSecPerLoop - base_nSecPerLoop;
// ------------------------------------------------------
// BENCHMARK: 14 nSec
for (int i=0; i<numOfLoops;i++) {
if (HasFlag_Faster_Integer((int)gTestFlags, (int)TestFlags.C)) {
ctr++;
}
Jump();
}
e = BenchMarkSystem_Helper.SimpleTimer_Loops( ref timer, numOfLoops, out e_nSecPerLoop );
double e_diff = e_nSecPerLoop - base_nSecPerLoop;
int brkPt=0;
}

Categories

Resources