Skip to content

Commit

Permalink
Migrate AllCastles and AllHeroes to std::unique_ptr (#9153)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz authored Oct 5, 2024
1 parent 48461ec commit debb7e5
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 185 deletions.
86 changes: 56 additions & 30 deletions src/fheroes2/castle/castle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cassert>
#include <iterator>
#include <sstream>
#include <utility>

#include "agg_image.h"
#include "ai_planner.h"
Expand Down Expand Up @@ -2427,35 +2428,16 @@ Castle * VecCastles::GetFirstCastle() const

AllCastles::AllCastles()
{
// reserve memory
_castles.reserve( maximumCastles );
}

AllCastles::~AllCastles()
void AllCastles::AddCastle( std::unique_ptr<Castle> && castle )
{
Clear();
}

void AllCastles::Init()
{
Clear();
}

void AllCastles::Clear()
{
std::for_each( begin(), end(), []( const Castle * castle ) {
assert( castle != nullptr );
assert( castle );

delete castle;
} );
const fheroes2::Point & center = castle->GetCenter();

_castles.clear();
_castleTiles.clear();
}

void AllCastles::AddCastle( Castle * castle )
{
_castles.push_back( castle );
_castles.emplace_back( std::move( castle ) );

/* Register position of all castle elements on the map
Castle element positions are:
Expand All @@ -2471,7 +2453,6 @@ void AllCastles::AddCastle( Castle * castle )
*/

const size_t id = _castles.size() - 1;
const fheroes2::Point & center = castle->GetCenter();

// Castles are added from top to bottom, from left to right.
// Tiles containing castle ID cannot be overwritten.
Expand All @@ -2494,11 +2475,56 @@ void AllCastles::AddCastle( Castle * castle )
}
}

void AllCastles::Scout( int colors ) const
Castle * AllCastles::Get( const fheroes2::Point & position ) const
{
auto iter = _castleTiles.find( position );
if ( iter == _castleTiles.end() ) {
return nullptr;
}

assert( iter->second < _castles.size() && _castles[iter->second] );

return _castles[iter->second].get();
}

void AllCastles::Scout( const int colors ) const
{
for ( const Castle * castle : *this ) {
assert( castle != nullptr );

if ( !( castle->GetColor() & colors ) ) {
continue;
}

castle->Scout();
}
}

void AllCastles::NewDay() const
{
for ( auto it = begin(); it != end(); ++it )
if ( colors & ( *it )->GetColor() )
( *it )->Scout();
std::for_each( begin(), end(), []( Castle * castle ) {
assert( castle != nullptr );

castle->ActionNewDay();
} );
}

void AllCastles::NewWeek() const
{
std::for_each( begin(), end(), []( Castle * castle ) {
assert( castle != nullptr );

castle->ActionNewWeek();
} );
}

void AllCastles::NewMonth() const
{
std::for_each( begin(), end(), []( const Castle * castle ) {
assert( castle != nullptr );

castle->ActionNewMonth();
} );
}

OStreamBase & operator<<( OStreamBase & stream, const Castle & castle )
Expand Down Expand Up @@ -2608,10 +2634,10 @@ IStreamBase & operator>>( IStreamBase & stream, AllCastles & castles )
castles.Clear();

for ( uint32_t i = 0; i < size; ++i ) {
Castle * castle = new Castle();
auto castle = std::make_unique<Castle>();
stream >> *castle;

castles.AddCastle( castle );
castles.AddCastle( std::move( castle ) );
}

return stream;
Expand Down
69 changes: 35 additions & 34 deletions src/fheroes2/castle/castle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
#ifndef H2CASTLE_H
#define H2CASTLE_H

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "army.h"
Expand Down Expand Up @@ -121,7 +120,7 @@ enum class BuildingStatus : int32_t
LACK_RESOURCES
};

class Castle : public MapPosition, public BitModes, public ColorBase, public Control
class Castle final : public MapPosition, public BitModes, public ColorBase, public Control
{
public:
// Maximum number of creature dwellings that can be built in a castle
Expand Down Expand Up @@ -463,59 +462,61 @@ class AllCastles
AllCastles();
AllCastles( const AllCastles & ) = delete;

~AllCastles();
~AllCastles() = default;

AllCastles & operator=( const AllCastles & ) = delete;

void Init();
void Clear();

void AddCastle( Castle * castle );

Castle * Get( const fheroes2::Point & position ) const
auto begin() const noexcept
{
auto iter = _castleTiles.find( position );
if ( iter == _castleTiles.end() )
return nullptr;

return _castles[iter->second];
return Iterator( _castles.begin() );
}

void Scout( int ) const;

void NewDay()
auto end() const noexcept
{
std::for_each( _castles.begin(), _castles.end(), []( Castle * castle ) { castle->ActionNewDay(); } );
return Iterator( _castles.end() );
}

void NewWeek()
void Init()
{
std::for_each( _castles.begin(), _castles.end(), []( Castle * castle ) { castle->ActionNewWeek(); } );
Clear();
}

void NewMonth()
void Clear()
{
std::for_each( _castles.begin(), _castles.end(), []( const Castle * castle ) { castle->ActionNewMonth(); } );
_castles.clear();
_castleTiles.clear();
}

// begin/end methods so we can iterate through the elements
std::vector<Castle *>::const_iterator begin() const
size_t Size() const
{
return _castles.begin();
return _castles.size();
}

std::vector<Castle *>::const_iterator end() const
{
return _castles.end();
}
void AddCastle( std::unique_ptr<Castle> && castle );

size_t Size() const
Castle * Get( const fheroes2::Point & position ) const;

void Scout( const int colors ) const;

void NewDay() const;
void NewWeek() const;
void NewMonth() const;

template <typename BaseIterator>
struct Iterator : public BaseIterator
{
return _castles.size();
}
explicit Iterator( BaseIterator && other ) noexcept
: BaseIterator( std::move( other ) )
{}

auto operator*() const noexcept
{
return BaseIterator::operator*().get();
}
};

private:
std::vector<Castle *> _castles;
std::vector<std::unique_ptr<Castle>> _castles;
std::map<fheroes2::Point, size_t> _castleTiles;
};

Expand Down
1 change: 0 additions & 1 deletion src/fheroes2/castle/castle_building_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "castle_building_info.h"

#include <algorithm>
#include <cassert>
#include <cstdint>

Expand Down
Loading

0 comments on commit debb7e5

Please sign in to comment.