Re: Get class size of object

2013-08-12 Thread John Colvin

On Sunday, 11 August 2013 at 18:29:15 UTC, JS wrote:

On Sunday, 11 August 2013 at 18:18:22 UTC, Dicebot wrote:

On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:
This is essentially what I'm doing BUT I am trying to avoid 
having to repeat the exact same crap in every class because 
it is time consuming, verbose, and error prone.


I was answering to Maxim, I don't give a fuck about your 
problems.


THEN WHY THE FUCK DO YOU POST ON MY THREAD? You have not helped 
or tried to help one post. You have only ever tried to shut 
down any questions or suggestions I have based on your arrogant 
belief system that everyone and everything revolves around 
you... yet you continue to invade my posts. Seriously, Why 
can't you just leave me the fuck along and ignore my posts? Is 
it difficult? No, it's not. This suggests you intentionally 
want to make trouble. In any case, I'm through talking to you 
bastards. I will ignore your post from now on. In fact, I'll 
write a script at some point to simply remove your posts, 
Tim's, and others that want to act like little fucking arrogant 
kids that haven't got their ass kicked hard enough to realize 
they are not the center of the universe.


I am actually coming round to seeing the funny side :-) It's like 
a toddler screaming and throwing soft toys at people


Re: Get class size of object

2013-08-12 Thread Jacob Carlborg

On 2013-08-11 21:08, Simen Kjaeraas wrote:


If you're looking for a no-overhead solution, then this might not be
good enough. I'm surprised that a virtual function call is fine,
though.


How about this:

interface I
{
size_t size (this T) ()
{
return __traits(classInstanceSize, T);
}
}

class A : I
{
}

class B : I
{
int a;
}

void main ()
{
auto a = new A;
auto b = new B;

assert(a.size == 24);
assert(b.size == 32);
}

--
/Jacob Carlborg


Re: Get class size of object

2013-08-12 Thread JS

On Monday, 12 August 2013 at 11:34:25 UTC, Jacob Carlborg wrote:

On 2013-08-11 21:08, Simen Kjaeraas wrote:

If you're looking for a no-overhead solution, then this might 
not be
good enough. I'm surprised that a virtual function call is 
fine,

though.


How about this:

interface I
{
size_t size (this T) ()
{
return __traits(classInstanceSize, T);
}
}

class A : I
{
}

class B : I
{
int a;
}

void main ()
{
auto a = new A;
auto b = new B;

assert(a.size == 24);
assert(b.size == 32);
}


Sorry but this doesn't work. b is a B, not an A.

try

I a = new A;
I b = new B;







Re: Get class size of object

2013-08-12 Thread Jacob Carlborg

On 2013-08-12 17:49, JS wrote:


Sorry but this doesn't work. b is a B, not an A.

try

I a = new A;
I b = new B;


Right, forgot about the interface.

--
/Jacob Carlborg


Re: Get class size of object

2013-08-12 Thread JS

On Monday, 12 August 2013 at 19:44:56 UTC, Jacob Carlborg wrote:

On 2013-08-12 17:49, JS wrote:


Sorry but this doesn't work. b is a B, not an A.

try

I a = new A;
I b = new B;


Right, forgot about the interface.



NP, happens to all of us. Simen's method of casting to object 
seems to be the easiest way to get it to work.


Re: Get class size of object

2013-08-11 Thread Tobias Pankrath

On 11.08.2013 06:25, JS wrote:

Given an object, is there a built in way to get the size of the class
the object represents? The object may be instantiated with a derived
instance of the object type, so using classInstanceSize doesn't work on
the type of the object.

I can obviously go through the trouble of adding a class member and
override it in all the derived classes. I saw tsize in object but 2.063
says it doesn't exist.



There is no general way to do this at compile time in any language.


Re: Get class size of object

2013-08-11 Thread Timon Gehr

On 08/11/2013 06:25 AM, JS wrote:

