Comments (7)
I'm using clang v13.0.0 on macosx catalina, c++14
from visit_struct.
I must be doing something fundamentally wrong, because I just tested using the VISITABLE_STRUCT
method instead of the instrusive method and everything is still reading as 'not visitable'.
from visit_struct.
The issue might be that decltype(value)
gives you a const reference type. Maybe try visit_struct::traits::is_visitable<std::decay_t<decltype(value)>>::value
. "decay" should remove the const reference and give you the underlying type.
from visit_struct.
Thanks for the reply. No dice it seems. Minimally reproducible example:
#include <stdio.h>
#include <stdlib.h>
#include <visit_struct/visit_struct.hpp>
#include <visit_struct/visit_struct_instrusive.hpp>
typedef struct Water {
BEGIN_VISITABLES(Water);
VISITABLE(float, x);
VISITABLE(float, y);
VISITABLE(float, z);
END_VISITABLES;
} Water;
typedef struct Level {
BEGIN_VISITABLES(Level);
VISITABLE(Water*, waterTiles);
VISITABLE(int, numWaterTiles);
END_VISITABLES;
} Level;
template<typename T>
static void visitSerialize(const T& type) {
visit_struct::for_each(type, [](const char* name, const auto & value) {
if (visit_struct::traits::is_visitable<std::decay_t<decltype(value)>>::value) {
printf("is visitable! %s", name);
}
});
}
int main(void) {
Level level;
level.waterTiles = (Water*) malloc(sizeof(Water) * 12);
level.numWaterTiles = 12;
visitSerialize(level);
return 0;
}
clang main.cpp -std=c++14 -I . -o out
(assuming you have visit struct in that dir)
I tried using std::decay_t
as well as just decltype
as I did before.
I recognize this isn't exactly an issue with the library itself, but I was assuming there would be a way to visit recursively on structs. Was I mistaken, or is there a better way to do it?
from visit_struct.
Inspecting some code you have written @NikolausDemmel in one of your public repositories, it certainly seems like it is possible. Looking at this example:
// convert visitable struct to json; supports types that can be assigned to
// nlohmann::json, such as strings, doubles, as well as recursive std::vector
// and visistable structs;
template <class Value>
auto visitable_to_json(const Value& value) {
nlohmann::json result;
if constexpr (visit_struct::traits::is_visitable<Value>::value) {
// visitable struct --> visit each member and save as json object
visit_struct::for_each(value, [&](const char* name, const auto& member) {
result[name] = visitable_to_json(member);
});
} else if constexpr (is_vector_v<Value>) {
// vector --> recursively save elements to json array
for (const auto& elem : value) {
result.push_back(visitable_to_json(elem));
}
} else if constexpr (wise_enum::is_wise_enum_v<Value>) {
// enum --> save as string
result = wise_enum::to_string(value);
} else {
// other basic value (string, number, ...)
result = value;
}
return result;
}
Trying to work through comparing your assumedly working code and mine.
from visit_struct.
Just noticed that your member is a pointer to an array of Water
. The pointer is of course not visitable. You need to dereference and visit the array elements.
from visit_struct.
Thanks. I managed to get it working by essentially copying up to the first if-block in your JSON serialization example, AND making sure that I always pass my structs to visitSerialize
by value.
If I call visitSerialize(Level* level)
it doesn't work, but if I call visitSerialize(Level level)
, it does.
Makes sense to me in retrospect. I'll close the issue, appreciate the help. Ideally I'd like to be able to pass the struct in by pointer too, but I can probably figure that out.
from visit_struct.
Related Issues (20)
- Compatibility with cereal HOT 2
- runtime versions of indexed access functions HOT 3
- Allow non-intrusive annotation of template types
- "Intrusive" Syntax and member initialization HOT 15
- Feature request: optional visitable metadata HOT 2
- if constexpr type deduction in visitor? HOT 2
- Separate parser program for 'intrusive'
- Get member variable pointer at compile-time HOT 1
- Non const visitor function arguments HOT 1
- Figure out what's wrong with `visit_struct` core on appveyor build HOT 1
- Clang: Compilation error with visitable struct include template class
- QA HOT 1
- conan package HOT 1
- compile error with boost fusion HOT 1
- New release? HOT 6
- Exposing private members HOT 1
- Add the fully visitable test example as a concept?
- Visiting template classes
- Not compiling using Intel 16.0 (with Visual Studio 2013) HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from visit_struct.