C++ “Dynamic” function pointers for C callback functions

前端 未结 2 1151
生来不讨喜
生来不讨喜 2021-01-18 21:44

I have an API for managing a camera configuration. There are 344 individual options to manage. When a certain value changes the API calls a callback function to notify the p

2条回答
  •  生来不讨喜
    2021-01-18 22:18

    You do not provide many details about the camera API or the semantics of the call; if it is publicly available software a link to the actual documentation would be helpful.

    In any case I found the question interesting and will show a solution below which uses the fact that we can generate different functions with templates, instead of coding them by hand.

    The code below assumes that the Option *ptr parameter in void RegisterCallback(Option *ptr, void (fn*)(void*)) is a pointer which you obtained from the camera; it serves as an indicator to the camera API for which option you want to register this callback. Conceivably the camera API has an enumeration with 344 members or such for this, which would replace the size_t template parameters and map key type below.

    A simple recursive template function is called to register a callback for each of the 344 options.

    #include 
    #include 
    #include 
    using namespace std;
    
    typedef void (*voidFnPtrT)(void *);
    
    ////////////////////////////////////////////////////////////
    // This part simulates the camera API.
    class Camera
    {   public:
        static constexpr size_t NUM_OPTS = 344;
        static constexpr size_t OPTSIZE = 100;
        struct Option
        { 
            unsigned char data[OPTSIZE];
        };
    
        Option &GetOption(size_t index) { return options[index]; }
    
        void RegisterCallback(Option *opt, voidFnPtrT callback) 
        { 
            callbacks[opt] = callback;
        }
    
        /// Set a new option value and call the callback,
        /// if set for that option.
        void SetOptionVal(size_t index, Option *newVal) 
        {
            assert(index < NUM_OPTS);
            options[index] = *newVal;
            OnOptionChange(index);
        }
        private:
        Option options[NUM_OPTS];
        map

    Sample session:

    $ g++ -Wall -o callbacks callbacks.cpp  && ./callbacks
    reacting to change in property 0
    reacting to change in property 2
    property callback for 343 not yet implemented
    

提交回复
热议问题