Giter VIP home page Giter VIP logo

Comments (7)

churchianity avatar churchianity commented on May 30, 2024

I'm using clang v13.0.0 on macosx catalina, c++14

from visit_struct.

churchianity avatar churchianity commented on May 30, 2024

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.

NikolausDemmel avatar NikolausDemmel commented on May 30, 2024

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.

churchianity avatar churchianity commented on May 30, 2024

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.

churchianity avatar churchianity commented on May 30, 2024

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.

NikolausDemmel avatar NikolausDemmel commented on May 30, 2024

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.

churchianity avatar churchianity commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.