Given an object, is there a built in way to get the size of the class
the object represents?


Yes.


Re: Get class size of object

2013-08-11 Thread Andrej Mitrovic
On 8/11/13, JS js.m...@gmail.com wrote:
 Given an object, is there a built in way to get the size of the
 class the object represents?

Try:
import core.memory;
auto size = GC.sizeOf(object);


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:

On 08/11/2013 06:25 AM, JS wrote:
Given an object, is there a built in way to get the size of 
the class

the object represents?


Yes.


Troll.


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 09:30:32 UTC, Tobias Pankrath wrote:

On 11.08.2013 06:25, JS wrote:
Given an object, is there a built in way to get the size of 
the class
the object represents? The object may be instantiated with a 
derived
instance of the object type, so using classInstanceSize 
doesn't work on

the type of the object.

I can obviously go through the trouble of adding a class 
member and
override it in all the derived classes. I saw tsize in object 
but 2.063

says it doesn't exist.



There is no general way to do this at compile time in any 
language.


Is this a mathematical fact that you have proven for all 
languages? I'd like to see your proof if it's not bigger than 
what will fit in a post.


Re: Get class size of object

2013-08-11 Thread Tobias Pankrath
Is this a mathematical fact that you have proven for all 
languages? I'd like to see your proof if it's not bigger than 
what will fit in a post.


That's so obvious, that you should be able to come up with one 
yourself. Maybe you never experienced a situation where it was 
clear, but that doesn't mean I need to come up with a proof, just 
because you are to lazy to do it yourself.




Re: Get class size of object

2013-08-11 Thread Maxim Fomin

On Sunday, 11 August 2013 at 15:28:44 UTC, JS wrote:

On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:

On 08/11/2013 06:25 AM, JS wrote:
Given an object, is there a built in way to get the size of 
the class

the object represents?


Yes.


Troll.


I guess he does not answer to the question as well as others do 
(including me) because of your misbehavior in D newsgroups. 
Solution to your question is obvious (built-in feature without 
any import). By the way, GC.sizeOf seems to return size of 
allocated page which is bigger than needed (for ex. it returns 16 
for a new int which is 4).




Re: Get class size of object

2013-08-11 Thread bearophile

Maxim Fomin:

GC.sizeOf seems to return size of allocated page which is 
bigger than needed (for ex. it returns 16 for a new int which 
is 4).


The object instance contains a pointer to the virtual table, so 
there's a way to know what object it is. With that runtime 
information it should be possible to know the static size of the 
instance.


Bye,
bearophile


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 15:38:04 UTC, Tobias Pankrath wrote:
Is this a mathematical fact that you have proven for all 
languages? I'd like to see your proof if it's not bigger than 
what will fit in a post.


That's so obvious, that you should be able to come up with one 
yourself. Maybe you never experienced a situation where it was 
clear, but that doesn't mean I need to come up with a proof, 
just because you are to lazy to do it yourself.


No, but your simple wrong. Either you have no clue what you are 
talking about, don't understand the issue, or are intentionally 
stating falsehoods.


It's very easy to manually implement a way to get the class size 
by just using a virtual function that returns the class size. But 
do you think I should have to waste my time adding the same 
function to every class to make it work? Of course you do.




Re: Get class size of object

2013-08-11 Thread Maxim Fomin

On Sunday, 11 August 2013 at 16:16:26 UTC, bearophile wrote:

Maxim Fomin:

GC.sizeOf seems to return size of allocated page which is 
bigger than needed (for ex. it returns 16 for a new int which 
is 4).


The object instance contains a pointer to the virtual table, so 
there's a way to know what object it is. With that runtime 
information it should be possible to know the static size of 
the instance.


Bye,
bearophile


I do not know how you can calculate precisely object size by 
knowing that GC.sizeOf returns some aligned amount plus knowing 
something about virtual table. In case of simple objects (like 
ints) it does not work since int has no virtual table.


