Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@cbeck88
Copy link
Owner

@cbeck88 cbeck88 commented Jul 17, 2025

this avoids the complexity of introducing proxy objects (and handling copies of these) when one wants to visit the same object in multiple ways

follows plan in issue #34

building but still needs tests

@cbeck88
Copy link
Owner Author

cbeck88 commented Jul 20, 2025

this needs docu, then i'm probably happy to ship it

@Sven-vh
Copy link

Sven-vh commented Jul 20, 2025

This is perfect, thank you for taking your time and implementing this!

I tried this branch with my Transform test and it works perfectly! I like that the VISITABLE_STRUCT macro uses void as default context. Makes it super easy to do stuff like this, where you take the context as template parameter.

struct SerializeContext {};
struct PrintContext {};
struct ScriptingContext {};

struct Transform {
private:
	BEFRIEND_VISITABLE();
	glm::vec3 m_position;
	glm::vec3 m_scale;
	glm::quat m_rotation;
};

VISITABLE_STRUCT(Transform, m_position, m_scale, m_rotation);
VISITABLE_STRUCT_IN_CONTEXT(SerializeContext, Transform, m_position);
VISITABLE_STRUCT_IN_CONTEXT(PrintContext, Transform, m_scale);
VISITABLE_STRUCT_IN_CONTEXT(ScriptingContext, Transform, m_rotation);

struct debug_printer {
	template <typename T>
	void operator()(const char* name, const T&) {
		std::cout << name << std::endl;
	}
};

//template <typename T, typename Context = void>
// Swapped Context and T so I don't have to specify T when calling print_properties
template <typename Context = void, typename T>
void print_properties(const T& obj) {
	visit_struct::context<Context>::for_each(obj, debug_printer{});
}

int main() {
	Transform transform;
	print_properties(transform); // uses void, thus default macro, thus: m_position, m_scale, m_rotation
	print_properties<SerializeContext>(transform); // m_position
	print_properties<PrintContext>(transform); // m_scale
	print_properties<ScriptingContext>(transform); // m_rotation
}

I'm planning on using it a lot when making custom engines. Probably going to use it to differentiate between:

  • Serializing
  • Debug printing
  • Properties in scripting
  • Properties in inspector

Limitations are endless. Thanks again for this feature and your time!

Copy link

@Sven-vh Sven-vh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I said, ran my Transfrom test and everything works.
Tested with msvc on C++ 14.

Errors are also super nice. Misspelling or having a duplicate context give clear errors that are easy to debug/know what is wrong. Awesome job!

@cbeck88
Copy link
Owner Author

cbeck88 commented Jul 22, 2025

Thank you for testing!

I still need to write some docs and maybe extend the tests, then will merge

@cbeck88 cbeck88 marked this pull request as ready for review July 22, 2025 18:15
@cbeck88 cbeck88 merged commit 65c55be into master Jul 22, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants