Boost logo

Boost :

From: Philippe Vaucher (philippe.vaucher_at_[hidden])
Date: 2006-11-27 11:56:26


FWIW, another interface could be a class like :

// untested
class scope_guard
{
   public:

     typedef boost::function<void()> func_type;

     explicit scope_guard()
     {
     }

     explicit scope_guard(const func_type& f)
     {
         add_function(f);
     }

     ~scope_guard()
     {
         for(int i = 0; i < m_functions.size(); ++i)
           m_functions[i]();
     }

     void add_function(const func_type& f)
     {
         m_functions.push_back(f);
     }

     // Could be used to cancel something
     void clear()
     {
        m_functions.clear();
     }

   private:

     std::vector<func_type> m_functions;

};

This, used along with boost::bind would make your example become smth like :

glob_t result_glob;
scope_guard guard(boost::bind(&globfree, &result_glob));
/* throw or return here */

And you could add more than one function call to do with the add_function()
member... of course it's more limited than what your interface offers, but I
think the idea is worth mentionning.

Credits goes to Eelis (irc) for the idea.

Philippe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk