Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

units::volume and units::mass readers for mandatory and optional #36354

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/generic_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,38 @@ class auto_flags_reader : public generic_typed_reader<auto_flags_reader<FlagType

using string_reader = auto_flags_reader<>;

class volume_reader : public generic_typed_reader<units::volume>
{
public:
bool operator()( const JsonObject &jo, const std::string &member_name,
units::volume &member, bool /* was_loaded */ ) const {
if( !jo.has_member( member_name ) ) {
return false;
}
member = read_from_json_string<units::volume>( *jo.get_raw( member_name ), units::volume_units );
return true;
}
units::volume get_next( JsonIn &jin ) const {
return read_from_json_string<units::volume>( jin, units::volume_units );
}
};

class mass_reader : public generic_typed_reader<units::mass>
{
public:
bool operator()( const JsonObject &jo, const std::string &member_name,
units::mass &member, bool /* was_loaded */ ) const {
if( !jo.has_member( member_name ) ) {
return false;
}
member = read_from_json_string<units::mass>( *jo.get_raw( member_name ), units::mass_units );
return true;
}
units::mass get_next( JsonIn &jin ) const {
return read_from_json_string<units::mass>( jin, units::mass_units );
}
};

/**
* Uses a map (unordered or standard) to convert strings from JSON to some other type
* (the mapped type of the map: `C::mapped_type`). It works for all mapped types, not just enums.
Expand Down
2 changes: 1 addition & 1 deletion src/mapdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ void furn_t::load( const JsonObject &jo, const std::string &src )
optional( jo, was_loaded, "bonus_fire_warmth_feet", bonus_fire_warmth_feet, 300 );
optional( jo, was_loaded, "keg_capacity", keg_capacity, legacy_volume_reader, 0_ml );
mandatory( jo, was_loaded, "required_str", move_str_req );
assign( jo, "max_volume", max_volume, src == "dda" );
optional( jo, was_loaded, "max_volume", max_volume, volume_reader(), 0_ml );
optional( jo, was_loaded, "crafting_pseudo_item", crafting_pseudo_item, "" );
optional( jo, was_loaded, "deployed_item", deployed_item );
load_symbol( jo );
Expand Down
27 changes: 27 additions & 0 deletions src/units.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "json.h"
#include "units.h"

namespace units
{
template<>
void volume::serialize( JsonOut &jsout ) const
{
if( value_ % 1000 == 0 ) {
jsout.write( string_format( "%d L", value_ ) );
} else {
jsout.write( string_format( "%d ml", value_ ) );
}
}

template<>
void mass::serialize( JsonOut &jsout ) const
{
if( value_ % 1000000 == 0 ) {
jsout.write( string_format( "%d kg", value_ ) );
} else if( value_ % 1000 == 0 ) {
jsout.write( string_format( "%d g", value_ ) );
} else {
jsout.write( string_format( "%d mg", value_ ) );
}
}
} // namespace units
5 changes: 5 additions & 0 deletions src/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ static const std::vector<std::pair<std::string, money>> money_units = { {
{ "kUSD", 1_kUSD },
}
};
static const std::vector<std::pair<std::string, volume>> volume_units = { {
{ "ml", 1_ml },
{ "L", 1_liter }
}
};
} // namespace units

template<typename T>
Expand Down