OSDN Git Service

[Refactor] #2745 BaseitemKeyのsvalをstd::optionalでカプセル化し、SV_ANYを使わなくて良いようにした
authorHourier <66951241+Hourier@users.noreply.github.com>
Fri, 4 Nov 2022 11:09:07 +0000 (20:09 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Fri, 4 Nov 2022 16:10:47 +0000 (01:10 +0900)
src/object/object-kind-hook.cpp
src/system/baseitem-info-definition.cpp
src/system/baseitem-info-definition.h

index cd59c97..cffc7de 100644 (file)
@@ -220,7 +220,13 @@ static const std::vector<const BaseItemInfo *> &get_sorted_baseitems()
 
 short lookup_kind(const BaseitemKey &key)
 {
-    return lookup_kind(key.tval(), key.sval());
+    const auto sval = key.sval();
+    if (!sval.has_value()) {
+        constexpr auto any = 255;
+        return lookup_kind(key.tval(), any);
+    }
+
+    return lookup_kind(key.tval(), sval.value());
 }
 
 /*!
@@ -239,7 +245,8 @@ KIND_OBJECT_IDX lookup_kind(ItemKindType tval, OBJECT_SUBTYPE_VALUE sval)
     k_obj.tval = tval;
     k_obj.sval = sval;
 
-    if (sval == SV_ANY) {
+    constexpr auto any = 255;
+    if (sval == any) {
         auto [begin, end] = std::equal_range(sorted_cache.begin(), sorted_cache.end(), &k_obj, comp_tval);
         if (auto candidates_num = std::distance(begin, end);
             candidates_num > 0) {
index 90fb59b..2a6e1cd 100644 (file)
@@ -10,7 +10,7 @@
 #include "system/baseitem-info-definition.h"
 #include "object/tval-types.h"
 
-BaseitemKey::BaseitemKey(ItemKindType type_value, int subtype_value)
+BaseitemKey::BaseitemKey(const ItemKindType type_value, const std::optional<int> &subtype_value)
     : type_value(type_value)
     , subtype_value(subtype_value)
 {
@@ -40,7 +40,7 @@ ItemKindType BaseitemKey::tval() const
     return this->type_value;
 }
 
-int BaseitemKey::sval() const
+std::optional<int> BaseitemKey::sval() const
 {
     return this->subtype_value;
 }
index ac5d206..bc69002 100644 (file)
@@ -11,7 +11,7 @@
 enum class ItemKindType : short;
 class BaseitemKey {
 public:
-    BaseitemKey(ItemKindType type_value, int subtype_value);
+    BaseitemKey(const ItemKindType type_value, const std::optional<int> &subtype_value = std::nullopt);
     bool operator==(const BaseitemKey &other) const;
     bool operator!=(const BaseitemKey &other) const
     {
@@ -35,11 +35,11 @@ public:
     }
 
     ItemKindType tval() const;
-    int sval() const;
+    std::optional<int> sval() const;
 
 private:
     ItemKindType type_value;
-    int subtype_value; // @todo optionalにする.
+    std::optional<int> subtype_value;
 };
 
 enum class ItemKindType : short;