Code Monkey home page Code Monkey logo

Comments (2)

veselink1 avatar veselink1 commented on June 14, 2024

Generally, I consider enum reflection to be out of scope, because there already are excellent libraries out there. I was going to suggest magic_enum just after reading the question title, but you say you know about it.

I haven't used magic_enum personally, but from looking at their readme, I do not see what more we could do in refl-cpp. It would be useful to know what limitations of magic_enum you are having trouble with.

from refl-cpp.

W4RH4WK avatar W4RH4WK commented on June 14, 2024

Okay. I understand.

It's not so much that there is a specific limitation of magic_enum that causes a problem for me. It's just that the approach seems too sub-optimal to me such that I don't want to build on top of it; even though it might actually work perfectly fine in practice.

Anyway, for future reference: I've tried a couple of different approaches to my issue and it appears that a constexpr array of pairs is sufficient for what I need. Reusing the example from above:

enum class ToneMapping {
#define IKAROS_ENUM_ToneMapping \
	IKAROS_ENUM_E(None) \
	IKAROS_ENUM_E(Uncharted2) \
	IKAROS_ENUM_E(ACES)
#define IKAROS_ENUM_E(_entry) _entry,
	IKAROS_ENUM_ToneMapping
#undef IKAROS_ENUM_E
};
constexpr std::array ToneMappingEntries{
#define IKAROS_ENUM_E(_entry) std::pair{ToneMapping::_entry, #_entry},
    IKAROS_ENUM_ToneMapping
#undef IKAROS_ENUM_E
};

This allows one to iterate over all entries, get the number of entries as a compile-time constant, and build more efficient lookup tables from it.

lookup table
const etl::unordered_map<ToneMapping, std::string_view, ToneMappingEntries.size()> ToneMappingToStringLookup{
    ToneMappingEntries.begin(), ToneMappingEntries.end()};

std::string_view to_string(ToneMapping e)
{
	if (auto it = ToneMappingToStringLookup.find(e); it != ToneMappingToStringLookup.end()) {
		return it->second;
	} else {
		return "invalid";
	}
}

const auto ToneMappingFromStringLookup = [] {
	etl::unordered_map<std::string_view, ToneMapping, ToneMappingEntries.size()> lookup;
	for (const auto& entry : ToneMappingEntries) {
		lookup[entry.second] = entry.first;
	}
	return lookup;
}();

void from_string_impl(std::string_view input, std::optional<ToneMapping>& result)
{
	if (auto it = ToneMappingFromStringLookup.find(input); it != ToneMappingFromStringLookup.end()) {
		result = it->second;
	} else {
		result.reset();
	}
}

Technically, an array would be sufficient for the toString lookup, but this requires enum values to always be in the range of 0 to count-1.

from refl-cpp.

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.