OSDN Git Service

Merge pull request #3532 from sikabane-works/release/3.0.0.87-alpha
[hengbandforosx/hengbandosx.git] / src / object / item-tester-hooker.h
1 #pragma once
2
3 #include "object/tval-types.h"
4
5 #include <functional>
6 #include <memory>
7
8 enum class StoreSaleType;
9 class ItemEntity;
10 class PlayerType;
11
12 /*!
13  * @brief アイテムの絞り込み条件をテストする基底クラス
14  */
15 class ItemTester {
16 public:
17     virtual ~ItemTester() = default;
18     bool okay(const ItemEntity *o_ptr) const;
19     virtual std::unique_ptr<ItemTester> clone() const = 0;
20
21 protected:
22     ItemTester() = default;
23
24 private:
25     virtual bool okay_impl(const ItemEntity *o_ptr) const = 0;
26 };
27
28 /**
29  * @brief ItemTesterの派生クラスのオブジェクトを複製するメンバ関数 clone() を実装するクラス
30  *
31  * @tparam T ItemTesterの派生クラスのオブジェクトの型
32  */
33 template <typename DerivedItemTester>
34 class CloneableItemTester : public ItemTester {
35 public:
36     virtual std::unique_ptr<ItemTester> clone() const final
37     {
38         return std::make_unique<DerivedItemTester>(static_cast<const DerivedItemTester &>(*this));
39     }
40 };
41
42 /*!
43  * @brief 全てのアイテムがOKとなるアイテムテストクラス
44  */
45 class AllMatchItemTester : public CloneableItemTester<AllMatchItemTester> {
46 public:
47     AllMatchItemTester() = default;
48
49 private:
50     virtual bool okay_impl(const ItemEntity *) const
51     {
52         return true;
53     }
54 };
55
56 /*!
57  * @brief 指定した tval のアイテムならばOKとなるアイテムテストクラス
58  */
59 class TvalItemTester : public CloneableItemTester<TvalItemTester> {
60 public:
61     explicit TvalItemTester(ItemKindType tval);
62
63 private:
64     virtual bool okay_impl(const ItemEntity *o_ptr) const;
65
66     ItemKindType tval;
67 };
68
69 /*!
70  * @brief 指定した関数を呼び出した結果戻り値が true であればOKとなるアイテムテストクラス
71  */
72 class FuncItemTester : public CloneableItemTester<FuncItemTester> {
73 public:
74     using TestMemberFunctionPtr = bool (ItemEntity::*)() const;
75     explicit FuncItemTester(TestMemberFunctionPtr test_func);
76     explicit FuncItemTester(std::function<bool(const ItemEntity *)> test_func);
77     explicit FuncItemTester(std::function<bool(PlayerType *, const ItemEntity *)> test_func, PlayerType *player_ptr);
78     explicit FuncItemTester(std::function<bool(PlayerType *, const ItemEntity *, StoreSaleType)> test_func, PlayerType *player_ptr, StoreSaleType store_num);
79
80 private:
81     virtual bool okay_impl(const ItemEntity *o_ptr) const;
82
83     std::function<bool(PlayerType *, const ItemEntity *)> test_func;
84     PlayerType *player_ptr = nullptr;
85 };