Giter VIP home page Giter VIP logo

my_enum's Introduction

CI

MY_ENUM

MY_ENUM is a small c++ macro library to add compile-time introspection to c++ enum classes.

Examples

For example MY_ENUM(FooBar, int, (foo, bar)); defines an enum class

enum class FooBar : int {
  foo,
  bar
};

with the following free functions:

// Returns corresponding string of given value
//
// Preconditions: value must be a valid enum value, i.e. FooBar::foo, or
//                FooBar::bar.
//
std::string toString(FooBar value);

// Returns corresponding string view of given value
//
// Preconditions: value must be a valid enum value, i.e. FooBar::foo, or
//                FooBar::bar.
//
string_view toStringView(FooBar value);

// Returns pretty string representation of given value
//
// Preconditions: value must be a valid enum value, i.e. FooBar::foo, or
//                FooBar::bar.
//
std::string toPretty(FooBar value);

// Sets enum given corresponding string, if string is valid (i.e. "foo" or
// "bar"). Returns false otherwise.
//
bool trySetFromString(FooBar& value, std::string str);

// Return count of enum type. First argument is needed for ADL only.
//
constexpr size_t getCount(FooBar) {
  return 2;
}

// Return string views of enum type. First argument is needed for ADL only.
std::array<string_view, 2> getStrings(FooBar) {
  return {"foo", "bar"};
}

// Return string of enum names. First argument is needed for ADL only.
std::array<string_view, 2> getStrings(FooBar) {
  return "foo, bar";
}

// Return values of enum type. First argument is needed for ADL only.
constexpr std::array<int, 2> getValues(FooBar) {
  return {0, 1};
}

// Returns the position of enum value in the enum class. This is the inverse
// of `getValues(FooBar)[i]`.
constexpr size_t getPosition(FooBar value) {
  switch(value) {
    case FooBar::foo: { return 0; }
    case FooBar::bar: { return 1; }
  }
}

// Return string representation of type name. First argument is needed for
// ADL only.
string_view getTypeName(FooBar) {
  return "FooBar";
}

Requirements

MY_ENUM is a c++ macro library.

  • CMake configuration and build steps are tested on MacOS and Linux.
  • It depends on the BOOST preprocessor library.
  • It depends on a string_view implementation: either std::string_view (c++17), or fmt::string_view from the {fmt} library.
  • MY_ENUM tests depend on googletest which are pulled in through git sub-modules.
  • The MY_ENUM CliExample depends on CLI11.

Building MY_ENUM

In the following, we detail how to build MY_ENUM for Ubuntu 18.04.

Install BOOST preprocessor headers:

sudo apt install libboost-system-dev

Install CLI11 if you want to build the CliExample application:

git clone https://github.com/CLIUtils/CLI11.git
cd CLI11
git checkout v1.9.0
mkdir build
cd build
cmake .. -DCLI11_BUILD_TESTS=OFF
make -j2
sudo make install

Now checkout MY_ENUM from the repo and build the MY_ENUM tests and example:

git clone https://github.com/facebook/MY_ENUM
cd MY_ENUM
git submodule update --init --recursive
mkdir build
cd build
cmake ..
make -j2

Join the MY_ENUM community

See the CONTRIBUTING file for how to help out.

License

MY_ENUM is MIT licensed, as found in the LICENSE file.

my_enum's People

Contributors

bigfootjon avatar facebook-github-bot avatar h-friederich avatar jhouben-fb avatar maichefb avatar mitrandir77 avatar steveonorato avatar

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.