2 registered members (AndrewAMD, SBGuy),
987
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: C# wrapper - RELEASE
[Re: Stromausfall]
#316983
03/28/10 13:23
03/28/10 13:23
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
oh ok, well I had only recently discovered it while reading a C# book. It allows implicit conversion...
var a = 45;
var b = 7.86;
var c = "hello world";
var d = false;
Maybe that could help you with automatic var handling?
|
|
|
Re: C# wrapper - RELEASE
[Re: DJBMASTER]
#317009
03/28/10 16:11
03/28/10 16:11
|
Joined: Dec 2006
Posts: 434 UK,Terra, SolarSystem, Milky W...
pararealist
Senior Member
|
Senior Member
Joined: Dec 2006
Posts: 434
UK,Terra, SolarSystem, Milky W...
|
I see, Var.IntValue is a little too much sometimes, find myself forgetting them a lot, expecially when i return from C++ project.
A8.3x Commercial, AcknexWrapper and VS 2010 Express ○pararealist now.
|
|
|
Re: C# wrapper - RELEASE
[Re: pararealist]
#317470
03/31/10 20:38
03/31/10 20:38
|
Joined: Dec 2002
Posts: 616 Austria
Stromausfall
OP
User
|
OP
User
Joined: Dec 2002
Posts: 616
Austria
|
the current Var object also allows implicit conversion for example Var xx = 30; but the main reason why 'Var' is indispensable at the moment, is, that certain functions like "pan_setneedle" take a REFERENCE to a variable ! and in c# it's not possible (at least not user friendly afaik) to get the reference to a value type, like int, var, or float ! and if one would only use implicit conversion, for example : Var xx = 30; and then give pan_setneedle the xx variable and then for example change xx like this : xx = 400 + 70; pan_setneedle would NOT take notice of the change, as xx references now to another new variable... but nonetheless, the wrapper already uses implicit conversion, thus float value1 = 3.4f; int bla = minv(value1, 100); and int currentTimeStepValue = EngVar.time_step; int currentTimeStepValue2 = EngVar.time_step.IntValue; should work fine afaik! I already tried to use ventilator's suggestion - lambda expressions, but they don't seem to be able to hold references to value types in c#
|
|
|
Re: C# wrapper - RELEASE
[Re: ventilator]
#317487
03/31/10 21:31
03/31/10 21:31
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
ooo, i thought getting a reference to a value type was as easy as supplying the 'ref' keyword...
public void ChangeMe(ref int i)
{
i++;
}
...
int x = 3;
ChangeMe(ref x);
// now x = 4
It's probably alot more complex than that, lol. Lambdas seem pretty powerful, although havent really played with them.
|
|
|
Re: C# wrapper - RELEASE
[Re: DJBMASTER]
#317520
04/01/10 02:07
04/01/10 02:07
|
Joined: Dec 2002
Posts: 616 Austria
Stromausfall
OP
User
|
OP
User
Joined: Dec 2002
Posts: 616
Austria
|
here's an example for a lambda expression in c# :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lambdaTest
{
class Program
{
delegate int getValueOfReference();
static getValueOfReference newReferenceStorage = null;
static void dummy(int valueInteger)
{
newReferenceStorage = () => { return valueInteger; };
//that's how it should look like - but unfortunately doesn't work in c#
//newReferenceStorage = () => { return refInteger; };
}
static void Main(string[] args)
{
int refInteger = 3;
int valueInteger = 4;
dummy(ref refInteger, valueInteger);
valueInteger++;
//when newReferenceStorage is called, the anonymous method is called..
Console.WriteLine("variable : " + valueInteger);
Console.WriteLine("lambda expresssion : " + newReferenceStorage());
//give some time to read the output...
Console.ReadLine();
}
}
}
Meanwhile I was able to further reduce the lines of code required for a experimental method (as mentioned : "at least not user friendly") which allows to store the reference of a value type. Although this method has some drawbacks : 1. the code needed from the user...
int dummyVariable = 3;
EngFun.pan_setvar(
dummyPanel,
1,
1,
delegate() { Reference.acquire(ref dummyVariable); }
);
//or (the same)
EngFun.pan_setvar(
dummyPanel,
1,
1,
() => { Reference.acquire(ref dummyVariable); }
);
2. for each variable that is stored, one thread needs to be created -> overhead 3. the method pins/fixes the position of the variable in the memory, thus it disables the object, of which the variable is a member variable, to be moved from the garbage collector, which isn't preferrable.... My conclusion is that taking a reference from a variable still isn't a solution One way to deal with this problem would be, to remove the Var object alltogether and replace them with doubles (properties). And to create a special object/class for the panel functions that use a pointer to a variable - like the current Var object. Thus such a special class would only be needed for these panel functions, and everywhere else doubles (properties) would be used... edit : -> thus the Var object would only be used in the panel functions that need a pointer to a variable..
Last edited by Stromausfall; 04/01/10 02:10.
|
|
|
Re: C# wrapper - RELEASE
[Re: Stromausfall]
#317537
04/01/10 07:58
04/01/10 07:58
|
Joined: Dec 2002
Posts: 616 Austria
Stromausfall
OP
User
|
OP
User
Joined: Dec 2002
Posts: 616
Austria
|
Finally I got an idea, how to store references to a value type, without the need of a Thread. This method would/could use ventilator's suggested idea, of keeping an array for all variables that should be updated, in/because of these panel functions and update them at the beginning/end of a frame... The downside is, that the code for the user to enter isn't that beautiful (two lambdas, if the panel function would read AND write into the variable)...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class ReferenceStorage
{
public delegate double getValue();
public delegate void setValue(double value);
public getValue getValueLambda;
public setValue setValueLambda;
public ReferenceStorage(getValue getValueLambda, setValue setValueLambda)
{
this.getValueLambda = getValueLambda;
this.setValueLambda = setValueLambda;
}
}
class Program
{
/* this method should depict the engine, changing/reading through the reference */
static void backgroundFunction(ReferenceStorage.getValue getValue, ReferenceStorage.setValue setValue)
{
/*
* if a permanent reference to that variable would be needed,
* create a Reference Storage, to keep the two Lambda expressions
*
* code :
*
* ReferenceStorage newStorage = new ReferenceStorage(getValue, setValue);
*/
while (true)
{
setValue(getValue() + 1);
Console.WriteLine(getValue() + " increased, using lambda expression");
System.Threading.Thread.Sleep(1500);
}
}
/* a method that would be called from the user */
static void dummyWrapperFunction(ReferenceStorage.getValue getValue, ReferenceStorage.setValue setValue)
{
System.Threading.Thread t = new System.Threading.Thread(
() => { backgroundFunction(getValue, setValue); }
);
t.Start();
}
static void Main(string[] args)
{
double dummyValue = 30;
/* call a dummy wrapper function and pass a 'reference' to the value type dummyValue
*
* THIS is the code that user would have to enter, if the panel function would read AND write into the variable
*/
dummyWrapperFunction(
() => { return dummyValue; },
(double newDummyValue) => { dummyValue = newDummyValue; }
);
while (true)
{
dummyValue += 1;
Console.WriteLine(dummyValue + " increaesd, through directly increasing the variable");
System.Threading.Thread.Sleep(1500);
}
}
}
}
This could/would most likely be preferable over the two other options I mentioned, although it's not that easy for the user to use.... edit: I'm currently always referring to double as the new variable type that could replace Var, but what I'm asking myself is, wouldn't a float suffice, as the value range of Var is already way smaller than float ? pros for float in my opinion would be, a double isn't really needed and float should be faster in general than double I think cons for float would be, that the usage of float is ugly to say the least... for example, the following statement is not allowed : it has to be :
Last edited by Stromausfall; 04/01/10 08:13.
|
|
|
|