-template<EndgameType>
-struct ScalingFunction : public EndgameScalingFunctionBase {
- typedef EndgameScalingFunctionBase Base;
- explicit ScalingFunction(Color c) : EndgameScalingFunctionBase(c) {}
- ScaleFactor apply(const Position&) const;
+
+/// The Endgames class stores the pointers to endgame evaluation and scaling
+/// base objects in two std::map. We use polymorphism to invoke the actual
+/// endgame function by calling its virtual operator().
+
+class Endgames {
+
+ template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
+ template<typename T> using Map = std::map<Key, Ptr<T>>;
+
+ template<typename T>
+ Map<T>& map() {
+ return std::get<std::is_same<T, ScaleFactor>::value>(maps);
+ }
+
+ template<EndgameCode E, typename T = eg_type<E>, typename P = Ptr<T>>
+ void add(const std::string& code) {
+
+ StateInfo st;
+ map<T>()[Position().set(code, WHITE, &st).material_key()] = P(new Endgame<E>(WHITE));
+ map<T>()[Position().set(code, BLACK, &st).material_key()] = P(new Endgame<E>(BLACK));
+ }
+
+ std::pair<Map<Value>, Map<ScaleFactor>> maps;
+
+public:
+ Endgames();
+
+ template<typename T>
+ EndgameBase<T>* probe(Key key) {
+ return map<T>().count(key) ? map<T>()[key].get() : nullptr;
+ }