Anyway such guessing is useless since D provides feature to get 
size of class instance (if it is defined) even in comple time 
like enum S = _get_size ...


Re: Get class size of object

2013-08-11 Thread Dicebot

On Sunday, 11 August 2013 at 16:36:53 UTC, Adam D. Ruppe wrote:

On Sunday, 11 August 2013 at 04:25:21 UTC, JS wrote:
Given an object, is there a built in way to get the size of 
the class the object represents?


try this:

Object obj = new Whatever();
auto size = typeid(obj).init.length;


This.


Re: Get class size of object

2013-08-11 Thread Maxim Fomin

On Sunday, 11 August 2013 at 16:36:53 UTC, Adam D. Ruppe wrote:

On Sunday, 11 August 2013 at 04:25:21 UTC, JS wrote:
Given an object, is there a built in way to get the size of 
the class the object represents?


try this:

Object obj = new Whatever();
auto size = typeid(obj).init.length;


Yes, this is answer to the question. By the way, it can be simply

enum S = typeid(Whatever).init.length;


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 16:43:09 UTC, Dicebot wrote:

On Sunday, 11 August 2013 at 16:40:48 UTC, Maxim Fomin wrote:
Yes, this is answer to the question. By the way, it can be 
simply


enum S = typeid(Whatever).init.length;


If exact type is statically known, no ClassInfo is required:

enum S = __traits(classInstanceSize, Whatever);


This is essentially what I'm doing BUT I am trying to avoid 
having to repeat the exact same crap in every class because it is 
time consuming, verbose, and error prone.




For those that care, here is a snippet of what I am doing. For 
those that are bitches, GTFO. Remember, I'm a troll so everything 
I say or do here is irrelevant, so I don't even know why you are 
reading this in the first place.


http://dpaste.dzfl.pl/d295


Note that because it is a function call it is extremely expensive 
compared to a compile time literal. Disassembly shows that the 
actual size is a constant so the vtabl lookup and a function call 
is all that is required. I'd like to minimize the overhead as 
much as possible and, ideally not have to manually add the 
template mixins to each class(it would be nice if it were 
automated). I'm trying to reduce the costs of RT lookup as much 
as possible(as I feel it is not necessary).








Re: Get class size of object

2013-08-11 Thread Timon Gehr

On 08/11/2013 05:28 PM, JS wrote:

On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:

On 08/11/2013 06:25 AM, JS wrote:

Given an object, is there a built in way to get the size of the class
the object represents?


Yes.


Troll.


http://en.wikipedia.org/wiki/Troll_(Internet)


Re: Get class size of object

2013-08-11 Thread Adam D. Ruppe

On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:

For those that care, here is a snippet of what I am doing.


What are you actually going to use the number for? If you are 
going to write your own New function, you'll need the init 
anyway, so you aren't really paying more to get the size from it 
either.


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 17:12:48 UTC, Timon Gehr wrote:

On 08/11/2013 05:28 PM, JS wrote:

On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:

On 08/11/2013 06:25 AM, JS wrote:
Given an object, is there a built in way to get the size of 
the class

the object represents?


Yes.


Troll.


http://en.wikipedia.org/wiki/Troll_(Internet)


Seriously, do you not have anything better to do with your life? 
Why do you hang out on my posts if you are the one that's not the 
troll. I don't want any of your help so why don't you just do us 
all a favor and ignore any post I make?




Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 17:19:21 UTC, Adam D. Ruppe wrote:

On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:

For those that care, here is a snippet of what I am doing.


What are you actually going to use the number for? If you are 
going to write your own New function, you'll need the init 
anyway, so you aren't really paying more to get the size from 
it either.


What's the difference between init.length and classInstanceSize? 
The class instance size must be known at compile time and I don't 
see why there will be a difference and what little testing I have 
done shows no difference.




Re: Get class size of object

2013-08-11 Thread Dicebot

On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:
This is essentially what I'm doing BUT I am trying to avoid 
having to repeat the exact same crap in every class because it 
is time consuming, verbose, and error prone.


I was answering to Maxim, I don't give a fuck about your problems.


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 18:15:28 UTC, Timon Gehr wrote:

On 08/11/2013 07:26 PM, JS wrote:

On Sunday, 11 August 2013 at 17:12:48 UTC, Timon Gehr wrote:

On 08/11/2013 05:28 PM, JS wrote:

On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:

On 08/11/2013 06:25 AM, JS wrote:
Given an object, is there a built in way to get the size 
of the class

the object represents?


Yes.


Troll.


http://en.wikipedia.org/wiki/Troll_(Internet)


Seriously, do you not have anything better to do with your 
life?


No. How about you?



That explains it all. How bout you get a life?

Why do you hang out on my posts if you are the one that's not 
the troll.


That's obvious. You are the _first_ guy capably trolling these 
newsgroups, so we are still too naive to believe that you are 
not receptive to rational argumentation and incapable of 
constructive discussion.




Really? Because I won't except your help hands down. You are an 
arrogant bastard who thinks you know it all. When someone 
challenges you because they won't accept you as the god you then 
cry like a little bitch.



I don't want any of your help


The best way to not receive help is to quit asking for help.


Rigghhtt... your logic is wonderful. Don't ask for help in 
.learn. I guess you are trying to run me off because you can't 
stand me huh? Well the feeling is mutual... but at least there 
are a few intelligent people still left in here that actually 
want to help for the sake of helping and not to feed their ego.


so why don't you just do us all a favor and ignore any post I 
make?




I am as confused about this as you are but I can't talk for 
everyone.



What is _your_ motivation, 'JS'?
If you think your posts are worth ignoring, simply stop making 
noise.


Again, here is your very arrogant egotistical personality shining 
through(only other arrogant egotistical people will not see 
this). What is useless to one person is not to another. If you 
weren't so arrogant you would see this and let me make my posts 
in peace and let those that want to help me do that. Instead you 
rant against my posts in my thread to shut it down... but yet you 
call me the troll.




Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 18:18:22 UTC, Dicebot wrote:

On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:
This is essentially what I'm doing BUT I am trying to avoid 
having to repeat the exact same crap in every class because it 
is time consuming, verbose, and error prone.


I was answering to Maxim, I don't give a fuck about your 
problems.


THEN WHY THE FUCK DO YOU POST ON MY THREAD? You have not helped 
or tried to help one post. You have only ever tried to shut down 
any questions or suggestions I have based on your arrogant belief 
system that everyone and everything revolves around you... yet 
you continue to invade my posts. Seriously, Why can't you just 
leave me the fuck along and ignore my posts? Is it difficult? No, 
it's not. This suggests you intentionally want to make trouble. 
In any case, I'm through talking to you bastards. I will ignore 
your post from now on. In fact, I'll write a script at some point 
to simply remove your posts, Tim's, and others that want to act 
like little fucking arrogant kids that haven't got their ass 
kicked hard enough to realize they are not the center of the 
universe.


Re: Get class size of object

2013-08-11 Thread Dicebot

On Sunday, 11 August 2013 at 18:29:15 UTC, JS wrote:

THEN WHY THE FUCK DO YOU POST ON MY THREAD?


Because you are so funny when you rage and I take pleasure in 
stressing someone as retarded as you ;)


Well, actually not. It was just to clarify possible misconception 
by Maxim. Still feels good.


Re: Get class size of object

2013-08-11 Thread Simen Kjaeraas

On 2013-08-11, 20:33, JS wrote:

I think you're missing the point to some degree(I realize there is a  
diff between an object and a type, but I should be able to easily get  
the class size of an object at run time regardless if the object is  
typed as a base class). The code below does this, but at a cost of  
verbosity.


Here is code that exactly demonstrates what I am essentially trying to  
do. The only drawback now is having to mixin the template for each class  
which I would want to automate and it would be nice to wrap the RT and  
CT methods in subclasses without overhead.


It would appear that some information is lost when calling typeid from an
interface. I would have expected this to work:

interface I {
final
size_t size() {
return typeid(this).init.length;
}
}

class A {}

class B : A, I {
int a;
}

void test1() {
I i = new B();
assert(i.size  0); // Fails.
}


A bit more experimentation shows:

void test2() {
B b = new B();
I i = b;
A a = b;

assert(typeid(a) == typeid(b)); // Passes.
assert(typeid(i) == typeid(b)); // Fails.

assert(typeid(b).init.length  0); // Passes.
assert(typeid(i).init.length  0); // Fails.
}


It appears thus that the error is in typeid(interface), which does not
give the actual typeid.

The workaround is as follows:


interface I {
final
size_t size() {
return typeid(cast(Object)this).init.length;
}
}


Is this what you want? Is it good enough? I have no idea, as you're
notoriously bad at describing what you want, but pretty good at
attacking people.

If you're looking for a no-overhead solution, then this might not be
good enough. I'm surprised that a virtual function call is fine,
though.


Re: Get class size of object

2013-08-11 Thread Timon Gehr
You seem to show a certain amount of sophistication in projecting your 
own attributes on others. Why not retarget that creativity to achieve 
something useful?


Re: Get class size of object

2013-08-11 Thread Adam D. Ruppe

On Sunday, 11 August 2013 at 18:33:19 UTC, JS wrote:

The code below does this, but at a cost of verbosity.


I don't see what the difference is in functionality - it looks to 
me that you just reimplemented what the compiler does 
automatically with typeid.


The way typeid(obj) works is similar to a virtual function call 
internally, indeed, Class.vtbl[0] is a pointer to its TypeInfo 
instance.


But anyway, then the typeinfo contains functions for the class 
name and size, pretty similar to what you just did.



What does your implementation give you that typeid() doesn't?


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 19:08:58 UTC, Simen Kjaeraas wrote:

On 2013-08-11, 20:33, JS wrote:

I think you're missing the point to some degree(I realize 
there is a diff between an object and a type, but I should be 
able to easily get the class size of an object at run time 
regardless if the object is typed as a base class). The code 
below does this, but at a cost of verbosity.


Here is code that exactly demonstrates what I am essentially 
trying to do. The only drawback now is having to mixin the 
template for each class which I would want to automate and it 
would be nice to wrap the RT and CT methods in subclasses 
without overhead.


It would appear that some information is lost when calling 
typeid from an

interface. I would have expected this to work:

interface I {
final
size_t size() {
return typeid(this).init.length;
}
}

class A {}

class B : A, I {
int a;
}

void test1() {
I i = new B();
assert(i.size  0); // Fails.
}


A bit more experimentation shows:

void test2() {
B b = new B();
I i = b;
A a = b;

assert(typeid(a) == typeid(b)); // Passes.
assert(typeid(i) == typeid(b)); // Fails.

assert(typeid(b).init.length  0); // Passes.
assert(typeid(i).init.length  0); // Fails.
}


It appears thus that the error is in typeid(interface), which 
does not

give the actual typeid.

The workaround is as follows:


interface I {
final
size_t size() {
return typeid(cast(Object)this).init.length;
}
}


Is this what you want? Is it good enough? I have no idea, as 
you're

notoriously bad at describing what you want, but pretty good at
attacking people.

If you're looking for a no-overhead solution, then this might 
not be

good enough. I'm surprised that a virtual function call is fine,
though.


I attack people because they are arrogant bastards. They do 
nothing to help but only contribute to the chaos. They deserve to 
be attacked because arrogant people are evil.


In any case I think your solution might be correct. Originally I 
was using interfaces and do recall it working with classes but 
not interfaces but didn't know how to get it to work.




Re: Get class size of object

2013-08-11 Thread JS
BTW, I hope that if you want to be added to the ignore list on my 
side, you use the script and do the same. I know some will want 
everyone to see their irrelevant posts but I won't see it and so 
you will never get a response from me and it just clutters up the 
NG and distracts from D.





Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 19:06:07 UTC, Adam D. Ruppe wrote:

On Sunday, 11 August 2013 at 18:33:19 UTC, JS wrote:

The code below does this, but at a cost of verbosity.


I don't see what the difference is in functionality - it looks 
to me that you just reimplemented what the compiler does 
automatically with typeid.


The way typeid(obj) works is similar to a virtual function call 
internally, indeed, Class.vtbl[0] is a pointer to its TypeInfo 
instance.


But anyway, then the typesameinfo contains functions for the 
class name and size, pretty similar to what you just did.



What does your implementation give you that typeid() doesn't?


Well, I am wrapping the stuff. But when I've tried to do 
essentially l the same thing before using the compiler directly, 
it simply did not work.


If you read Simen Kjaeraas's post you will see it doesn't work 
directly with interfaces, which is what I was doing.


Hence, either a bug or anti-feature or whatever, the issue was 
with D and not some weird thing I was trying to do... All this 
nonsense is trying to work around D doing stupid stuff.


All I know is that in my code, when trying to get the actual 
object size, even if derived(which may change the size), I would 
always get 4. I was using an interface as my base type.


Regardless, there are now two solutions and I have to go back to 
my production code and figure out which one works best for my 
situation.




Re: Get class size of object

2013-08-11 Thread Adam D. Ruppe

On Sunday, 11 August 2013 at 19:33:43 UTC, JS wrote:
If you read Simen Kjaeraas's post you will see it doesn't work 
directly with interfaces, which is what I was doing.


Oh, I see it now. I think typeid(interface) gives a different set 
of info.


This seems to work though:

writeln(typeid(cast(Object)(f)).init.length);

Just casting the interface to Object before fetching the info.


Re: Get class size of object

2013-08-11 Thread Adam D. Ruppe

On Sunday, 11 August 2013 at 19:58:26 UTC, Adam D. Ruppe wrote:
Oh, I see it now. I think typeid(interface) gives a different 
set of info.


Thinking about it a bit more, this makes sense because an 
interface is not necessarily an Object - it might also be a C++ 
class, or a COM object, or maybe even a slice into another vtbl 
if an Object implements multiple interfaces (not completely sure 
about the latter, I'd have to read some source, but it seems 
logical to me).


Re: Get class size of object

2013-08-11 Thread JS

On Sunday, 11 August 2013 at 20:03:27 UTC, Adam D. Ruppe wrote:

On Sunday, 11 August 2013 at 19:58:26 UTC, Adam D. Ruppe wrote:
Oh, I see it now. I think typeid(interface) gives a different 
set of info.


Thinking about it a bit more, this makes sense because an 
interface is not necessarily an Object - it might also be a C++ 
class, or a COM object, or maybe even a slice into another vtbl 
if an Object implements multiple interfaces (not completely 
sure about the latter, I'd have to read some source, but it 
seems logical to me).


Possibly. Since I program to interfaces, every reference I use is 
referenced to an interface. Needing the correct object size for 
implementing an allocator was not working as expected(at least 
as one would expect with classes). I could not see anything in 
the docs that would suggest otherwise(else the light bulb might 
have went off).


i.e., one would expect, IMO, that tsize would work on interfaces 
because interfaces do not have a size so it should return the 
class size of the object. (so things work uniformly and we can 
program to interfaces without having to create workarounds).






Get class size of object

2013-08-10 Thread JS
Given an object, is there a built in way to get the size of the 
class the object represents? The object may be instantiated with 
a derived instance of the object type, so using classInstanceSize 
doesn't work on the type of the object.


I can obviously go through the trouble of adding a class member 
and override it in all the derived classes. I saw tsize in object 
but 2.063 says it doesn't exist.