Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:38:46+00:00 2026-06-18T03:38:46+00:00

I have an http server that has a request handler like that : bool

  • 0

I have an http server that has a request handler like that :

bool handleRequest(const RequestObject& request, ResponseRequest& response);

I’m trying to write a wrapper that would provide an API like that :

addRouteHandler(GET, "/foo/bar", handler);

With handler able to be either :

  1. a function : bool handleFooBarRequest(const RequestObject& request, ResponseRequest& response);
  2. a method on an existing object : FooResourceInstance + bool FooResource::handleFooBarRequest(const RequestObject& request, ResponseRequest& response);
  3. a static method of an object : static bool FooResource::handleFooBarRequest(const RequestObject& request, ResponseRequest& response);

I insist : C++03 (gcc 4.1.2) and no Boost (the reason is NOT the point)

So far the techniques I have found either use Boost, C++11 or third-party code (http://www.codeproject.com/Articles/136799/Lightweight-Generic-C-Callbacks-or-Yet-Another-Del)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:38:47+00:00Added an answer on June 18, 2026 at 3:38 am

    Use an interface and a derived template class. This technique is called “type erasure”.

    class CallbackBase
    {
    public:
        virtual ~CallbackBase() = 0;
        virtual bool exec(const RequestObject& request, ResponseRequest& response) = 0;
    };
    
    template <typename F>
    class CallbackImpl : public CallbackBase
    {
    public:
        CallbackImpl(F f) : f_(f) {}
        virtual bool exec(const RequestObject& request, ResponseRequest& response) {
            return f_(request, response);
        }
    private:
        F f_;
    };
    
    template <typename F>
    void CreateCallback(F f, std::auto_ptr<CallbackBase>& r) {
        r.reset(new CallbackImpl<F>(f));
    }
    
    // examples
    std::auto_ptr<CallbackBase> my_callback;
    
    CreateCallback(GlobalFooBarRequest, my_callback);
    CreateCallback(&FooResource::staticFooBarRequest, my_callback);
    // for member function, use std::mem_fun and std::bind1st
    ...
    
    my_callback->exec(request, response);
    

    You might want to use a shared_ptr or similar instead of auto_ptr, all depends on how you want to store these objects.

    Edit:
    you can write your own member function wrapper/closure/function object. Code would looks something like the following (I haven’t tried compiling it so there could be some errors):

    template <typename T>
    class RequestWrapper
    {
        typedef bool (T::*F)(const RequestObject&, ResponseRequest&);
        T* obj_;
        F f_;
    public:
        RequestWrapper(T* obj, F f)
        : obj_(obj)
        , f_(f)
        {}
    
        bool operator()(const RequestObject& request, ResponseRequest& response) const {
            return (obj_->*f_)(request, response);
        }
    };
    
    template <typename T, typename F>
    RequestWrapper<T> BindRequestMfn(T* obj, F mfn)
    {
        return RequestWrapper<T>(obj, mfn);
    }
    
    
    CreateCallback(BindRequestMfn(foo, &FooResource::fooBarRequest), my_callback);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website that has images with urls like this: http://mysite.com/image1.jpg My server
I have a small app that downloads some files from a remote (HTTP) server
Alright, I have a server that serves a motion-jpeg stream over http. What I
I have some pdf files located on a http server: Like: http://domain.com/files/file1.pdf http://domain.com/files/file1.pdf http://domain.com/files/file1.pdf
I have to map a REST Webservice URL like http://server:8080/application/service/customer/v1 to createCustomer method in
I have my Apache http server running on localhost:80 and restlet server on localhost:8182,
We have Tomcat application server set up at port 8080 and Apache Http Server
I have an excel workbook which needs to query data from an HTTP server.
I am using indy's http server for a project so i have a few
I am running a HTTP server on my development machine. I have a website,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.