Skip to content

Commit

Permalink
Merge pull request #50138 from mqrause/private_contents_pt3
Browse files Browse the repository at this point in the history
Make contents member of item class private final part
  • Loading branch information
kevingranade committed Jul 23, 2021
2 parents 32ee909 + e1a3575 commit 7f33807
Show file tree
Hide file tree
Showing 57 changed files with 190 additions and 158 deletions.
1 change: 0 additions & 1 deletion src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "handle_liquid.h"
#include "iexamine.h"
#include "item.h"
#include "item_contents.h"
#include "item_group.h"
#include "item_location.h"
#include "itype.h"
Expand Down
1 change: 0 additions & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
#include "iexamine.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_factory.h"
#include "item_location.h"
#include "item_pocket.h"
Expand Down
1 change: 0 additions & 1 deletion src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "iexamine.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_location.h"
#include "itype.h"
#include "iuse.h"
Expand Down
3 changes: 1 addition & 2 deletions src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "inventory.h"
#include "item.h"
#include "item_category.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_pocket.h"
#include "item_stack.h"
Expand Down Expand Up @@ -340,7 +339,7 @@ void advanced_inventory::print_items( const advanced_inventory_pane &pane, bool
if( !active ) {
thiscolor = norm;
} else if( it.is_food_container() && !it.is_craft() && it.num_item_stacks() == 1 ) {
thiscolor = it.contents.all_items_top().front()->color_in_inventory();
thiscolor = it.all_items_top().front()->color_in_inventory();
} else {
thiscolor = it.color_in_inventory();
}
Expand Down
1 change: 0 additions & 1 deletion src/advanced_inv_area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "game_constants.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "map.h"
#include "map_selector.h"
#include "mapdata.h"
Expand Down
1 change: 0 additions & 1 deletion src/advanced_inv_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "cata_assert.h"
#include "flag.h"
#include "item.h"
#include "item_contents.h"
#include "item_pocket.h"
#include "item_search.h"
#include "make_static.h"
Expand Down
1 change: 0 additions & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
#include "handle_liquid.h"
#include "input.h"
#include "inventory.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_pocket.h"
#include "item_stack.h"
Expand Down
1 change: 0 additions & 1 deletion src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "flat_set.h"
#include "game_constants.h"
#include "item.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_pocket.h"
#include "magic_enchantment.h"
Expand Down
1 change: 0 additions & 1 deletion src/computer_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "game_inventory.h"
#include "input.h"
#include "item.h"
#include "item_contents.h"
#include "item_factory.h"
#include "item_location.h"
#include "item_pocket.h"
Expand Down
1 change: 0 additions & 1 deletion src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "game.h"
#include "item.h"
#include "item_category.h"
#include "item_contents.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
Expand Down
1 change: 0 additions & 1 deletion src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "handle_liquid.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_pocket.h"
#include "item_stack.h"
Expand Down
1 change: 0 additions & 1 deletion src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_group.h"
#include "item_pocket.h"
#include "item_stack.h"
Expand Down
1 change: 0 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
#include "inventory.h"
#include "item.h"
#include "item_category.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_pocket.h"
#include "item_stack.h"
Expand Down
1 change: 0 additions & 1 deletion src/game_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "inventory.h"
#include "inventory_ui.h"
#include "item.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_pocket.h"
#include "itype.h"
Expand Down
1 change: 0 additions & 1 deletion src/handle_liquid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "game_inventory.h"
#include "iexamine.h"
#include "item.h"
#include "item_contents.h"
#include "itype.h"
#include "line.h"
#include "map.h"
Expand Down
1 change: 0 additions & 1 deletion src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "flag.h"
#include "iexamine.h"
#include "inventory_ui.h" // auto inventory blocking
#include "item_contents.h"
#include "item_pocket.h"
#include "item_stack.h"
#include "map.h"
Expand Down
1 change: 0 additions & 1 deletion src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "inventory.h"
#include "item.h"
#include "item_category.h"
#include "item_contents.h"
#include "item_pocket.h"
#include "item_search.h"
#include "item_stack.h"
Expand Down
73 changes: 60 additions & 13 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ item &item::convert( const itype_id &new_type )
item temp( *this );
temp.contents = item_contents( type->pockets );
for( const item *it : contents.mods() ) {
if( !temp.contents.insert_item( *it, item_pocket::pocket_type::MOD ).success() ) {
if( !temp.put_in( *it, item_pocket::pocket_type::MOD ).success() ) {
debugmsg( "failed to insert mod" );
}
}
Expand Down Expand Up @@ -1190,6 +1190,11 @@ ret_val<bool> item::put_in( const item &payload, item_pocket::pocket_type pk_typ
}
}

void item::force_insert_item( const item &it, item_pocket::pocket_type pk_type )
{
contents.force_insert_item( it, pk_type );
}

void item::set_var( const std::string &name, const int value )
{
std::ostringstream tmpstream;
Expand Down Expand Up @@ -5020,15 +5025,11 @@ std::string item::tname( unsigned int quantity, bool with_prefix, unsigned int t
// in their name, also food is active while it rots.
tagtext += _( " (active)" );
}
switch( contents.get_sealed_summary() ) {
case item_contents::sealed_summary::unsealed:
break;
case item_contents::sealed_summary::part_sealed:
tagtext += _( " (part sealed)" );
break;
case item_contents::sealed_summary::all_sealed:
tagtext += _( " (sealed)" );
break;

if( all_pockets_sealed() ) {
tagtext += _( " (sealed)" );
} else if( any_pockets_sealed() ) {
tagtext += _( " (part sealed)" );
}

if( is_favorite ) {
Expand Down Expand Up @@ -7234,6 +7235,16 @@ bool item::seal()
}
}

bool item::all_pockets_sealed() const
{
return contents.all_pockets_sealed();
}

bool item::any_pockets_sealed() const
{
return contents.any_pockets_sealed();
}

bool item::is_container() const
{
return contents.has_pocket_type( item_pocket::pocket_type::CONTAINER );
Expand Down Expand Up @@ -8054,6 +8065,21 @@ int item::ammo_required() const
return 0;
}

item &item::first_ammo()
{
return contents.first_ammo();
}

const item &item::first_ammo() const
{
return contents.first_ammo();
}

void item::handle_liquid_or_spill( Character &guy, const item *avoid )
{
contents.handle_liquid_or_spill( guy, avoid );
}

bool item::ammo_sufficient( const Character *carrier, int qty ) const
{
if( ammo_required() ) {
Expand Down Expand Up @@ -8665,7 +8691,7 @@ bool item::reload( Character &u, item_location ammo, int qty )
if( ammo->has_flag( flag_SPEEDLOADER ) ) {
container = ammo;
// if the thing passed in is a speed loader, we want the ammo
ammo = item_location( ammo, &ammo->contents.first_ammo() );
ammo = item_location( ammo, &ammo->first_ammo() );
}

if( !is_reloadable_with( ammo->typeId() ) ) {
Expand Down Expand Up @@ -8701,9 +8727,9 @@ bool item::reload( Character &u, item_location ammo, int qty )

if( ammo->has_flag( flag_SPEEDLOADER ) ) {
// sets curammo to one of the ammo types contained
curammo = ammo->contents.first_ammo().type;
curammo = ammo->first_ammo().type;
qty = std::min( qty, ammo->ammo_remaining() );
item ammo_copy( ammo->contents.first_ammo() );
item ammo_copy( ammo->first_ammo() );
ammo_copy.charges = qty;
put_in( ammo_copy, item_pocket::pocket_type::MAGAZINE );
ammo->ammo_consume( qty, tripoint_zero, &u );
Expand Down Expand Up @@ -9010,6 +9036,11 @@ int item::get_remaining_capacity_for_liquid( const item &liquid, const Character
return res;
}

units::volume item::total_contained_volume() const
{
return contents.total_contained_volume();
}

bool item::use_amount( const itype_id &it, int &quantity, std::list<item> &used,
const std::function<bool( const item & )> &filter )
{
Expand Down Expand Up @@ -11083,6 +11114,12 @@ int item::get_recursive_disassemble_moves( const Character &guy ) const
return moves;
}

void item::remove_internal( const std::function<bool( item & )> &filter,
int &count, std::list<item> &res )
{
contents.remove_internal( filter, count, res );
}

std::list<const item *> item::all_items_top() const
{
return contents.all_items_top();
Expand Down Expand Up @@ -11176,6 +11213,11 @@ const item &item::only_item() const
return contents.only_item();
}

item *item::get_item_with( const std::function<bool( const item & )> &filter )
{
return contents.get_item_with( filter );
}

size_t item::num_item_stacks() const
{
return contents.num_item_stacks();
Expand All @@ -11195,3 +11237,8 @@ void item::favorite_settings_menu( const std::string &item_name )
{
contents.favorite_settings_menu( item_name );
}

void item::combine( const item_contents &read_input, bool convert )
{
contents.combine( read_input, convert );
}
30 changes: 29 additions & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,9 @@ class item : public visitable

// seal the item's pockets. used for crafting and spawning items.
bool seal();

bool all_pockets_sealed() const;
bool any_pockets_sealed() const;
/** Whether this is container. Note that container does not necessarily means it's
* suitable for liquids. */
bool is_container() const;
Expand Down Expand Up @@ -790,6 +793,9 @@ class item : public visitable
std::string *err = nullptr ) const;
int get_remaining_capacity_for_liquid( const item &liquid, const Character &p,
std::string *err = nullptr ) const;

units::volume total_contained_volume() const;

/**
* It returns the maximum volume of any contents, including liquids,
* ammo, magazines, weapons, etc.
Expand All @@ -816,6 +822,7 @@ class item : public visitable
*/
ret_val<bool> put_in( const item &payload, item_pocket::pocket_type pk_type,
bool unseal_pockets = false );
void force_insert_item( const item &it, item_pocket::pocket_type pk_type );

/**
* Returns this item into its default container. If it does not have a default container,
Expand Down Expand Up @@ -1881,6 +1888,16 @@ class item : public visitable

/** Quantity of ammunition consumed per usage of tool or with each shot of gun */
int ammo_required() const;
// gets the first ammo in all magazine pockets
// does not support multiple magazine pockets!
item &first_ammo();
// gets the first ammo in all magazine pockets
// does not support multiple magazine pockets!
const item &first_ammo() const;
// spills liquid and other contents from the container. contents may remain
// in the container if the player cancels spilling. removing liquid from
// a magazine requires unload logic.
void handle_liquid_or_spill( Character &guy, const item *avoid = nullptr );

/**
* Check if sufficient ammo is loaded for given number of uses.
Expand Down Expand Up @@ -2283,6 +2300,14 @@ class item : public visitable
// inherited from visitable
VisitResponse visit_items( const std::function<VisitResponse( item *, item * )> &func ) const
override;
/**
* @relates visitable
* NOTE: upon expansion, this may need to be filtered by type enum depending on accessibility
*/
VisitResponse visit_contents( const std::function<VisitResponse( item *, item * )> &func,
item *parent = nullptr );
void remove_internal( const std::function<bool( item & )> &filter,
int &count, std::list<item> &res );
std::list<item> remove_items_with( const std::function<bool( const item & )> &filter,
int count = INT_MAX ) override;

Expand Down Expand Up @@ -2313,6 +2338,7 @@ class item : public visitable
// gets the item contained IFF one item is contained (CONTAINER pocket), otherwise a null item reference
item &only_item();
const item &only_item() const;
item *get_item_with( const std::function<bool( const item & )> &filter );

/**
* returns the number of items stacks in contents
Expand All @@ -2333,6 +2359,8 @@ class item : public visitable
*/
void favorite_settings_menu( const std::string &item_name );

void combine( const item_contents &read_input, bool convert = false );

private:
/** migrates an item into this item. */
void migrate_content_item( const item &contained );
Expand Down Expand Up @@ -2404,12 +2432,12 @@ class item : public visitable
static const int INFINITE_CHARGES;

const itype *type;
item_contents contents;
std::list<item> components;
/** What faults (if any) currently apply to this item */
std::set<fault_id> faults;

private:
item_contents contents;
/** `true` if item has any of the flags that require processing in item::process_internal.
* This flag is reset to `true` if item tags are changed.
*/
Expand Down
Loading

0 comments on commit 7f33807

Please sign in to comment.