本文整理汇总了C++中optional函数的典型用法代码示例。如果您正苦于以下问题:C++ optional函数的具体用法?C++ optional怎么用?C++ optional使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了optional函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: optional
HCI::optional HCI::getConnHandle(device &dev) {
conn_info info;
info.req.conn_num = 1;
info.req.dev_id = _dev_id;
if(ioctl(_hci_sock, HCIGETCONNLIST, &info) < 0) {
err::code = err::LIB_SYS;
return optional();
}
uint16_t handle;
for(hci_conn_info &x: info.info) {
if(bacmp(&x.bdaddr, &dev.bdaddr) == 0) {
handle = x.handle;
return optional(handle);
}
}
return optional();
}
示例2: fb_add_metadata
static fb_metadata_t *parse_metadata(fb_parser_t *P)
{
fb_token_t *t, *t0;
fb_metadata_t *md = 0;
if (!(t0 = optional(P, '('))) {
return 0;
}
if ((t = optional(P, LEX_TOK_ID)))
for (;;) {
fb_add_metadata(P, &md);
md->ident = t;
if (optional(P, ':')) {
parse_value(P, &md->value, allow_string_value, "scalar or string value expected");
}
if (P->failed >= FLATCC_MAX_ERRORS) {
return md;
}
if (!optional(P, ',')) {
break;
}
if (!(t = match(P, LEX_TOK_ID, "attribute name expected identifier after ','"))) {
break;
}
}
advance(P, ')', "metadata expected ')' to match", t0);
revert_metadata(&md);
return md;
}
示例3: mandatory
void furn_t::load( JsonObject &jo )
{
mandatory( jo, was_loaded, "name", name, translated_string_reader );
mandatory( jo, was_loaded, "move_cost_mod", movecost );
mandatory( jo, was_loaded, "required_str", move_str_req );
optional( jo, was_loaded, "max_volume", max_volume, legacy_volume_reader, DEFAULT_MAX_VOLUME_IN_SQUARE );
optional( jo, was_loaded, "crafting_pseudo_item", crafting_pseudo_item, "" );
load_symbol( jo );
transparent = false;
for( auto & flag : jo.get_string_array( "flags" ) ) {
set_flag( flag );
}
if( jo.has_member( "examine_action" ) ) {
examine = iexamine_function_from_string( jo.get_string( "examine_action" ) );
} else {
examine = iexamine_function_from_string( "none" );
}
optional( jo, was_loaded, "open", open, string_id_reader<furn_t> {}, NULL_ID );
optional( jo, was_loaded, "close", close, string_id_reader<furn_t> {}, NULL_ID );
bash.load( jo, "bash", true );
deconstruct.load( jo, "deconstruct", true );
}
示例4: client
bool dto_complex::parse( const rapidjson::Value& json )
{
if( json.IsObject() && json.HasMember( key() ) && json[key()].IsObject() )
{
for( auto& child : get_children() )
{
child->set_client( client() );
if( !child->parse( json[key()] ) && !optional() )
{
return false;
}
}
return true;
}
else if( json.IsObject() )
{
for( auto& child : get_children() )
{
child->set_client( client() );
if( !child->parse( json ) && !optional() )
{
return false;
}
}
return true;
}
set_present( false );
return optional();
}
示例5: parse_enum_decl
/* `enum` must already be matched. */
static void parse_enum_decl(fb_parser_t *P, fb_compound_type_t *ct)
{
fb_token_t *t, *t0;
fb_member_t *member;
if (!(ct->symbol.ident = match(P, LEX_TOK_ID, "enum declaration expected identifier"))) {
goto fail;
}
if (optional(P, ':')) {
parse_type(P, &ct->type);
if (ct->type.type != vt_scalar_type) {
error_tok(P, ct->type.t, "integral type expected");
} else {
switch (ct->type.t->id) {
case tok_kw_float:
case tok_kw_double:
error_tok(P, ct->type.t, "integral type expected");
default:
break;
}
}
}
ct->metadata = parse_metadata(P);
if (!((t0 = match(P, '{', "enum declaration expected '{'")))) {
goto fail;
}
for (;;) {
if (!(t = match(P, LEX_TOK_ID,
"member identifier expected"))) {
goto fail;
}
if (P->failed >= FLATCC_MAX_ERRORS) {
goto fail;
}
member = fb_add_member(P, &ct->members);
member->symbol.ident = t;
if (optional(P, '=')) {
t = P->token;
parse_value(P, &member->value, 0, "integral constant expected");
/* Leave detailed type (e.g. no floats) and range checking to a later stage. */
}
/*
* Trailing comma is optional in flatc but not in grammar, we
* follow flatc.
*/
if (!optional(P, ',') || P->token->id == '}') {
break;
}
P->doc = 0;
}
if (t0) {
advance(P, '}', "enum missing closing '}' to match", t0);
}
revert_symbols(&ct->members);
return;
fail:
recover(P, '}', 1);
}
示例6: parse_type
/* ':' must already be matched */
static void parse_type(fb_parser_t *P, fb_value_t *v)
{
fb_token_t *t = 0;
fb_token_t *t0 = P->token;
int vector = 0;
v->type = vt_invalid;
while ((t = optional(P, '['))) {
++vector;
}
if (vector > 1) {
error_tok(P, t0, "vector type can only be one-dimensional");
}
switch (P->token->id) {
case tok_kw_int:
case tok_kw_bool:
case tok_kw_byte:
case tok_kw_long:
case tok_kw_uint:
case tok_kw_float:
case tok_kw_short:
case tok_kw_ubyte:
case tok_kw_ulong:
case tok_kw_ushort:
case tok_kw_double:
v->t = P->token;
v->type = vector ? vt_vector_type : vt_scalar_type;
next(P);
break;
case tok_kw_string:
v->t = P->token;
v->type = vector ? vt_vector_string_type : vt_string_type;
next(P);
break;
case LEX_TOK_ID:
parse_ref(P, &v->ref);
v->type = vector ? vt_vector_type_ref : vt_type_ref;
break;
case ']':
error_tok(P, t, "vector type cannot be empty");
break;
default:
error_tok(P, t, "invalid type specifier");
break;
}
while (optional(P, ']') && vector--) {
}
if (vector) {
error_tok_2(P, t, "vector type missing ']' to match", t0);
}
if ((t = optional(P, ']'))) {
error_tok_2(P, t, "extra ']' not matching", t0);
while (optional(P, ']')) {
}
}
}
示例7: mandatory
void overmap_connection::subtype::load( JsonObject &jo )
{
static const typed_flag_reader<decltype( connection_subtype_flag_map )> flag_reader{ connection_subtype_flag_map, "invalid connection subtype flag" };
mandatory( jo, false, "terrain", terrain );
mandatory( jo, false, "locations", locations );
optional( jo, false, "basic_cost", basic_cost, 0 );
optional( jo, false, "flags", flags, flag_reader );
}
示例8: optional
void species_type::load( JsonObject &jo, const std::string & )
{
const auto flag_reader = enum_flags_reader<m_flag> { "monster flag" };
optional( jo, was_loaded, "flags", flags, flag_reader );
const auto trigger_reader = enum_flags_reader<mon_trigger> { "monster trigger" };
optional( jo, was_loaded, "anger_triggers", anger, trigger_reader );
optional( jo, was_loaded, "placate_triggers", placate, trigger_reader );
optional( jo, was_loaded, "fear_triggers", fear, trigger_reader );
}
示例9: pgettext
void profession::load( JsonObject &jo, const std::string & )
{
//If the "name" is an object then we have to deal with gender-specific titles,
if( jo.has_object( "name" ) ) {
JsonObject name_obj = jo.get_object( "name" );
_name_male = pgettext( "profession_male", name_obj.get_string( "male" ).c_str() );
_name_female = pgettext( "profession_female", name_obj.get_string( "female" ).c_str() );
} else if( jo.has_string( "name" ) ) {
// Same profession names for male and female in English.
// Still need to different names in other languages.
const std::string name = jo.get_string( "name" );
_name_female = pgettext( "profession_female", name.c_str() );
_name_male = pgettext( "profession_male", name.c_str() );
} else if( !was_loaded ) {
jo.throw_error( "missing mandatory member \"name\"" );
}
if( !was_loaded || jo.has_member( "description" ) ) {
const std::string desc = jo.get_string( "description" );
// These also may differ depending on the language settings!
_description_male = pgettext( "prof_desc_male", desc.c_str() );
_description_female = pgettext( "prof_desc_female", desc.c_str() );
}
mandatory( jo, was_loaded, "points", _point_cost );
if( !was_loaded || jo.has_member( "items" ) ) {
JsonObject items_obj = jo.get_object( "items" );
if( items_obj.has_array( "both" ) ) {
optional( items_obj, was_loaded, "both", legacy_starting_items, item_reader {} );
}
if( items_obj.has_object( "both" ) ) {
_starting_items = item_group::load_item_group( *items_obj.get_raw( "both" ), "collection" );
}
if( items_obj.has_array( "male" ) ) {
optional( items_obj, was_loaded, "male", legacy_starting_items_male, item_reader {} );
}
if( items_obj.has_object( "male" ) ) {
_starting_items_male = item_group::load_item_group( *items_obj.get_raw( "male" ), "collection" );
}
if( items_obj.has_array( "female" ) ) {
optional( items_obj, was_loaded, "female", legacy_starting_items_female, item_reader {} );
}
if( items_obj.has_object( "female" ) ) {
_starting_items_female = item_group::load_item_group( *items_obj.get_raw( "female" ),
"collection" );
}
}
optional( jo, was_loaded, "no_bonus", no_bonus );
optional( jo, was_loaded, "skills", _starting_skills, skilllevel_reader {} );
optional( jo, was_loaded, "addictions", _starting_addictions, addiction_reader {} );
// TODO: use string_id<bionic_type> or so
optional( jo, was_loaded, "CBMs", _starting_CBMs, auto_flags_reader<bionic_id> {} );
// TODO: use string_id<mutation_branch> or so
optional( jo, was_loaded, "traits", _starting_traits, auto_flags_reader<trait_id> {} );
optional( jo, was_loaded, "flags", flags, auto_flags_reader<> {} );
}
示例10: optional
void species_type::load( JsonObject &jo )
{
MonsterGenerator &gen = MonsterGenerator::generator();
const typed_flag_reader<decltype( gen.flag_map )> flag_reader{ gen.flag_map, "invalid monster flag" };
optional( jo, was_loaded, "flags", flags, flag_reader );
const typed_flag_reader<decltype( gen.trigger_map )> trigger_reader{ gen.trigger_map, "invalid monster trigger" };
optional( jo, was_loaded, "anger_triggers", anger_trig, trigger_reader );
optional( jo, was_loaded, "placate_triggers", placate_trig, trigger_reader );
optional( jo, was_loaded, "fear_triggers", fear_trig, trigger_reader );
}
示例11: mandatory
void mission_type::load( JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "name", name, translated_string_reader );
mandatory( jo, was_loaded, "difficulty", difficulty );
mandatory( jo, was_loaded, "value", value );
auto djo = jo.get_object( "dialogue" );
// @todo There should be a cleaner way to do it
mandatory( djo, was_loaded, "describe", dialogue[ "describe" ] );
mandatory( djo, was_loaded, "offer", dialogue[ "offer" ] );
mandatory( djo, was_loaded, "accepted", dialogue[ "accepted" ] );
mandatory( djo, was_loaded, "rejected", dialogue[ "rejected" ] );
mandatory( djo, was_loaded, "advice", dialogue[ "advice" ] );
mandatory( djo, was_loaded, "inquire", dialogue[ "inquire" ] );
mandatory( djo, was_loaded, "success", dialogue[ "success" ] );
mandatory( djo, was_loaded, "success_lie", dialogue[ "success_lie" ] );
mandatory( djo, was_loaded, "failure", dialogue[ "failure" ] );
optional( jo, was_loaded, "urgent", urgent );
optional( jo, was_loaded, "item", item_id );
optional( jo, was_loaded, "count", item_count, 1 );
goal = jo.get_enum_value<decltype(goal)>( "goal" );
assign_function( jo, "place", place, tripoint_function_map );
assign_function( jo, "start", start, mission_function_map );
assign_function( jo, "end", end, mission_function_map );
assign_function( jo, "fail", fail, mission_function_map );
if( jo.has_int( "deadline_low" ) ) {
deadline_low = DAYS( jo.get_int( "deadline_low" ) );
}
if( jo.has_int( "deadline_high" ) ) {
deadline_high = DAYS( jo.get_int( "deadline_high" ) );
}
if( jo.has_member( "origins" ) ) {
origins.clear();
for( auto &m : jo.get_tags( "origins" ) ) {
origins.emplace_back( io::string_to_enum_look_up( io::origin_map, m ) );
}
}
if( jo.has_member( "followup" ) ) {
follow_up = mission_type_id( jo.get_string( "followup" ) );
}
if( jo.has_member( "destination" ) ) {
target_id = oter_id( jo.get_string( "destination" ) );
}
}
示例12: parse_cmdline
// Command line parsing
std::pair<bool, etix::tool::opt_parse>
parse_cmdline(int argc, char* argv[]) {
auto opt_parse = etix::tool::opt_parse{ argc, argv };
opt_parse.optional("-c", "Path to the configuration file (-c /path/to/conf)", true);
opt_parse.optional("-l", "Set log level (-l 4 will only show warnings and errors)", true);
opt_parse.optional("-d", "Launch the discovery tool on the given subnet", false);
opt_parse.optional("-b", "Launch the bruteforce tool on all discovered devices", false);
opt_parse.optional("-t", "Generate thumbnails from detected cameras", false);
opt_parse.optional("-g", "Check if the stream can be opened with GStreamer", false);
opt_parse.optional("-v", "Display Cameradar's version", false);
opt_parse.optional("-h", "Display this help", false);
opt_parse.execute();
if (opt_parse.exist("-h")) {
opt_parse.print_help();
return std::make_pair(false, opt_parse);
} else if (opt_parse.exist("-v")) {
print_version();
return std::make_pair(false, opt_parse);
} else if (opt_parse.has_error()) {
std::cout << "Usage: ./cameradar [option]\n\toptions:\n" << std::endl;
opt_parse.print_help();
return std::make_pair(false, opt_parse);
}
return std::make_pair(true, opt_parse);
}
示例13: mandatory
void material_type::load( JsonObject &jsobj )
{
mandatory( jsobj, was_loaded, "name", _name, translated_string_reader );
mandatory( jsobj, was_loaded, "bash_resist", _bash_resist );
mandatory( jsobj, was_loaded, "cut_resist", _cut_resist );
mandatory( jsobj, was_loaded, "acid_resist", _acid_resist );
mandatory( jsobj, was_loaded, "elec_resist", _elec_resist );
mandatory( jsobj, was_loaded, "fire_resist", _fire_resist );
mandatory( jsobj, was_loaded, "chip_resist", _chip_resist );
mandatory( jsobj, was_loaded, "density", _density );
optional( jsobj, was_loaded, "salvaged_into", _salvaged_into, "null" );
optional( jsobj, was_loaded, "repaired_with", _repaired_with, "null" );
optional( jsobj, was_loaded, "edible", _edible, false );
optional( jsobj, was_loaded, "soft", _soft, false );
auto arr = jsobj.get_array( "vitamins" );
while( arr.has_more() ) {
auto pair = arr.next_array();
_vitamins.emplace( vitamin_id( pair.get_string( 0 ) ), pair.get_float( 1 ) );
}
mandatory( jsobj, was_loaded, "bash_dmg_verb", _bash_dmg_verb, translated_string_reader );
mandatory( jsobj, was_loaded, "cut_dmg_verb", _cut_dmg_verb, translated_string_reader );
JsonArray jsarr = jsobj.get_array( "dmg_adj" );
while( jsarr.has_more() ) {
_dmg_adj.push_back( _( jsarr.next_string().c_str() ) );
}
JsonArray burn_data_array = jsobj.get_array( "burn_data" );
for( size_t intensity = 0; intensity < MAX_FIELD_DENSITY; intensity++ ) {
if( burn_data_array.has_more() ) {
JsonObject brn = burn_data_array.next_object();
_burn_data[ intensity ] = load_mat_burn_data( brn );
} else {
// If not specified, supply default
bool flammable = _fire_resist <= ( int )intensity;
mat_burn_data mbd;
if( flammable ) {
mbd.burn = 1;
}
_burn_data[ intensity ] = mbd;
}
}
}
示例14: parse_union_decl
/* `union` must already be matched. */
static void parse_union_decl(fb_parser_t *P, fb_compound_type_t *ct)
{
fb_token_t *t0;
fb_member_t *member;
fb_ref_t *ref;
if (!(ct->symbol.ident = match(P, LEX_TOK_ID, "union declaration expected identifier"))) {
goto fail;
}
ct->metadata = parse_metadata(P);
if (!((t0 = match(P, '{', "union declaration expected '{'")))) {
goto fail;
}
for (;;) {
if (P->token->id != LEX_TOK_ID) {
error_tok(P, P->token, "union expects an identifier");
goto fail;
}
if (P->failed >= FLATCC_MAX_ERRORS) {
goto fail;
}
member = fb_add_member(P, &ct->members);
parse_ref(P, &ref);
member->type.ref = ref;
member->type.type = vt_type_ref;
while (ref->link) {
ref = ref->link;
}
/* The union member is the unqualified reference. */
member->symbol.ident = ref->ident;
if (optional(P, '=')) {
parse_value(P, &member->value, 0, "integral constant expected");
/* Leave detailed type (e.g. no floats) and range checking to a later stage. */
}
if (!optional(P, ',') || P->token->id == '}') {
break;
}
P->doc = 0;
}
advance(P, '}', "union missing closing '}' to match", t0);
revert_symbols(&ct->members);
/* Add implicit `NONE` member first in the list. */
member = fb_add_member(P, &ct->members);
member->symbol.ident = &P->t_none;
return;
fail:
recover2(P, ';', 1, '}', 0);
}
示例15: parse_method
static void parse_method(fb_parser_t *P, fb_member_t *fld)
{
fb_token_t *t;
if (!(t = match(P, LEX_TOK_ID, "method expected identifier"))) {
goto fail;
}
fld->symbol.ident = t;
if (!match(P, '(', "method expected '(' after identifier")) {
goto fail;
}
parse_type(P, &fld->req_type);
if (!match(P, ')', "method expected ')' after request type")) {
goto fail;
}
if (!match(P, ':', "method expected ':' before mandatory response type")) {
goto fail;
}
parse_type(P, &fld->type);
if ((t = optional(P, '='))) {
error_tok(P, t, "method does not accept an initializer");
goto fail;
}
fld->metadata = parse_metadata(P);
advance(P, ';', "method must be terminated with ';'", 0);
return;
fail:
recover2(P, ';', 1, '}', 0);
}