jeudi 27 septembre 2018

Using function pointer forward declaration as lamba declaration

What's the closest I can get to something like this?

.h:
    typedef bool (*DoWorkFunc)(uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2);
    void DoWork(DoWorkFunc func);

.cpp:
    bool MyClass::MyFunc(uint32_t val) {
        DoWork(DoWorkFunc { return c2.mVal < c1.mVal + fParam1 + fParam2; } );

        auto f1 = DoWorkFunc { return (bParam ? dParam1 : c1.mVal) < mVal; };
        auto f2 = DoWorkFunc { return ((dParam1 & dParam2) == 0) == bParam; };

        auto f = val < 3 ? f1: f2;
        DoWork(f);
    }

This is the closest clean solution I've been able to get, which requires a mostly copy-pasted solution for the lambda forward declaration.

.h:
    typedef bool (*DoWorkFunc)(uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2);
    #define DoWorkLambda [](uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2) -> bool
    void DoWork(DoWorkFunc func);

.cpp:
    bool MyClass::MyFunc(uint32_t val) {
        DoWork(DoWorkLambda { return c2.mVal < c1.mVal + fParam1 + fParam2; } );

        auto f1 = DoWorkLambda { return (bParam ? dParam1 : c1.mVal) < mVal; };
        auto f2 = DoWorkLambda { return ((dParam1 & dParam2) == 0) == bParam; };

        auto f = val < 3 ? f1: f2;
        DoWork(f);
    }

This works alright for one-off solution, but ends up with a pretty messy header for multiple functions declarations

.h:

    typedef bool (*DoWorkFunc1)(uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2);
    #define DoWorkLambda1 [](uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2) -> bool
    void DoWork1(DoWorkFunc1 func);

    typedef bool (*DoWorkFunc2)(uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass3& c2);
    #define DoWorkLambda2 [](uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass3& c2) -> bool
    void DoWork2(DoWorkFunc2 func);

    ...

For reference, this is what the code looks like without the preprocessor:

.h:
    typedef bool (*DoWorkFunc)(uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2);
    void DoWork(DoWorkFunc func);

.cpp:
    bool MyClass::MyFunc(uint32_t val) {
        DoWork([](uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2) -> bool
            {
                return c2.mVal < c1.mVal + fParam1 + fParam2;
            }
        );

        auto f1 = [](uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2) -> bool
        {
            return (bParam ? dParam1 : c1.mVal) < mVal;
        };
        auto f2 = [](uint32_t dParam1, uint32_t dParam2, bool bParam, float fParam1, float fParam2, MyClass& c1, MyClass2& c2) -> bool
        {
            return ((dParam1 & dParam2) == 0) == bParam;
        };

        auto f = val < 3 ? f1: f2;
        DoWork(f);
    }

Aucun commentaire:

Enregistrer un commentaire