Skip to content

feat: add custom parameter based on a map of values #345

Description

Currently the PARAMETER macro only accepts a regex string. But there are quite some occasions where a custom parameter is nothing more than mapping a string to values. This can for example be used to create a custom parameter based on enums:

// somewhere
enum class State{
 connected,
 disconnected,
 standby,
 shutdown
}

namespace{
  const std::map<std::string, State> stateMap{
   {"connected", State::connected},
   {"disconnected", State::disconnected},
   {"standby", State::standby},
   {"shutdown", State::shutdown},
  };
}

PARAMETER_MAP("state", stateMap, true);

// PARAMETER_MAP does the heavy lifting of transforming all the keys from the provided
// map in to a regex that would look like: `(connected|disconnected|standby|shutdown)`
// it also takes care of creating a simple implementation that takes a string
// and uses the .at function on the provided map using the captured argument
// it could potentially look something like this (pseudo-code-ish)
#define PARAMETER_MAP(name, map, snippet) \
PARAMETER(decltype(map)::value_type, (name, transformKeysToRegex(map), snippet), (const std::string& key)) \
{ \
  return map.at(key); \
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions