Skip to content

Commit

Permalink
units::volume and units::mass readers for mandatory and optional
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT authored and kevingranade committed Dec 24, 2019
1 parent c92dec0 commit 1d87bc5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
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

0 comments on commit 1d87bc5

Please sign in to comment.