Faced with a challenge, the solution to which I can not resist the second day.
Below is a brief description of the task, the solution of which I ask to leave behind me .
Questions:
1. How to check the presence of the stored implementation of multimethods in this case?
2. Is it possible to solve this problem in my own way? If not, how to correctly organize the algorithm for solving this problem?
3. Is it correct to use map here?
4. How correctly to run through all the first values in map for comparison with std :: pair and call the file stored in the map via a pointer to it with parameters from this very std :: pair?
5. Please point out my mistakes and explain how to correctly
addImpl — добавляет реализацию мультиметода для двух типов, которые заданы через std::type_info. hasImpl — принимает два указателя и проверяет, есть ли реализация мультиметода для соответствующих типов. call — принимает два указателя и вызывает для них соответствующую реализацию. * The implementation of these methods should correctly handle the situation when the multimethod is commutative
// Base - базовый класс иерархии // Result - тип возвращаемого значения мультиметода // Commutative - флаг, который показывает, что // мультиметод коммутативный (т.е. f(x,y) = f(y,x)). template<class Base, class Result, bool Commutative> struct Multimethod2 { // устанавливает реализацию мультиметода // для типов t1 и t2 заданных через typeid // f - это функция или функциональный объект // принимающий два указателя на Base // и возвращающий значение типа Result void addImpl( ... t1, ... t2, ... f ) { } // проверяет, есть ли реализация мультиметода // для типов объектов a и b bool hasImpl(Base * a, Base * b) const { // возвращает true, если реализация есть // если операция коммутативная, то нужно // проверить есть ли реализация для b и а return ...; } // Применяет мультиметод к объектам // по указателям a и b Result call(Base * a, Base * b) const { // возвращает результат применения реализации // мультиметода к a и b return ...; } }; Further, my reasoning.
1. To store the implementation of the multimethod we will use the private field of this class. This will be a map with a type_index pair and a pointer to the function, which is implemented via std :: function
using FunFObj = std::function <Result(Base*, Base*)>; private: std::map< std::pair<std::type_index, std::type_index>, FunFObj > MultData; }; 2. Addition is realizable for two cases.
void addImpl(std::type_info const & t1, std::type_info const & t2, FunFObj f) { if (!Commutative) MultData.insert(std::make_pair<t1, t2>, f); if (Commutative) { MultData.insert(std::make_pair<t2, t1>, f); MultData.insert(std::make_pair<t1, t2>, f); } } 3. But I don’t know how to compare std :: pair from map with data in hasimpl and call.
type_infohas abeforemethod. Another thing is that more or less is not needed here. - AnTtype_info(a pair of them) in themap, and for him, just the same, this is necessary (at least by defaultlessif no other comparison function is specified). - Andrej Levkovitchbeforemethod, which orders alltype_info. Those. There are no difficulties with this - compare to health. - AnT