kandi X-RAY | rttr Summary
kandi X-RAY | rttr Summary
RTTR stands for Run Time Type Reflection. It describes the ability of a computer program to introspect and modify an object at runtime. It is also the name of the library itself, which is written in C++ and released as open source library. You can find more information on: www.rttr.org.
Top functions reviewed by kandi - BETA
rttr Key Features
rttr Examples and Code Snippets
Trending Discussions on rttr
I am trying to use LexicalRichness (https://pypi.org/project/lexicalrichness/). I copied and pasted the code:...
ANSWERAnswered 2021-Jun-21 at 19:21
Check this line for capitalisation:
lex = LexicalRichness(text)
I have some JSON files in which I define objects of various types. The types are given as a field within the objects. I want to load the file and for each JSON object, create a new class of that type and pass the rest of the JSON data to its constructor.
The issue is that I'd rather not have a huge case statement matching the type and creating an object of that type. Here are some of the possibilities I've considered:
Reflection. I don't know too much about it, but my understanding is that it might allow me to create a class in this manner. While I'm aware C++ doesn't provide this capability natively, I've seen a few libraries such as this one that might provide such functionality.
Create an enum of class types. Create a template function that takes a type parameter from this enum and creates an object of that type. Use something like smart_enum to convert the string field.
Option 2 seems like a good one but I haven't been able to get this working. I've done extensive googling, but no luck. Does anyone know how I might go about doing this, or if there is a better option which I have not considered? Apologies if this has been answered elsewhere, perhaps under a term which I do not know; I have spent quite a lot of time trying to solve this problem and had no luck.
Please let me know if I can provide any additional information, and thank you.
Edit: here's an example of what I've tried to get option 2 working....
ANSWERAnswered 2020-Oct-27 at 04:14
As noted in the comments, #1 is out, C++ lacks reflection (until P0194 gets adopted).
#2 still requires a big ol' switch block because you're still have to switch on a run-time type ID.
So, I'll propose #3: use a template to generate all those case statements you don't want to have to write (well, a map anyway).
This is the final code, which uses JSON for Modern C++ library for JSON parsing since that's the one that's available from godbolt :).
No vulnerabilities reported
Reuse Trending Solutions
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page