Generating unique keys to be used in e.g. an unordered_map for member functions

Just for fun, I’m trying to create a class, that allows “basic mocking functionality”.
More concretely, you can set return values for function calls.
That’s it ;D

I have a solution for that, however I don’t really like it.
I’ll show the code, give an explanation and tell what I don’t like about it.
I am using up to C++20.

#include <any>
#include <iostream>
#include <string_view>
#include <typeindex>
#include <unordered_map>

using namespace std;

class MockHelper {
public:
    virtual ~MockHelper() = default;

    template<typename Class, typename Ret, typename ...Args>
    void
    setFunctionReturnValue(const string_view &functionName, Ret(Class::*functionPtr)(Args...), const Ret &returnValue) {
        functionsReturnValues[functionName][type_index(typeid(functionPtr))] = make_any<Ret>(returnValue);
    }
    
protected:
    template<typename Class, typename Ret, typename ...Args>
    Ret handleCall(const string_view &functionName, Ret(Class::*functionPtr)(Args...)) {
        const auto &returnValues = functionsReturnValues[functionName];
        const auto typeIndex = type_index(typeid(functionPtr));
        if (!returnValues.contains(typeIndex))
            return {};
        return any_cast<Ret>(returnValues.at(typeIndex));
    }

private:
    unordered_map<string_view, unordered_map<type_index, any>> functionsReturnValues;
};

class SomethingToMock : public MockHelper {
public:
    int f() {
        return handleCall("f", &SomethingToMock::f);
    }

    int g() {
        // g is overloaded, thus we need to provide the template arguments explicitly
        return handleCall<SomethingToMock, int>("g", &SomethingToMock::g);
    }

    int g(bool) {
        return handleCall<SomethingToMock, int, bool>("g", &SomethingToMock::g);
    }
};

int main() {
    SomethingToMock beingMocked;

    // Setup expected return values
    beingMocked.setFunctionReturnValue("f", &SomethingToMock::f, 0);
    // again, g is overloaded, thus we need to provide the template arguments explicitly
    beingMocked.setFunctionReturnValue<SomethingToMock, int>("g", &SomethingToMock::g, 1);
    beingMocked.setFunctionReturnValue<SomethingToMock, int, bool>("g", &SomethingToMock::g, 2);

    // Show that this is actually working
    std::cout << "Should be 0 and is actually " << beingMocked.f() << std::endl;
    std::cout << "Should be 1 and is actually " << beingMocked.g() << std::endl;
    std::cout << "Should be 2 and is actually " << beingMocked.g(true) << std::endl;

    return 0;
}

class MockHelper allows to set return values for function calls (via setFunctionReturnValue), and allows to execute those function calls from derived classes (via handleCall).

class SomethingToMock derives from class MockHelper and contains 3 member functions which I use to show stuff.

In the main function we setup return values and show that it actually works.

A LIVE DEMO.

I dislike, that I have to give functions names as strings to all relevant functions. This is because if I wouldn’t I couldn’t distinguish the functions.
E.g.

type_index(typeid(&SomethingToMock::f)) == type_index(typeid(static_cast<int (SomethingToMock::*)()>(&SomethingToMock::g)))

evaluates to true, which means f() and g() cannot be distinguished by the signatures only, we have to include the name as well.

So, my question is, can we somehow generate unique keys for all the functions, so that they can be used in e.g. an unordered_map, without having to explicitly give the names of the functions?
So, as an example the main function should look more like this:

int main() {
    SomethingToMock beingMocked;

    // Setup expected return values
    beingMocked.setFunctionReturnValue(&SomethingToMock::f, 0);
    // again, g is overloaded, thus we need to provide the template arguments explicitly
    beingMocked.setFunctionReturnValue<SomethingToMock, int>(&SomethingToMock::g, 1);
    beingMocked.setFunctionReturnValue<SomethingToMock, int, bool>(&SomethingToMock::g, 2);

    // Show that this is actually working
    std::cout << "Should be 0 and is actually " << beingMocked.f() << std::endl;
    std::cout << "Should be 1 and is actually " << beingMocked.g() << std::endl;
    std::cout << "Should be 2 and is actually " << beingMocked.g(true) << std::endl;

    return 0;
}

New contributor

MockingMocker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật