Search code examples
typesmacrosglibgnome

Correct way to cast a gpointer to a GType


I am trying to create a macro which will cast a gpointer to GType and vice versa. I have created the following macros to do this using the relevant API documentation as a guide:

#ifdef GTYPE_TO_POINTER
  #define GTYPE_TO_POINTER (x) (GSIZE_TO_POINTER (x))
#endif
#ifdef GPOINTER_TO_GTYPE
  #define GPOINTER_TO_GTYPE(p) (GPOINTER_TO_SIZE (p))
#endif

I have the casting done as follows:

type = (GType)g_hash_table_lookup(typeTable, GUINT_TO_POINTER(tflag));

I want to do the casting as follows:

type = GPOINTER_TO_GTYPE(g_hash_table_lookup(typeTable, GUINT_TO_POINTER(tflag)));

 // stuff

 g_hash_table_insert(typeTable,
                     GUINT_TO_POINTER(tflag),
                     GTYPE_TO_POINTER(type)); 

However, this seems to be problematic as my compiler is indicating an implicit declaration has been made with the g_hash_table_lookup cast and with the third argument of the following g_hash_table_insert

warning: implicit declaration of function 'GPOINTER_TO_GTYPE'          
warning: implicit declaration of function 'GTYPE_TO_POINTER'
warning: passing argument 3 of 'g_hash_table_insert' makes pointer from integer without a cast [-Wint-conversion]

Also when I try to run the wrapper I get a java: symbol lookup error of an undefined symbol: GPOINTER_TO_GTYPE.

Can anyone advise on what I might be able to do to fix this?


Solution

  • OK I seem to have figured it out myself now....

    Firstly, I was using the wrong test in checking for GTYPE_TO_POINTER and GPOINTER_TO_GTYPE since they were the variables I wanted to define the compiler could not find them and the definition was never being made.

    Secondly, I was using undefined variables as an argument so the correct definition for these macros seems to be:

    #ifdef GPOINTER_TO_SIZE
      #define GPOINTER_TO_GTYPE(gpointer) (GPOINTER_TO_SIZE (gpointer))
    #endif
    #ifdef GSIZE_TO_POINTER
      #define GTYPE_TO_POINTER(gtype) (GSIZE_TO_POINTER(gtype))
    #endif
    

    Which means my casting functions now compile without problems.