Skip to content

Instantly share code, notes, and snippets.

@tristan0x
Last active August 16, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristan0x/cfd8366c4e064fd04506b85029bd03ed to your computer and use it in GitHub Desktop.
Save tristan0x/cfd8366c4e064fd04506b85029bd03ed to your computer and use it in GitHub Desktop.
/*
MIT License
Copyright (c) 2017 Tristan CAREL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <string>
#include <sstream>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
// Human way to describe runtime scenario
enum use_case_enum {
case_1,
case_2,
case_3
};
// Trait to get the parameter type according to the scenario
template <use_case_enum uc>
struct parameter_type_trait {
};
template <>
struct parameter_type_trait<case_1> {
typedef int value_type;
};
template <>
struct parameter_type_trait<case_2> {
typedef std::string value_type;
};
template <>
struct parameter_type_trait<case_3> {
typedef float value_type;
};
// Some work on a boost::variant
class process: public boost::static_visitor<std::string> {
public:
// the parameter type of operator() member methods below
// is a little bit cumbersome.
// the real type suffices (int, std::string, and float)
std::string operator()(parameter_type_trait<case_1>::value_type& value) const {
return "case_1: " + std::to_string(value);
}
std::string operator()(parameter_type_trait<case_2>::value_type& value) const {
return "case_2: " + value;
}
std::string operator()(parameter_type_trait<case_3>::value_type& value) const {
std::ostringstream ss;
ss << "case_3: " << value;
return ss.str();
}
};
// boost::variant of possible input parameter types
typedef boost::variant
<
parameter_type_trait<case_1>::value_type,
parameter_type_trait<case_2>::value_type,
parameter_type_trait<case_3>::value_type
> parameter_type;
// ----------------------------------------------------
// it's time to use all of those
#include <iostream>
/**
* For instance:
* ./a.out 1 42
* ./a.out 2 42
* ./a.out 3 42
*/
int main(int argc, char** argv) {
if (argc != 3) {
std::cerr << "Error. Usage: %s use_case parameter_value" << std::endl;
}
const int use_case = boost::lexical_cast<int>(argv[1]);
// the parameter, initialized for now.
parameter_type param;
// assign the parameter according to the use_case
if (use_case == 1) {
param = boost::lexical_cast<parameter_type_trait<case_1>::value_type>(argv[2]);
} else if (use_case == 2) {
param = boost::lexical_cast<parameter_type_trait<case_2>::value_type>(argv[2]);
} else if (use_case == 3) {
param = boost::lexical_cast<parameter_type_trait<case_3>::value_type>(argv[2]);
} else {
std::cerr << "Unknown use case" << std::endl;
}
// let's run an operation taking the variant in parameter.
std::cout << boost::apply_visitor(process(), param) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment