C++ equivalent of Java Enum.valueOf() [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is it possible to define enumalpha?
Is there any equivalent of Java Enum.valueOf(string) on C++?

There's no table of names generated by the compiler (unless you count debug information), but if you create one (or use e.g. doxygen which parses the source code and can output such lists in XML format) then you can use a dictionary of some type, such as std::map<string, int> to turn an identifier into its numeric value.

No, there isn't even the much simpler task of going the other way (enum to string), you'd need to write it yourself

Related

Regex to extract function from c++ function calls [duplicate]

This question already has an answer here:
Regex for extracting functions from C++ code
(1 answer)
Closed 7 years ago.
I have to extract a function name from various c++ function calls. Following are some of the function calls examples and extracted function names highlighted.
std::basic_fstream<char,std::char_traits<char> >::~basic_fstream<char,std::char_traits<char> >
~basic_fstream
CSocket::Send send
CMap<unsigned int,unsigned int &,tagLAUNCHOBJECT,tagLAUNCHOBJECT &>::RemoveAll
Cerner::Foundations::String::Rep::~Rep~Rep
CCMessage::~CCMessage ~CCMessage
std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,u_Tree
Lib::DispatcherCache::~DispatcherCache~DispatcherCache
CPrefDataObjectLoader<CPrefManagerKey,CPrefManagerValue,CGetPrefManager,PrefManagerKeyFunctor>::Get Get
The following Regex works for most of the functions
/((?:[^:]*))$';/ This regex get the string from the last :
/+?(?=<)';/ This one removes string that starts with <
But for std::basic_fstream<char,std::char_traits<char> >::~basic_fstream<char,std::char_traits<char> > the output I get is char_traits because this string is after last ':' but the result should be ~basic_fstream. Is there a way I can combine both regex and ignore everything that is within <>?
The grammar of C++ is not only not regular, it's actually highly context-sensitive (especially near templates). Even a proper CFG parser won't help you, let alone a plain old regex… Rather than trying to approximate the impossible using ugly and fragile hacks, why not use an actual tool for the job? If you want to parse C++, then use a C++ parser, such as libclang.

C++ strings vs vector<char> [duplicate]

This question already has answers here:
What are differences between std::string and std::vector<char>?
(5 answers)
C++: char test[100] vs array<char, 100> vs string
(4 answers)
Closed 7 years ago.
It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.
However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?
I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.
To be clear: it is all about expressing the intent.
To put it briefly: if you're storing text, then string, otherwise vector<char>.

C++ Using strings to access variables [duplicate]

This question already has answers here:
Access variable value using string representing variable's name in C++ [duplicate]
(8 answers)
Closed 7 years ago.
Suppose I have a structure bbox containing 6 sets, where each set contains 4 vectors. I can add a vector element by using bbox.set1.vect1.push_back(foo). However, I'm reading data from a file and I'm looking for an elegant way to store the data in the vectors. Using a double for() loop with indices i (1 to 6) and k (1 to 4) I've tried the following (using string concatenation):
string test1 = "bbox.set";
string test2 = ".vect";
string fin = test1 + to_string(i) + test2 + to_string(k);
fin.push_back(val);
Though the code compiles fine, nothing seems to happen. Explicitly writing bbox.set1.vect1.push_back(foo) does work. Can this be done in such a way? In another topic I've read that C does not support changing/creating variable names during runtime, but here I simply try to access an existing variable.
No, C++ does not support this, since variable names are resolved at compile time, which means that by the time the program runs, the variable names themselves are meaningless. (In other words, the name bbox has in practice been replaced by a set of numbers representing the object called by that variable name.)
If you really need to something like this, you should consider using a container such as std::map, which you can use to map strings to objects. You can't access them like variables, though, but you can dynamically build strings to decide which object to get.
What you are looking for is some method of reflection, which C++ does not natively support. There is no way to do what you want directly.
Instead you will need to provide your own support.

Input of integer in the range of 10^1000 [duplicate]

This question already has answers here:
Handling large numbers in C++?
(10 answers)
Closed 8 years ago.
Is it possible to take an input of range greater than what C/C++ provides? Is it possible to accept an input range greater than that of unsigned long long and even larger up to the range of 10^1000?
If it is possible in C/C++, please answer how it can be done, thanks.
There's no bigint in C or C++, however library like this one can provide it: https://code.google.com/p/infint/
Input into a string. Then convert the string into the desired type.
If you use a library that provides types for large integers, such a library might also offer input functions.

Format a String in C++ with the same convenience as String.format() in Java 5 / 6? [duplicate]

This question already has answers here:
How to construct a std::string with embedded values, i.e. "string interpolation"?
(8 answers)
Closed 2 years ago.
Is there a common function available to be able to do sprintf type String formatting without having to supply a fixed size buffer, that returns a string class instance?
I know about stringstream it doesn't do what I want, I don't want to hard code the position of the tokens in the output statement like it requires.
I want to be able to define a pattern like sprintf lets you, but without the C baggage and in a more idiomatic Object Oriented C++ manner.
Maybe some function that does what sprintf does using a stringstream and produces a string object? Something along the line of the convenience of what String.format() does in Java or the equivalent String formatting syntax in Python.
The Boost Format Library:
The <boost/format.hpp> format class provides printf-like formatting, in a type-safe manner which allows output of user-defined types.
If you don't use Boost.Format or Boost.Locale you can use my simple stringstream wrapper or wrap it even further:
fakeformat
example:
REQUIRE( ff::format("{2}ff{1}").with('a').also_with(7).now()=="7ffa" );
Ideone