Tagged: programming Toggle Comment Threads | Keyboard Shortcuts

  • frankiezafe 19:18 on 2017-04-04 Permalink | Reply
    Tags: , , programming   

    Today, certainly one of the less sexy thing i’ve ever posted…

    How to create a unique key out of two pointers in C++?

    After some investigation and a poor attempt to use boost uint128_t, i found my way using a char array.

    Here is the code:

    SomeClass* a = new SomeClass();
    SomeClass* b = new SomeClass();
    
    unique = new unsigned char[ 16 ]; // room for 64bits pointers
    uintptr_t ta = reinterpret_cast<uintptr_t>(a);
    uintptr_t tb = reinterpret_cast<uintptr_t>(b);
    
    for ( int i = 0; i < 8; ++i ) {
        unique[ i + 8 ] = ta;
        unique[ i ] = tb;
        ta = ta >> 8;
        tb = tb >> 8;
    }
    
    cout << "chars: ";
    for ( int i = 0; i < 16; ++i ) {
        cout << uint16_t( unique[i] ) << " | ";
    }
    cout << endl;
    
    uintptr_t newa = 
            unique[15] << 56 | 
            unique[14] << 48 | 
            unique[13] << 40 |
            unique[12] << 32 |
            unique[11] << 24 |
            unique[10] << 16 |
            unique[9] << 8 |
            unique[8];
    
    uintptr_t newb = 
            unique[7] << 56 | 
            unique[6] << 48 | 
            unique[5] << 40 |
            unique[4] << 32 |
            unique[3] << 24 |
            unique[2] << 16 |
            unique[1] << 8 |
            unique[0];
    
    cout << reinterpret_cast<uintptr_t>(a) << " <> " << newa << endl;
    cout << reinterpret_cast<uintptr_t>(b) << " <> " << newb << endl;
    cout << reinterpret_cast<RoadDot*>(newa) << " <> " << a << endl;
    cout << reinterpret_cast<RoadDot*>(newb) << " <> " << b << endl;
    

    And… done! The pointers are casted into the very useful uintptr_t as a unsigned int, stored in the array in 8 bits chunks. On 64 bits systems, pointers use 64bits… Therefore, the char array needs to have 16 slots ( 128 / 8 = 16 ).

    At the end of the code, the pointers are recreated from the characters and compared to the original values. In my terminal, here is what i saw:

    chars: 96 | 168 | 98 | 1 | 0 | 0 | 0 | 0 | 224 | 130 | 98 | 1 | 0 | 0 | 0 | 0 | 
    23233248 <> 23233248
    23242848 <> 23242848
    0x16282e0 <> 0x16282e0
    0x162a860 <> 0x162a860
    

    I’ll optimise this a bit, mainly by creating the char array based on the size of the uintptr_t * 2.

     
  • frankiezafe 19:35 on 2016-11-20 Permalink | Reply
    Tags: , programming,   

    selection_709

    Here is the output and the code of the empty example. It is using polymorph package.

    Once installed (the tricky part, soon fixed with a magic installation script), you just have to extends PolymorphApplication class and start working.

    As you may notice, the class CustomApp does nothing on its own. It just calls the mother class’ methods. The purpose of this empty project is to enlight crucial methods to overwrite in order to start coding something.

    I’m now working on a basic example, showing how to add primitives, load meshes, add lights and animate them.

     
    • frankiezafe 21:34 on 2016-11-20 Permalink | Reply

      @xuv & @balt : i think it’s important to be obvious on what is polymorph and what is ogre by keeping the namespaces as they are. For instance, creating a PNode requires to call polymorph::PNode. When you want to create a vec3, there you have to call Ogre::Vector3.
      In the example above, the using namespace is only used in cpp => for all class params (.h), they have to be set this way. It will be explained in the basic example.
      What d’u think?

      • xuv 14:19 on 2016-11-21 Permalink | Reply

        @frankiezafe : seems coherent.

        • frankiezafe 15:53 on 2016-11-21 Permalink | Reply

          i’ve started working on a “PObject”, wich is a common ground for PNodes, PLights and PSounds. It’s a abstract object defining anyhting located in the 3d world, with possibility to attach them together or add physics on them.
          Result: ultra tuff conceptual work, using an abstract class + a template class for PObject – will work in a few days.

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel