Results 1 to 5 of 5

Thread: boost::shared_ptr

  1. #1
    mwilson is offline Registered User
    Name: Mark Wilson
    Organization: University of Rochester
    Project: Omega EP laser
    Join Date
    Jul 2005
    Location
    Rochester, NY
    Posts
    100

    boost::shared_ptr

    I want to create a Boost function object by binding to an Ice proxy member function:


    boost::bind(&IceProxy::DVAC::DVACControl::setTrans Setpoint, thePrx, _1);


    Unfortunately, this doesn't work. Bind is looking for a get_pointer function to return an actual pointer for the binding. So, instead, I need to do this:

    boost::bind(&IceProxy::DVAC::DVACControl::setTrans Setpoint, thePrx.get(), _1);

    Correct me if I am wrong, but this means that the reference count for the proxy handle will not be incremented, which leaves the program open to a null pointer error.

    Any advice on what to do?
    Last edited by mwilson; 03-22-2010 at 09:31 AM. Reason: Remove smilies
    Mark E. Wilson
    Lead Programmer/Analyst
    Omega EP Project
    Laboratory for Laser Energetics (www.lle.rochester.edu)
    University of Rochester
    Rochester, NY 14623

  2. #2
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    Hi Mark,

    I am not sure what "SetPoint" represents in your expression.

    get() on a Proxy handle (Prx object) will indeed extract the underlying pointer without incrementing the reference count of the target proxy object. As long as you use this pointer while you hold the associated Prx object (and nobody changes this Prx), I don't see any risk.

    Cheers,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  3. #3
    mwilson is offline Registered User
    Name: Mark Wilson
    Organization: University of Rochester
    Project: Omega EP laser
    Join Date
    Jul 2005
    Location
    Rochester, NY
    Posts
    100
    Should have been "setTransSetpoint", which is the function on the proxy I want to bind to.
    Mark E. Wilson
    Lead Programmer/Analyst
    Omega EP Project
    Laboratory for Laser Energetics (www.lle.rochester.edu)
    University of Rochester
    Rochester, NY 14623

  4. #4
    mwilson is offline Registered User
    Name: Mark Wilson
    Organization: University of Rochester
    Project: Omega EP laser
    Join Date
    Jul 2005
    Location
    Rochester, NY
    Posts
    100
    Finally got back to this and figured it out; you need to define get_pointer for whatever smart pointer/handle is being used, in this case IceInternal::ProxyHandle, and it has to be in the proper namespace:

    Code:
    namespace IceProxy
    {
    namespace DVAC
    {
        template<class T> inline T* get_pointer(
            IceInternal::ProxyHandle<T> const & p)
        {
            return p.get();
        }
    }
    }
    Now I can create boost function objects via boost::bind that will maintain the reference count for an Ice proxy.

    So for an interface:

    Code:
    module A
    {
         interface B
         {
             void function1(int i);
         };
    };
    I can do

    Code:
    class Contrived
    {
    public:
        void setFunctionToCall(const boost::function<void(int)> & func)
        {
            func_ = func;
        }
    
        void doSomething(int i)
        {
             func_(i); // calls onto proxy.
        }
    private:
        boost::function<void(int)> func_;
    };
    
    .
    .
    .
    A::BPrx theProxy;
    
    boost::function<void(int)> f = boost::bind(&IceProxy::A::B::function, theProxy, _1);
    
    Contrived c;
    c.setFunctionToCall(f);
    
    c.doSomething(23);
    As long as the c object doesn't go out of scope, the proxy will not be deleted.
    Mark E. Wilson
    Lead Programmer/Analyst
    Omega EP Project
    Laboratory for Laser Energetics (www.lle.rochester.edu)
    University of Rochester
    Rochester, NY 14623

  5. #5
    mwilson is offline Registered User
    Name: Mark Wilson
    Organization: University of Rochester
    Project: Omega EP laser
    Join Date
    Jul 2005
    Location
    Rochester, NY
    Posts
    100
    In

    Code:
    boost::function<void(int)> f = boost::bind(&IceProxy::A::B::function, theProxy, _1);
    &IceProxy::A::B::function should be &IceProxy::A::B::function1
    Mark E. Wilson
    Lead Programmer/Analyst
    Omega EP Project
    Laboratory for Laser Energetics (www.lle.rochester.edu)
    University of Rochester
    Rochester, NY 14623

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Any plan to absorb the boost stuff
    By OrNot in forum Help Center
    Replies: 1
    Last Post: 07-21-2008, 11:11 AM
  2. Ice or Boost threads?
    By codeazure in forum Help Center
    Replies: 2
    Last Post: 04-11-2008, 12:48 PM
  3. Collaboration with Boost ?
    By e_quere in forum Comments
    Replies: 2
    Last Post: 04-27-2006, 04:16 PM
  4. boost::condiition lock Problem
    By ipek in forum Help Center
    Replies: 4
    Last Post: 03-10-2006, 02:31 PM

Posting Permissions

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