Templates in C using void * in C++ -
Templates in C using void * in C++ -
i have generic class written in c++ , exercise, i've been attempting port c. i've tried typedef specific types realized wrong way go it. i'm attempting utilize void pointers realized have noway instantiate generic class missing?
ctrie.h
#ifndef _com_wordgame_utility_trie_h_kyle #define _com_wordgame_utility_trie_h_kyle #ifdef __cplusplus extern "c"{ //this used identify next code c specific code enforce c style name mangling #endif typedef struct trie trie; //create new trie object trie* newtrie(void * defval); //return number of key-value pairs int size(trie * t); //return default value set user void * getdefaultvalue(trie * t); //check if returned value not equal default value bool contains(trie * t,const char * key); //return value void * get(trie * t,char * key); //insert string symbol table void put(trie * t,char * s,void * val); //find , homecoming longest prefix of s in tst char * longestprefix(trie * t,char * s); //compress table making immutable returning number of nodes removed int compress(trie * t); #ifdef __cplusplus #include "ctrie.cpp"//quick hack add together ctrie without linking } #endif #endif
ctrie.cpp
#include "trie.hpp" //c++ code #include "trie.h" //c code extern "c"{ using namespace com::wordgame::utility::trie; //create new trie object trie* newtrie(void * defval){ homecoming new trie<typeid(defval)>(defval); }; //return number of key-value pairs int size(trie * t){} //return default value set user void * getdefaultvalue(trie * t){} //check if returned value not equal default value bool contains(trie * t,const char * key){} //return value void * get(trie * t,char * key){} //insert string symbol table void put(trie * t,char * s,void * val){} //find , homecoming longest prefix of s in tst char * longestprefix(trie * t,char * s){} //compress table making immutable returning number of nodes removed int compress(trie * t){} }
one of key advantages of templated container types in c++ ability provide nice syntactic way embed relevant object construction itself, illustration typical std::vector<t>
(which typically implemented simple array) can have stride of t
. accomplish in c, need either implement entire function bunch of macros, , "instantiate" function set each type, example, simple "vector" implementation in c might (which either ugly, beautiful, or satanic depending on ask)
#define vectorfuncs(t) \ typedef struct { \ t* arr; \ size_t narr; \ size_t capacity; \ } vectorof_##t; \ \ void vector_##t##_reserve(vectorof_##t *v, size_t n) { \ // \ } \ const t* vector_##t##_at(vectorof_##t *v, size_t ix) { \ homecoming v->arr[ix]; \ } \ vectorfuncs(int) vectorfuncs(struct sockaddr)
// etc.
as side note, implemented much more "cleanly" (depending on ask) passing sizeof(t)
, using single set of functions implementation; functions need treat arr
element char*
, determine offsets various operations).
however, if template class attach pointer t
type safety, may exclusively in c without much hassle, except of course of study don't "instantiate" template each time (and not necessary. c makes casting void much less annoying).
c++ c templates generics void-pointers
Comments
Post a Comment