public static void argit1( __arglist ) { ArgIterator args = new ArgIterator( __arglist ); try { int argCount = args.GetRemainingCount(); for (int i = 0; i < argCount; i++) { TypedReference trTypRef = args.GetNextArg(); } } catch(Exception ex) { throw new Exception( "ExcErr001 ," + ex.ToString() ); } for ( int j = 0; j < 5; j++ ) { try { TypedReference trTypRef = args.GetNextArg(); throw new Exception( "ExcErr002 , Last call should have thrown." ); } catch (InvalidOperationException) {} } }
public static int Sum(int a, double b, int c, float d, __arglist) { ArgIterator args = new ArgIterator(__arglist); int S = a + (int)b + c + (int)d; while(args.GetRemainingCount() > 0) { if (args.GetNextArgType().Equals(typeof(int).TypeHandle)) { int N = __refvalue(args.GetNextArg(), int); Console.WriteLine("int value is "+N); S = S + N; } else if (args.GetNextArgType().Equals(typeof(string).TypeHandle)) { string s = __refvalue(args.GetNextArg(), string); Console.WriteLine("string value is "+s); } else if (args.GetNextArgType().Equals(typeof(MyStruct).TypeHandle)) { MyStruct st = __refvalue(args.GetNextArg(), MyStruct); Console.WriteLine("MyStruct value is "+st.x+" "+st.y); S = S + 2*st.x + 3*st.y; } else return -1; } return S; }
public static long EnumTest(__arglist) { ArgIterator args = new ArgIterator(__arglist); long S = 0; while (args.GetRemainingCount() > 0) { if (args.GetNextArgType().Equals(typeof(Enum1).TypeHandle)) { Enum1 e = __refvalue(args.GetNextArg(), Enum1); Console.WriteLine("Got Enum1, value = " + e + " = " + (long)e); S += (long)e; } if (args.GetNextArgType().Equals(typeof(Enum8).TypeHandle)) { Enum8 e = __refvalue(args.GetNextArg(), Enum8); Console.WriteLine("Got Enum8, value = " + e + " = " + (long)e); S += (long)e; } if (args.GetNextArgType().Equals(typeof(Enum16).TypeHandle)) { Enum16 e = __refvalue(args.GetNextArg(), Enum16); Console.WriteLine("Got Enum16, value = " + e + " = " + (long)e); S += (long)e; } if (args.GetNextArgType().Equals(typeof(Enuml).TypeHandle)) { Enuml e = __refvalue(args.GetNextArg(), Enuml); Console.WriteLine("Got Enuml, value = " + e + " = " + (long)e); S += (long)e; } } return S; }
static void TestRefValue (__arglist) { ArgIterator args = new ArgIterator (__arglist); var o = __refvalue ( args.GetNextArg (),int); for (int i = 0; i < args.GetRemainingCount (); i++) { Console.WriteLine (__refvalue (args.GetNextArg (), int)); } }
// Helper method for "TestArgIteratorGetType". private void TestTypes(String testNum, Type[] types, __arglist) { ArgIterator iter = new ArgIterator(__arglist); int count = iter.GetRemainingCount(); AssertEquals("Length " + testNum, types.Length, count); while(count > 0) { Type type = Type.GetTypeFromHandle (iter.GetNextArgType()); AssertEquals("TypeCheck " + testNum, types[types.Length - count], type); AssertEquals("Remaining " + testNum, count, iter.GetRemainingCount()); iter.GetNextArg(); --count; } try { iter.GetNextArgType(); Fail("EndCheck " + testNum); } catch(InvalidOperationException) { // We expect this exception at the end of the list. } AssertEquals("Remaining " + testNum, 0, iter.GetRemainingCount()); }
public static void argit1( __arglist ) { ArgIterator args = new ArgIterator( __arglist ); int iCount = args.GetRemainingCount(); for ( int i = 0; i < iCount + 15; i++ ){ try { try { TypedReference trTypRef = args.GetNextArg(); } catch (InvalidOperationException){} if (args.GetRemainingCount()!=0) if ( args.GetRemainingCount() != (iCount - i - 1) ){ throw new Exception( "ExcErr5 ,Should have had remaining count " + (iCount - i - 1) + " but had remaining count " + args.GetRemainingCount() ); } } catch(Exception ex){ if ( i < iCount ) Console.WriteLine( "Err_argit4_00: Loop has not gone through only " + i + " arguments" ); int iRemCount = args.GetRemainingCount(); if ( iRemCount != 0 ){ Console.WriteLine( "Err_argit4_01: Should have had remaining count 0 but had remaining count " + iRemCount ); } throw ex; } } if ( args.GetRemainingCount() != 0 ){ throw new Exception( "ExcErr4 ,Should have had remaining count 0"); } }
public static void argit2b (__arglist) { ArgIterator args = new ArgIterator( __arglist ); int hashcode = args.GetHashCode(); TypedReference trTypRef = args.GetNextArg(); int hashcodecompare= args.GetHashCode(); if (! hashcode.Equals(hashcodecompare)) { throw new Exception( "argit2b - 1, hashcode changed" ); } }
//Test 1 // check whether GetHashCode returns the same value // check to see if we can use ArgIterators on any class // check ArgIterators on Arrays // test arg iterators with instance methods on normal classes public TestStruct argit1( Object[] objExpectedArr, __arglist ) { ArgIterator args = new ArgIterator( __arglist ); int hashcode = args.GetHashCode(); int iCount = args.GetRemainingCount(); Console.WriteLine( "Found "+ iCount +" arguments. " ); Object[] objArr = new Object[iCount]; for ( int i = 0; i < iCount; i++ ){ objArr[i] = TypedReference.ToObject(args.GetNextArg()); Console.WriteLine( "Object " + i + ": " + objArr[i] + " Expected: " + objExpectedArr[i] ); if ( objExpectedArr[i] == null ){ if ( objArr[i] != null ){ throw new Exception( "argit1 - 1, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } else if ( ! objArr[i].Equals( objExpectedArr[i] ) ) { throw new Exception( "argit1 - 2, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } //repeating the code above __arglist should be valid in method scope ArgIterator args2 = new ArgIterator( __arglist ); int iCount2 = args2.GetRemainingCount(); Object[] objArr2 = new Object[iCount]; for ( int i = 0; i < iCount2; i++ ){ objArr2[i] = TypedReference.ToObject(args2.GetNextArg()); Console.WriteLine( "Object " + i + ": " + objArr2[i] + " Expected: " + objExpectedArr[i] ); if ( objExpectedArr[i] == null ){ if ( objArr2[i] != null ){ throw new Exception( "argit1 - 3, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } else if ( ! objArr2[i].Equals( objExpectedArr[i] ) ) { throw new Exception( "argit1 - 4, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } int hashcodecompare= args.GetHashCode(); if (! hashcode.Equals(hashcodecompare)) { throw new Exception( "argit1 - 5, hashcode changed" ); } TestStruct Bar = new TestStruct(); Bar.foo1 = 1; Bar.foo2 = 2; return Bar; }
static void X(__arglist) // 仮引数のところに __arglist を書く { // 中身のとりだしには ArgIterator 構造体を使う ArgIterator argumentIterator = new ArgIterator(__arglist); while (argumentIterator.GetRemainingCount() > 0) { object value = null; var r = argumentIterator.GetNextArg(); // 可変個引数の1個1個は TypedReference になっている var t = __reftype(r); // TypedReference から、元の型を取得 // 型で分岐して、__refvalue で値の取り出し if (t == typeof(int)) value = __refvalue(r, int); else if (t == typeof(char)) value = __refvalue(r, char); else if (t == typeof(double)) value = __refvalue(r, double); else value = __refvalue(r, string); Console.WriteLine(t.Name + ": " + value); } }
public void argit1( Object[] objExpectedArr, __arglist ) { ArgIterator args = new ArgIterator( __arglist ); int hashcode = args.GetHashCode(); int iCount = args.GetRemainingCount(); Object[] objArr = new Object[iCount]; for ( int i = 0; i < iCount; i++ ){ objArr[i] = TypedReference.ToObject(args.GetNextArg()); if ( objExpectedArr[i] == null ){ if ( objArr[i] != null ){ throw new Exception( "argit1 - 1, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } else if ( ! objArr[i].Equals( objExpectedArr[i] ) ) { throw new Exception( "argit1 - 2, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } ArgIterator args2 = new ArgIterator( __arglist ); int iCount2 = args2.GetRemainingCount(); Object[] objArr2 = new Object[iCount]; for ( int i = 0; i < iCount2; i++ ){ objArr2[i] = TypedReference.ToObject(args2.GetNextArg()); if ( objExpectedArr[i] == null ){ if ( objArr2[i] != null ){ throw new Exception( "argit1 - 3, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } else if ( ! objArr2[i].Equals( objExpectedArr[i] ) ) { throw new Exception( "argit1 - 4, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } int hashcodecompare= args.GetHashCode(); if (! hashcode.Equals(hashcodecompare)) { throw new Exception( "argit1 - 5, hashcode changed" ); } }
public static void argit1(__arglist) { ArgIterator args = new ArgIterator(__arglist); TypedReference trTypRef = args.GetNextArg(); }
public static int printf(sbyte *_Format, __arglist) { StringBuilder output = new StringBuilder(); ArgIterator ai = new ArgIterator(__arglist); string format = new string(_Format); int i = 0, n = format.Length; bool after_parse_width = false; while (i < n) { char c = format[i]; if (c == '%' || after_parse_width) { if (i + 1 <= n) { i++; char next = format[i]; switch (next) { // ignore width for now. case 'd': i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); break; case 'c': i++; output.Append(getChar(ai)); break; case 'x': i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); break; case 's': i++; output.Append(getString(ai)); break; case 'f': i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); break; case 'l': i++; if (format[i] == 'f') { i++; output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); } else { output.AppendFormat("{0}", TypedReference.ToObject(ai.GetNextArg())); } break; default: if (Char.IsDigit(next)) { StringBuilder sb = new StringBuilder(); while (i < n && Char.IsDigit(format[i])) { sb.Append(format[i]); i++; } width = int.Parse(sb.ToString()); width2 = 0; if (format[i] == '.') { i++; StringBuilder sb2 = new StringBuilder(); while (i < n && Char.IsDigit(format[i])) { sb2.Append(format[i]); i++; } width2 = int.Parse(sb2.ToString()); } } break; } } else { break; // break the loop } } else if (c == '\\') { // it is impossible for the next char to be out of bound. i++; char escaped = format[i]; output.Append(c); output.Append(escaped); i++; } else { output.Append(c); i++; } } Console.Write(output.ToString()); return(0); }
public MyStruct SumAndCount(int a, int b, __arglist) { Console.WriteLine("This type: " + this.GetType().ToString()); MyStruct t = new MyStruct(); ArgIterator args = new ArgIterator(__arglist); t.x += (a + b); t.y += args.GetRemainingCount(); while(args.GetRemainingCount() > 0) { if (args.GetNextArgType().Equals(typeof(int).TypeHandle)) { int N = __refvalue(args.GetNextArg(), int); Console.WriteLine("int value is "+N); t.x = t.x + N; } else if (args.GetNextArgType().Equals(typeof(byte).TypeHandle)) { byte N = __refvalue(args.GetNextArg(), byte); Console.WriteLine("byte value is "+N); t.x = t.x + N; } else if (args.GetNextArgType().Equals(typeof(short).TypeHandle)) { short N = __refvalue(args.GetNextArg(), short); Console.WriteLine("short value is "+N); t.x = t.x + N; } else if (args.GetNextArgType().Equals(typeof(long).TypeHandle)) { TypedReference tr = args.GetNextArg(); Interlocked.Increment(ref __refvalue(tr, long)); long N = __refvalue(tr, long); Console.WriteLine("long value is "+N); t.x = t.x + (int)(N % 57); } else if (args.GetNextArgType().Equals(typeof(float).TypeHandle)) { float N = __refvalue(args.GetNextArg(), float); Console.WriteLine("float value is "+N); t.x = t.x + (int)N; } else if (args.GetNextArgType().Equals(typeof(double).TypeHandle)) { double N = __refvalue(args.GetNextArg(), double); Console.WriteLine("double value is "+N); t.x = t.x + (int)N; } else if (args.GetNextArgType().Equals(typeof(Struct1).TypeHandle)) { Struct1 N = __refvalue(args.GetNextArg(), Struct1); Console.WriteLine("Struct1 value is "+N.x); t.x = t.x + N.x; } else throw new Exception(); } return t; }
public MyStruct SumAndCount(int a, int b, __arglist) { Console.WriteLine("This type: " + this.GetType().ToString()); MyStruct t = new MyStruct(); ArgIterator args = new ArgIterator(__arglist); t.x += (a + b); t.y += args.GetRemainingCount(); while (args.GetRemainingCount() > 0) { if (args.GetNextArgType().Equals(typeof(int).TypeHandle)) { int N = __refvalue(args.GetNextArg(), int); Console.WriteLine("int value is " + N); t.x = t.x + N; } else if (args.GetNextArgType().Equals(typeof(byte).TypeHandle)) { byte N = __refvalue(args.GetNextArg(), byte); Console.WriteLine("byte value is " + N); t.x = t.x + N; } else if (args.GetNextArgType().Equals(typeof(short).TypeHandle)) { short N = __refvalue(args.GetNextArg(), short); Console.WriteLine("short value is " + N); t.x = t.x + N; } else if (args.GetNextArgType().Equals(typeof(long).TypeHandle)) { TypedReference tr = args.GetNextArg(); Interlocked.Increment(ref __refvalue(tr, long)); long N = __refvalue(tr, long); Console.WriteLine("long value is " + N); t.x = t.x + (int)(N % 57); } else if (args.GetNextArgType().Equals(typeof(float).TypeHandle)) { float N = __refvalue(args.GetNextArg(), float); Console.WriteLine("float value is " + N); t.x = t.x + (int)N; } else if (args.GetNextArgType().Equals(typeof(double).TypeHandle)) { double N = __refvalue(args.GetNextArg(), double); Console.WriteLine("double value is " + N); t.x = t.x + (int)N; } else if (args.GetNextArgType().Equals(typeof(Struct1).TypeHandle)) { Struct1 N = __refvalue(args.GetNextArg(), Struct1); Console.WriteLine("Struct1 value is " + N.x); t.x = t.x + N.x; } else { throw new Exception(); } } return(t); }
public static void argit1( __arglist ) { ArgIterator args = new ArgIterator( __arglist ); TypedReference trTypRef = args.GetNextArg(); }
// Helper method for "TestArgIteratorGetValueByType". private void TestByType(String testNum, Type[] types, __arglist) { ArgIterator iter = new ArgIterator(__arglist); int count = iter.GetRemainingCount(); AssertEquals("Length " + testNum, types.Length, count); while(count > 0) { Type type = types[types.Length - count]; Object value = TypedReference.ToObject (iter.GetNextArg(type.TypeHandle)); Type valueType = value.GetType(); AssertEquals("TypeCheck " + testNum, type, valueType); --count; AssertEquals("Remaining " + testNum, count, iter.GetRemainingCount()); } try { iter.GetNextArg(typeof(double).TypeHandle); Fail("EndCheck " + testNum); } catch(InvalidOperationException) { // We expect this exception at the end of the list. } AssertEquals("Remaining " + testNum, 0, iter.GetRemainingCount()); // Reset the list and check for the wrong type. iter = new ArgIterator(__arglist); try { iter.GetNextArg(typeof(long).TypeHandle); Fail("TypeCheck 2 " + testNum); } catch(InvalidOperationException) { // This is what we expected. } }
public static void argit0( __arglist ) { ArgIterator args = new ArgIterator( __arglist ); int iCount = args.GetRemainingCount(); for ( int i = 0; i < iCount + 15; i++ ) { try { TypedReference trTypRef = args.GetNextArg(); if ( args.GetRemainingCount() != (iCount - i - 1) ) { throw new Exception( "ExcErr5 ,Should have had remaining count " + (iCount - i - 1) + " but had remaining count " + args.GetRemainingCount() ); } } catch(Exception ex) { if ( i < iCount ) { Console.WriteLine( i ); throw ex; } int iRemCount = args.GetRemainingCount(); if ( iRemCount != 0 ) { throw new Exception( "ExcErr3 ,Should have had remaining count 0 but had remaining count " + iRemCount ); } } } if ( args.GetRemainingCount() != 0 ) { throw new Exception( "ExcErr4 ,Should have had remaining count 0"); } }
public Object[] argit6( Object[] objExpectedArr, __arglist ) { ArgIterator args = new ArgIterator( __arglist ); int iCount = args.GetRemainingCount(); Object[] objArr = new Object[iCount]; for ( int i = 0; i < iCount; i++ ) { objArr[i] = TypedReference.ToObject(args.GetNextArg()); if ( objExpectedArr[i] == null ) { if ( objArr[i] != null ) { throw new Exception( "argit6 - 1, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } else if ( ! objExpectedArr[i].Equals( objExpectedArr[i] ) ) { throw new Exception( "argit6 - 2, __arglist[i] was null but it did not equal to objExpectedArr[i]" ); } } return objArr; }
//Test 1 // check whether GetHashCode returns the same value // check to see if we can use ArgIterators on any class // check ArgIterators on Arrays // test arg iterators with instance methods on normal classes public TestStruct argit1(Object[] objExpectedArr, __arglist) { ArgIterator args = new ArgIterator(__arglist); int hashcode = args.GetHashCode(); int iCount = args.GetRemainingCount(); Console.WriteLine("Found " + iCount + " arguments. "); Object[] objArr = new Object[iCount]; for (int i = 0; i < iCount; i++) { objArr[i] = TypedReference.ToObject(args.GetNextArg()); Console.WriteLine("Object " + i + ": " + objArr[i] + " Expected: " + objExpectedArr[i]); if (objExpectedArr[i] == null) { if (objArr[i] != null) { throw new Exception("argit1 - 1, __arglist[i] was null but it did not equal to objExpectedArr[i]"); } } else if (!objArr[i].Equals(objExpectedArr[i])) { throw new Exception("argit1 - 2, __arglist[i] was null but it did not equal to objExpectedArr[i]"); } } //repeating the code above __arglist should be valid in method scope ArgIterator args2 = new ArgIterator(__arglist); int iCount2 = args2.GetRemainingCount(); Object[] objArr2 = new Object[iCount]; for (int i = 0; i < iCount2; i++) { objArr2[i] = TypedReference.ToObject(args2.GetNextArg()); Console.WriteLine("Object " + i + ": " + objArr2[i] + " Expected: " + objExpectedArr[i]); if (objExpectedArr[i] == null) { if (objArr2[i] != null) { throw new Exception("argit1 - 3, __arglist[i] was null but it did not equal to objExpectedArr[i]"); } } else if (!objArr2[i].Equals(objExpectedArr[i])) { throw new Exception("argit1 - 4, __arglist[i] was null but it did not equal to objExpectedArr[i]"); } } int hashcodecompare = args.GetHashCode(); if (!hashcode.Equals(hashcodecompare)) { throw new Exception("argit1 - 5, hashcode changed"); } TestStruct Bar = new TestStruct(); Bar.foo1 = 1; Bar.foo2 = 2; return(Bar); }
// Helper method for "TestArgIteratorGetValue". private void TestValues(String testNum, Object[] values, __arglist) { Object expected, actual; ArgIterator iter = new ArgIterator(__arglist); int count = iter.GetRemainingCount(); AssertEquals("Length " + testNum, values.Length, count); while(count > 0) { expected = values[values.Length - count]; actual = TypedReference.ToObject(iter.GetNextArg()); if(expected == null) { AssertNull("ValueCheck " + testNum, actual); } else { Assert("ValueCheck " + testNum, expected.Equals(actual)); } --count; AssertEquals("Remaining " + testNum, count, iter.GetRemainingCount()); } try { iter.GetNextArg(); Fail("EndCheck " + testNum); } catch(InvalidOperationException) { // We expect this exception at the end of the list. } AssertEquals("Remaining " + testNum, 0, iter.GetRemainingCount()); // Restart and run the test again to make sure that // the first iteration did not modify the vararg values. iter = new ArgIterator(__arglist); count = iter.GetRemainingCount(); AssertEquals("Length " + testNum, values.Length, count); while(count > 0) { expected = values[values.Length - count]; actual = TypedReference.ToObject(iter.GetNextArg()); if(expected == null) { AssertNull("ValueCheck " + testNum, actual); } else { Assert("ValueCheck " + testNum, expected.Equals(actual)); } --count; AssertEquals("Remaining " + testNum, count, iter.GetRemainingCount()); } try { iter.GetNextArg(); Fail("EndCheck " + testNum); } catch(InvalidOperationException) { // We expect this exception at the end of the list. } AssertEquals("Remaining " + testNum, 0, iter.GetRemainingCount()); }
// Helper method for "TestArgIteratorEnd". private void TestEnd(String testNum, __arglist) { ArgIterator iter = new ArgIterator(__arglist); iter.End(); AssertEquals("Remaining " + testNum, 0, iter.GetRemainingCount()); try { iter.GetNextArg(); Fail("EndCheck " + testNum); } catch(InvalidOperationException) { // We expect this exception at the end of the list. } }