Is there a simple way to convert C++ enum to string? -
suppose have named enums:
enum myenum { foo, bar = 0x50 };
what googled script (any language) scans headers in project , generates header 1 function per enum.
char* enum_to_string(myenum t);
and implementation this:
char* enum_to_string(myenum t){ switch(t){ case foo: return "foo"; case bar: return "bar"; default: return "invalid enum"; } }
the gotcha typedefed enums, , unnamed c style enums. know this?
edit: solution should not modify source, except generated functions. enums in api, using solutions proposed until not option.
you may want check out gccxml.
running gccxml on sample code produces:
<gcc_xml> <namespace id="_1" name="::" members="_3 " mangled="_z2::"/> <namespace id="_2" name="std" context="_1" members="" mangled="_z3std"/> <enumeration id="_3" name="myenum" context="_1" location="f0:1" file="f0" line="1"> <enumvalue name="foo" init="0"/> <enumvalue name="bar" init="80"/> </enumeration> <file id="f0" name="my_enum.h"/> </gcc_xml>
you use language prefer pull out enumeration , enumvalue tags , generate desired code.
Comments
Post a Comment