CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: method pointer

  1. #1
    Join Date
    Jan 2003
    Posts
    615

    method pointer

    The MessageManager class is a wrapper around a c library with all the fun that brings along. The class retrieves all Messages available in the system. Since the class loads a lot of resources it is only used once in the software instance (in one thread). This class has an concept of ownership where the MessageManager needs to release resources via the ReturnCollection method. This means the user of the class first uses GetCollection to get a collection of messages, process the messages and then call ReturnCollection.

    Code:
    class MessageManager
    {
      MessageManager();
      int Init(...);
      void Close();
      int GetCollection(DataCollection<Message>& collection);
      int ReturnCollection(DataCollection<Message>& collection);
    }
    The usage of the ReturnCollection() method is a cumbersome and potentially error prone approach which I would like to solve in a generic way. One approach that I thought about was using an Iterator class where the Iterator's destructor would call the ReturnCollection. How would I implement such a feature? Could I pass a method pointer containing the address of the ReturnCollection method to the iterator class and then enabling the Iterator destructor to call ReturnCollection() method via the method pointer? Is this possible and a good approach?
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: method pointer

    you could make GetCollection return a RAII handle taking care of resource ownership, something like

    Code:
    class MessageManager
    {
      class DataCollectionOwnership
      {
        explicit DataCollectionOwnership( MessageManager& manager, DataCollection<Message>& collection )
          : manager_,(manager), collection_(collection) {}
    
    	// make non copyable
    	// if you like, make movable
    
        ~DataCollectionOwnership() { manager_.ReturnCollection(collection_); }
    
      private:
        MessageManager& manager_;
        DataCollection<Message>& collection_;
      };
    
      MessageManager();
      int Init(...);
      void Close();
      DataCollectionOwnership GetCollection(DataCollection<Message>& collection);
    
    private:
      int ReturnCollection(DataCollection<Message>& collection);
    }
    otherwise, if you plan to change GetCollection to return a pair of iterators, you can use boost shared_container_iterator;

    this iterator stores the iterator of the underlying sequence and a shared_ptr to its container. Now, in order to achieve the wanted effect you simply have to replace the default deleter with a call to ReturnCollection, and that's it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured