About Store Forum Documentation Contact



Post Reply 
How structs like Cpu, State, ClassTime work?
Author Message
Eric Offline
Member

Post: #1
How structs like Cpu, State, ClassTime work?
Esenthel, next question. I study the headers of EE and have a next question. Where and how do you manage structs like Cpu, State and ClassTime ? In Cpu You have PRIVATE member
Code:
UInt _cores;
which is returned by
Code:
UInt cores()C;
function - but, where do you set it on ? Where is something like
Code:
_cores = system::__availableCores();
? And how it's possible since the variable is PRIVATE ? Same in States, where're they managed ? In
Code:
int main() {...}
?
I'm very, very curious abut it. wink Pozdrawiam !

Regards,
Eric 'Eri' Przychocki
ourgames.eu
05-14-2013 07:49 PM
Find all posts by this user Quote this message in a reply
Ozmodian Offline
Member

Post: #2
RE: How structs like Cpu, State, ClassTime work?
Hi Eric,

The reason you are unable to see anything like _cores = system::availableCores() is because you are just looking at header files, the .cpp files are compiled so you cannot see the source. The header files dont tell you how the sausage is made, just what type of sausage it is smile. The source for the Engine is not available.

Sorry if this does not answer your question. If I can help further, please let me know.
05-14-2013 09:31 PM
Find all posts by this user Quote this message in a reply
Eric Offline
Member

Post: #3
RE: How structs like Cpu, State, ClassTime work?
Yeah I know I'm looking at headers but I'm curious how it works in the source smile Every time i look at function in EE I'm wondering how would I made it. wink It's just my "programming curiosity". pfft <- I don't even know if I wrote it properly.

P.S. Thanks Ozmodian for the reply ! : ))

Regards,
Eric 'Eri' Przychocki
ourgames.eu
05-15-2013 03:21 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: How structs like Cpu, State, ClassTime work?
(05-14-2013 07:49 PM)Eric Wrote:  And how it's possible since the variable is PRIVATE ? Same in States, where're they managed ?
Besides raw C++ codes Esenthel also has a bit of magic lol
05-16-2013 01:47 PM
Find all posts by this user Quote this message in a reply
Eric Offline
Member

Post: #5
RE: How structs like Cpu, State, ClassTime work?
what kind of magic ? maybe simple prompt ? pfft now i'm gonna google "how to get access to private class members c++ magic"

Regards,
Eric 'Eri' Przychocki
ourgames.eu
05-16-2013 07:44 PM
Find all posts by this user Quote this message in a reply
Sherincall Offline
Member

Post: #6
RE: How structs like Cpu, State, ClassTime work?
(05-16-2013 07:44 PM)Eric Wrote:  what kind of magic ? maybe simple prompt ? pfft now i'm gonna google "how to get access to private class members c++ magic"


If you know the offsets of the members, just increment the pointer to the instance. Like this:
Code:
class MyClass
{
public:
    int n;
private:
    int n_;
};

MyClass a;
a.n = 3;
*(((void *)&a)+4) = 4; // a.n_ = 4;

If you don't know the offsets, but know their value at some point, memdump the class and look for it.
Keep in mind when calculating the offset, if a class has virtual functions, or is derived from a class that does, it will have a VFTP. They might also have a class ID stored somewhere.

Also, while this theoretically kills all platform independence, I think all Esenthel-supported compilers generate the same code, so it should work. Just be sure to use sizeof instead of magic numbers.

Finally, if you want the offset of a field you can access (this might also work for privates, but don't count on it), you can use this macro:
Code:
#define OFFSET_OF(str, fld) ((int)(&((str*)0)->fld))
Even though you are doing (0)->fld, this will not generate an access violation, because you are only getting the address, so some compilers might allow it to work for private members as well.. Too lazy to test right now.
(This post was last modified: 05-16-2013 10:27 PM by Sherincall.)
05-16-2013 10:25 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #7
RE: How structs like Cpu, State, ClassTime work?
I don't recommend doing above is it's easy to make mistakes.

I do it other way wink

this is the actual cpu header:
Code:
/******************************************************************************

   Use 'Cpu' to check your cpu capabilities.

/******************************************************************************/
enum CPU_FLAG // CPU Flags
{
   CPU_MMX  =0x01, // if MMX   supported
   CPU_3DNOW=0x02, // if 3DNow supported
   CPU_SSE  =0x04, // if SSE   supported
   CPU_SSE2 =0x08, // if SSE2  supported
   CPU_SSE3 =0x10, // if SSE3  supported
};
/******************************************************************************/
struct CPU // Central Processing Unit
{
   // get
   UInt    cores()C {return _cores;} // number of available cores
   UInt    flag ()C {return _flag ;} // get CPU_FLAG
   C Str8& name ()C {return _name ;} // get cpu name

#ifdef EE_PRIVATE
          void create();
   static void set   ();
#endif

#ifndef EE_PRIVATE
private:
#endif
   UInt _cores, _flag;
   Str8 _name;
}extern
   Cpu;
/******************************************************************************/
inline Int Elms(C CPU &cpu) {return cpu.cores();}
/******************************************************************************/

but before publishing it to EE release, I just copy cleaned version without EE_PRIVATE parts (I've made automated tool for that)

I went the extra mile just so the users will have a more clean header browsing experience smile
05-17-2013 12:45 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #8
RE: How structs like Cpu, State, ClassTime work?
Eric, if you want a basic version, there's a code snippet made by... Intel I think? that a lot of people use to detect CPU information(works with any make processor). It's coded in C / Assembly.
It has 1 major bug, though(that I've seen): It crashes if the user has more than 8 cores. Very, very frustrating. Many big names use it, such as Skype. Funny thing, that; I contacted Skype support because it kept crashing. I ended up troubleshooting myself(as their level 2 guys couldn't even figure it out) and found that Skype runs that Intel code, and made a script to work around it, and gave them the solution. Go me!
05-17-2013 03:26 PM
Find all posts by this user Quote this message in a reply
Eric Offline
Member

Post: #9
RE: How structs like Cpu, State, ClassTime work?
Sherincall - when I googled the "how to get access to private class members c++" mantra (yes, I actually did it, just without the 'magic' word pfft) I've found your solution smile I was considering using this technique even if it's not quite safe, but it completely 'destroys' the purpose of public/private 'areas'. At first I used 'friend' operator, but..
..Esenthel - your way is - in fact - very, very clever smile It keeps away some very important variables from end EE users. Useful trick. Now, the experience of browsing EE headers will be - as you metioned - more clear smile Thanks and RESPECT wink
Rubeus - funny story how big companies sometimes don't give a f*** about their apps. Thanks for your help - will use it smile

Regards,
Eric 'Eri' Przychocki
ourgames.eu
05-17-2013 05:10 PM
Find all posts by this user Quote this message in a reply
Post Reply