Re: Template instantiation

2009-03-20 Thread Jarrett Billingsley
On Fri, Mar 20, 2009 at 11:35 AM, Trass3r  wrote:
> Got the following code:
>
> public LuaState wrapClass (T) (T instance)
> {
>        auto ptr = cast (T *) newUserdata ( (T *).sizeof);
>        *ptr = instance;
>
>        loadClassMetatable (typeid(T).toString);
>        setMetatable (-2);
>        return this;
> }
>
> Am I right in assuming that a different wrapClass will be created in the
> final executable for each template instantiation (e.g. calls with class
> A,B,C,... instances) and would thus bloat up the executable a bit when used
> with many classes?

Yes.

> Apart from that, couldn't you just use wrapClass (Object instance)? In the
> end each class instance pointer takes up the same amount of memory?!
>

I'd say yes.  It doesn't look like there's any need for a template here.

It wouldn't be (Object*).sizeof when allocating the userdata, though;
just Object.sizeof.


Re: Universel toString

2009-03-20 Thread Qian Xu
The problem has been solved.

There is a wonderfull function in Tango.


import tango.text.convert.Format;

class Foo {
  // attributes go here...
  public char[] toString() {
return Format.convert(" {} .. {} ..", attr1, attr2, attr3);
  }
}


This works perfect with all data types.



Template instantiation

2009-03-20 Thread Trass3r

Got the following code:

public LuaState wrapClass (T) (T instance)
{
auto ptr = cast (T *) newUserdata ( (T *).sizeof);
*ptr = instance;

loadClassMetatable (typeid(T).toString);
setMetatable (-2);
return this;
}

Am I right in assuming that a different wrapClass will be created in the 
final executable for each template instantiation (e.g. calls with class 
A,B,C,... instances) and would thus bloat up the executable a bit when 
used with many classes?


Apart from that, couldn't you just use wrapClass (Object instance)? In 
the end each class instance pointer takes up the same amount of memory?!


Re: Universel toString

2009-03-20 Thread Qian Xu
Daniel Keep wrote:
> 
> to!(char[]) should call toString.  to!(T) should support all atomic
> types, strings, structs and classes.
> 

There is one problem: I have to check, whether a pointer is NULL. 
If it is NULL, I should return "NULL", otherwise I should call to!(char[]
(*my_pointer_var)

I want to save this check.



Re: Universel toString

2009-03-20 Thread Daniel Keep


grauzone wrote:
> Daniel Keep wrote:
>>
>> Qian Xu wrote:
>>> Hi All,
>>>
>>> I want to write an universel toString() method for debugging propose.
>>> However I cannot write in one version. The compiler says error all
>>> the time.
>>> Could someone tell me, how to write an universel one?
>>>
>>> What I want, is to convert primtive types (int, int*, bool, bool*,
>>> etc.) to
>>> string using "tango.text.Convert.to()" and convert object to string by
>>> calling obj.toString.
>>>
>>> ...
>>>
>>> Best regards
>>> --Qian Xu
>>
>> to!(char[]) should call toString.  to!(T) should support all atomic
>> types, strings, structs and classes.
> 
> So to!(char[])(x) almost like format("%s", x)?
> 
>>   -- Daniel

Probably something like
tango.text.convert.Format.Format.convert("{0}",x) for Tango.

Actually, that's not entirely true.  to!(char[]) won't convert pointers
or arrays.

Sorry; the brain switched off as soon as I saw "I have to special-case
to" :P

  -- Daniel


Re: Universel toString

2009-03-20 Thread grauzone

Daniel Keep wrote:


Qian Xu wrote:

Hi All,

I want to write an universel toString() method for debugging propose.
However I cannot write in one version. The compiler says error all the time.
Could someone tell me, how to write an universel one?

What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to
string using "tango.text.Convert.to()" and convert object to string by
calling obj.toString.

...

Best regards
--Qian Xu


to!(char[]) should call toString.  to!(T) should support all atomic
types, strings, structs and classes.


So to!(char[])(x) almost like format("%s", x)?


  -- Daniel


Re: Universel toString

2009-03-20 Thread Daniel Keep


Qian Xu wrote:
> Hi All,
> 
> I want to write an universel toString() method for debugging propose.
> However I cannot write in one version. The compiler says error all the time.
> Could someone tell me, how to write an universel one?
> 
> What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to
> string using "tango.text.Convert.to()" and convert object to string by
> calling obj.toString.
>
> ...
> 
> Best regards
> --Qian Xu

to!(char[]) should call toString.  to!(T) should support all atomic
types, strings, structs and classes.

  -- Daniel


Universel toString

2009-03-20 Thread Qian Xu
Hi All,

I want to write an universel toString() method for debugging propose.
However I cannot write in one version. The compiler says error all the time.
Could someone tell me, how to write an universel one?

What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to
string using "tango.text.Convert.to()" and convert object to string by
calling obj.toString.


--- my current version -
public char[] toS(int* val)
{
if (val is null)
return "NULL";
return to!(char[])(*val);
}

public char[] toS(Object val)
{
if (val is null)
return "NULL";
return val.toString;
}

public char[] toS(char[] val)
{
if (val is null)
return "NULL";
return val;
}

public char[] toS(int val)
{
return to!(char[])(val);
}

public char[] toS(bool val)
{
return to!(char[])(val);
}
--- my current version -



Best regards
--Qian Xu


Re: Factory class

2009-03-20 Thread KeepYourMind
dennis luehring Wrote:

> > void main()
> > {
> > auto pizza = PizzaFactory.factory();
> > pizza.doSome(); // OK
> > pizza.doSomeA(); // error: IPizza.doSomeA() not found
> > }
> 
> you interface has only doSome() and that is what factory returns
> 
> and for all others: factories and cast don't belong together
> 
> you interface should fit your overall needs

Got it. Thanks to all.


Re: Factory class

2009-03-20 Thread KeepYourMind
Denis Koroskin Wrote:

> On Thu, 19 Mar 2009 22:58:02 +0300, Simen Kjaeraas  
> wrote:
> 
> > On Thu, 19 Mar 2009 18:06:37 +0100, KeepYourMind  
> >  wrote:
> >
> >> Sean Kelly Wrote:
> >>
> >>> KeepYourMind wrote:
> >>> >
> >>> > What i need to do for pizza.doSomeA() begin work?
> >>>
> >>> cast pizza to CoolPizza.
> >>
> >> Problem is i dont know what class returned. "char[] name" is dynamic  
> >> and getting from command-line arguments.
> >
> > Then you need some test to see if it really is a CoolPizza.
> >
> > void main( string[] args ) {
> > auto pizza = PizzaFactory.factory( args[1] );
> > pizza.doSome( );
> > if ( args[1] == "CoolPizza" ) {
> > ( cast( CoolPizza ) pizza ).doSomeA( );
> > }
> > }
> 
> Not like that. Here is a better way:
> 
> auto pizza = PizzaFactory.factory( name );
> pizza.doSome();
> if (auto cool = cast(CoolPizza)pizza) {
> cool.doSomeA();
> }
> 

Got it. If use one from this ways i'm not need factory, only switch. Bad. :-(