Dynamic switch depending on parameters loaded on startup [closed]
How can I create a switch function, that is created during runtime, depending on startup parameters.
My program loads it's configuration from a JSON during startup.
For each entry in that JSON file, there should be a entry in that switch function.
c++
closed as unclear what you're asking by Felix, eyllanesc, gsamaras, bolov, gnat Nov 17 '18 at 10:10
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
How can I create a switch function, that is created during runtime, depending on startup parameters.
My program loads it's configuration from a JSON during startup.
For each entry in that JSON file, there should be a entry in that switch function.
c++
closed as unclear what you're asking by Felix, eyllanesc, gsamaras, bolov, gnat Nov 17 '18 at 10:10
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Your question is about how to create a switch with strings?
– Dmitry Sazonov
Nov 16 '18 at 14:35
1
your question is very unclear. Give a "for instance"
– bolov
Nov 16 '18 at 14:46
add a comment |
How can I create a switch function, that is created during runtime, depending on startup parameters.
My program loads it's configuration from a JSON during startup.
For each entry in that JSON file, there should be a entry in that switch function.
c++
How can I create a switch function, that is created during runtime, depending on startup parameters.
My program loads it's configuration from a JSON during startup.
For each entry in that JSON file, there should be a entry in that switch function.
c++
c++
edited Nov 16 '18 at 14:34
Dmitry Sazonov
6,87912353
6,87912353
asked Nov 16 '18 at 12:32
Basti AnBasti An
7014
7014
closed as unclear what you're asking by Felix, eyllanesc, gsamaras, bolov, gnat Nov 17 '18 at 10:10
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Felix, eyllanesc, gsamaras, bolov, gnat Nov 17 '18 at 10:10
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Your question is about how to create a switch with strings?
– Dmitry Sazonov
Nov 16 '18 at 14:35
1
your question is very unclear. Give a "for instance"
– bolov
Nov 16 '18 at 14:46
add a comment |
Your question is about how to create a switch with strings?
– Dmitry Sazonov
Nov 16 '18 at 14:35
1
your question is very unclear. Give a "for instance"
– bolov
Nov 16 '18 at 14:46
Your question is about how to create a switch with strings?
– Dmitry Sazonov
Nov 16 '18 at 14:35
Your question is about how to create a switch with strings?
– Dmitry Sazonov
Nov 16 '18 at 14:35
1
1
your question is very unclear. Give a "for instance"
– bolov
Nov 16 '18 at 14:46
your question is very unclear. Give a "for instance"
– bolov
Nov 16 '18 at 14:46
add a comment |
1 Answer
1
active
oldest
votes
The easiest way is to use map of functors to handle options. But it really depends on your task. Something like this:
std::map< std::string, std::function< void( const std::string& ) > > handlers;
// In can be std::variant instead of std::string
handlers[ "key1" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
};
handlers[ "key2" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
}; //...
defaultHandler = (const std::string&)
{
throw "Not supported param";
};
// Somehow iterate, depends on your json parser
// Can be recursive
for ( const auto& keyVal : json )
{
const auto& key = keyVal.first; // JSON key
const auto& value= keyVal.second; // JSON value
const auto itHandler = handlers.find( key ); // Looking for handler
if ( itHandler != handlers.end() )
{
const auto& handler = itHandler.second;
handler( value ); // Use handler, it's a "content" of your "case" block
}
else
defaultHandler( value );
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The easiest way is to use map of functors to handle options. But it really depends on your task. Something like this:
std::map< std::string, std::function< void( const std::string& ) > > handlers;
// In can be std::variant instead of std::string
handlers[ "key1" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
};
handlers[ "key2" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
}; //...
defaultHandler = (const std::string&)
{
throw "Not supported param";
};
// Somehow iterate, depends on your json parser
// Can be recursive
for ( const auto& keyVal : json )
{
const auto& key = keyVal.first; // JSON key
const auto& value= keyVal.second; // JSON value
const auto itHandler = handlers.find( key ); // Looking for handler
if ( itHandler != handlers.end() )
{
const auto& handler = itHandler.second;
handler( value ); // Use handler, it's a "content" of your "case" block
}
else
defaultHandler( value );
}
add a comment |
The easiest way is to use map of functors to handle options. But it really depends on your task. Something like this:
std::map< std::string, std::function< void( const std::string& ) > > handlers;
// In can be std::variant instead of std::string
handlers[ "key1" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
};
handlers[ "key2" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
}; //...
defaultHandler = (const std::string&)
{
throw "Not supported param";
};
// Somehow iterate, depends on your json parser
// Can be recursive
for ( const auto& keyVal : json )
{
const auto& key = keyVal.first; // JSON key
const auto& value= keyVal.second; // JSON value
const auto itHandler = handlers.find( key ); // Looking for handler
if ( itHandler != handlers.end() )
{
const auto& handler = itHandler.second;
handler( value ); // Use handler, it's a "content" of your "case" block
}
else
defaultHandler( value );
}
add a comment |
The easiest way is to use map of functors to handle options. But it really depends on your task. Something like this:
std::map< std::string, std::function< void( const std::string& ) > > handlers;
// In can be std::variant instead of std::string
handlers[ "key1" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
};
handlers[ "key2" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
}; //...
defaultHandler = (const std::string&)
{
throw "Not supported param";
};
// Somehow iterate, depends on your json parser
// Can be recursive
for ( const auto& keyVal : json )
{
const auto& key = keyVal.first; // JSON key
const auto& value= keyVal.second; // JSON value
const auto itHandler = handlers.find( key ); // Looking for handler
if ( itHandler != handlers.end() )
{
const auto& handler = itHandler.second;
handler( value ); // Use handler, it's a "content" of your "case" block
}
else
defaultHandler( value );
}
The easiest way is to use map of functors to handle options. But it really depends on your task. Something like this:
std::map< std::string, std::function< void( const std::string& ) > > handlers;
// In can be std::variant instead of std::string
handlers[ "key1" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
};
handlers[ "key2" ] = ( const std::string& value )
{
std::cout << "Processing key1 in JSON, value is = " << value ;
}; //...
defaultHandler = (const std::string&)
{
throw "Not supported param";
};
// Somehow iterate, depends on your json parser
// Can be recursive
for ( const auto& keyVal : json )
{
const auto& key = keyVal.first; // JSON key
const auto& value= keyVal.second; // JSON value
const auto itHandler = handlers.find( key ); // Looking for handler
if ( itHandler != handlers.end() )
{
const auto& handler = itHandler.second;
handler( value ); // Use handler, it's a "content" of your "case" block
}
else
defaultHandler( value );
}
answered Nov 16 '18 at 14:44
Dmitry SazonovDmitry Sazonov
6,87912353
6,87912353
add a comment |
add a comment |
Your question is about how to create a switch with strings?
– Dmitry Sazonov
Nov 16 '18 at 14:35
1
your question is very unclear. Give a "for instance"
– bolov
Nov 16 '18 at 14:46