storing handlers (templated classes)

This is a discussion on storing handlers (templated classes) within the C++ Programming forums, part of the General Programming Boards category; Hello I'd like to store a templated class (handler) in a container.. The simplified design of handler class: Code: class ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    storing handlers (templated classes)

    Hello

    I'd like to store a templated class (handler) in a container..

    The simplified design of handler class:

    Code:
    class handler_base {
    public:
    	virtual ~handler_base() { }
    	virtual void callback(
    		) = 0;
    };
    
    template <typename T>
    class handler : public handler_base {
    public:
    	handler(T h) : m_handler(h) {
    	  }
    	~handler() { }
    
    	void callback() {
    		std::cout << "okay\n"; //here I call bind on m_handler
    	}
    private:
    	T m_handler;
    };
    Now the problem is I dont know what would be proper way to store more objects of handler class in a map container since you have to dynamically allocate memory to be able to store the function in a class.

    I've been thinking about having boost::map_ptr container or just ordinary map but with smart (shared_ptr) pointer..

    For instance:
    Code:
    std::map<int, boost::shared_ptr<handler_base> > somemap;
    somemap.insert(3, new boost::shared_ptr<handler_object>(arguments));
    What do you guys think would be best way? Maybe any other better implementation for that kind of design?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I've been thinking about having boost::map_ptr container or just ordinary map but with smart (shared_ptr) pointer..
    Both are possible, but the ptr_map provides the nicer interface and is faster. Assuming you don't actually need shared ownership, it's the better choice.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    How should I check with map_ptr if key exists?
    Is this the right way to do:
    Code:
    callback_map::iterator it = m_callbacks.find(key);
    if (! boost::is_null(it) ) {
    }
    or should I do:
    Code:
    if (it != m_callbacks.end())
    Thanks for help!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    http://www.boost.org/doc/libs/1_35_0...ics-algorithms

    The second one. Just like in a regular map, you need to make sure the key was found by checking the iterator against end().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing pointers to a templated class in a vector
    By rt454 in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2009, 02:04 AM
  2. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  3. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  4. Templated classes...
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2006, 04:31 PM
  5. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21