@@ -49,7 +49,7 @@ class GameStrategyRep;
4949using GameStrategy = GameObjectPtr<GameStrategyRep>;
5050
5151class GamePlayerRep ;
52- using GamePlayer = GameObjectPtr <GamePlayerRep>;
52+ using GamePlayer = GameObjectSharedPtr <GamePlayerRep>;
5353
5454class GameNodeRep ;
5555using GameNode = GameObjectPtr<GameNodeRep>;
@@ -271,7 +271,7 @@ class GameOutcomeRep : public std::enable_shared_from_this<GameOutcomeRep> {
271271 GameRep *m_game;
272272 int m_number;
273273 std::string m_label;
274- std::map<GamePlayerRep * , Number> m_payoffs;
274+ std::map<std::shared_ptr< GamePlayerRep> , Number> m_payoffs;
275275
276276public:
277277 // / @name Lifecycle
@@ -348,10 +348,10 @@ class GameInfosetRep : public std::enable_shared_from_this<GameInfosetRep> {
348348 template <class T > friend class MixedBehaviorProfile ;
349349
350350 bool m_valid{true };
351- GameRep *m_game;
352- int m_number;
351+ GameRep *m_game{ nullptr } ;
352+ int m_number{ 0 } ;
353353 std::string m_label;
354- GamePlayerRep * m_player;
354+ std::shared_ptr< GamePlayerRep> m_player;
355355 std::vector<std::shared_ptr<GameActionRep>> m_actions;
356356 std::vector<GameNodeRep *> m_members;
357357 int flag{0 }, whichbranch{0 };
@@ -371,7 +371,8 @@ class GameInfosetRep : public std::enable_shared_from_this<GameInfosetRep> {
371371 ~GameInfosetRep ();
372372
373373 static std::shared_ptr<GameInfosetRep> CreateInfoset (GameRep *p_efg, int p_number,
374- GamePlayerRep *p_player, int p_actions);
374+ std::shared_ptr<GamePlayerRep> p_player,
375+ int p_actions);
375376
376377 // / @name Validity management
377378 // @{
@@ -441,7 +442,7 @@ class GameStrategyRep : public GameObject {
441442 template <class T > friend class TableMixedStrategyProfileRep ;
442443 template <class T > friend class MixedBehaviorProfile ;
443444
444- GamePlayerRep * m_player;
445+ std::shared_ptr< GamePlayerRep> m_player;
445446 int m_number;
446447 long m_offset{-1L };
447448 std::string m_label;
@@ -450,7 +451,8 @@ class GameStrategyRep : public GameObject {
450451 // / @name Lifecycle
451452 // @{
452453 // / Creates a new strategy for the given player.
453- explicit GameStrategyRep (GamePlayerRep *p_player, int p_number, const std::string &p_label)
454+ explicit GameStrategyRep (std::shared_ptr<GamePlayerRep> p_player, int p_number,
455+ const std::string &p_label)
454456 : m_player(p_player), m_number(p_number), m_label(p_label)
455457 {
456458 }
@@ -475,7 +477,7 @@ class GameStrategyRep : public GameObject {
475477};
476478
477479// / A player in a game
478- class GamePlayerRep : public GameObject {
480+ class GamePlayerRep : public std ::enable_shared_from_this<GamePlayerRep> {
479481 friend class GameRep ;
480482 friend class GameExplicitRep ;
481483 friend class GameTreeRep ;
@@ -494,20 +496,27 @@ class GamePlayerRep : public GameObject {
494496 void MakeReducedStrats (class GameNodeRep *, class GameNodeRep *);
495497 // @}
496498
499+ bool m_valid{true };
497500 GameRep *m_game;
498501 int m_number;
499502 std::string m_label;
500503 std::vector<std::shared_ptr<GameInfosetRep>> m_infosets;
501504 std::vector<GameStrategyRep *> m_strategies;
502505
503- GamePlayerRep (GameRep *p_game, int p_id) : m_game(p_game), m_number(p_id) {}
504- GamePlayerRep (GameRep *p_game, int p_id, int m_strats);
505- ~GamePlayerRep () override ;
506-
507506public:
508507 using Infosets = SmartElementCollection<GamePlayer, GameInfosetRep>;
509508 using Strategies = ElementCollection<GamePlayer, GameStrategyRep>;
510509
510+ GamePlayerRep (GameRep *p_game, int p_id) : m_game(p_game), m_number(p_id) {}
511+ static std::shared_ptr<GamePlayerRep> CreatePlayer (GameRep *p_game, int p_id, int p_strats);
512+ ~GamePlayerRep ();
513+
514+ // / @name Validity management
515+ // @{
516+ bool IsValid () const { return m_valid; }
517+ void Invalidate () { m_valid = false ; }
518+ // @}
519+
511520 int GetNumber () const { return m_number; }
512521 Game GetGame () const ;
513522
@@ -521,7 +530,10 @@ class GamePlayerRep : public GameObject {
521530 // / Returns the p_index'th information set
522531 GameInfoset GetInfoset (int p_index) const { return m_infosets.at (p_index - 1 ); }
523532 // / Returns the information sets for the player
524- Infosets GetInfosets () const { return Infosets (this , &m_infosets); }
533+ Infosets GetInfosets () const
534+ {
535+ return Infosets (std::const_pointer_cast<GamePlayerRep>(shared_from_this ()), &m_infosets);
536+ }
525537
526538 // / @name Strategies
527539 // @{
@@ -605,7 +617,7 @@ class GameRep : public BaseGameRep {
605617 template <class T > friend class TableMixedStrategyProfileRep ;
606618
607619protected:
608- std::vector<GamePlayerRep * > m_players;
620+ std::vector<std::shared_ptr< GamePlayerRep> > m_players;
609621 std::vector<std::shared_ptr<GameOutcomeRep>> m_outcomes;
610622 std::string m_title, m_comment;
611623 unsigned int m_version{0 };
@@ -619,7 +631,7 @@ class GameRep : public BaseGameRep {
619631 // @}
620632
621633public:
622- using Players = ElementCollection <Game, GamePlayerRep>;
634+ using Players = SmartElementCollection <Game, GamePlayerRep>;
623635 using Outcomes = SmartElementCollection<Game, GameOutcomeRep>;
624636
625637 class Nodes {
@@ -834,17 +846,19 @@ class GameRep : public BaseGameRep {
834846 int NumStrategyContingencies () const
835847 {
836848 BuildComputedValues ();
837- return std::transform_reduce (m_players.begin (), m_players.end (), 0 , std::multiplies<>(),
838- [](const GamePlayerRep *p) { return p->m_strategies .size (); });
849+ return std::transform_reduce (
850+ m_players.begin (), m_players.end (), 0 , std::multiplies<>(),
851+ [](const std::shared_ptr<GamePlayerRep> p) { return p->m_strategies .size (); });
839852 }
840853 // / Returns the total number of actions in the game
841854 virtual int BehavProfileLength () const = 0;
842855 // / Returns the total number of strategies in the game
843856 int MixedProfileLength () const
844857 {
845858 BuildComputedValues ();
846- return std::transform_reduce (m_players.begin (), m_players.end (), 0 , std::plus<>(),
847- [](const GamePlayerRep *p) { return p->m_strategies .size (); });
859+ return std::transform_reduce (
860+ m_players.begin (), m_players.end (), 0 , std::plus<>(),
861+ [](const std::shared_ptr<GamePlayerRep> p) { return p->m_strategies .size (); });
848862 }
849863 // @}
850864
@@ -988,7 +1002,7 @@ inline GameStrategy GamePlayerRep::GetStrategy(int st) const
9881002inline GamePlayerRep::Strategies GamePlayerRep::GetStrategies () const
9891003{
9901004 m_game->BuildComputedValues ();
991- return Strategies (this , &m_strategies);
1005+ return Strategies (std::const_pointer_cast<GamePlayerRep>( shared_from_this ()) , &m_strategies);
9921006}
9931007
9941008inline Game GameNodeRep::GetGame () const { return m_game; }
0 commit comments