Urbi SDK Remote for C++  2.7.5
ucontext-impl.hxx
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00011 #include <libport/bind.hh>
00012 #include <libport/foreach.hh>
00013 
00014 namespace urbi
00015 {
00016   namespace impl
00017   {
00018 
00019     /*---------------.
00020     | UContextImpl.  |
00021     `---------------*/
00022 
00023     // Declared pure virtual, but needs an implementation.  Known
00024     // idiom.
00025     inline
00026     UContextImpl::~UContextImpl()
00027     {}
00028 
00029     inline
00030     void
00031     UContextImpl::send(const std::string& s)
00032     {
00033       send(s.c_str(), s.length());
00034     }
00035 
00036     /*--------------.
00037     | UObjectImpl.  |
00038     `--------------*/
00039 
00040     inline
00041     UObjectImpl::~UObjectImpl()
00042     {
00043     }
00044 
00045     /*-----------.
00046     | UVarImpl.  |
00047     `-----------*/
00048     inline
00049     UVarImpl::~UVarImpl()
00050     {
00051     }
00052 
00053     /*---------------.
00054     | UContextImpl.  |
00055     `---------------*/
00056 
00058     inline
00059     void
00060     UContextImpl::yield_for(libport::utime_t delay) const
00061     {
00062       return yield_until(libport::utime() + delay);
00063     }
00064 
00065 
00066     template <typename T> void
00067     deletor(T* ptr)
00068     {
00069       delete ptr;
00070     }
00071 
00072     template <typename T> void
00073     UContextImpl::addCleanup(T* ptr)
00074     {
00075       addCleanup(boost::bind(&deletor<T>, ptr));
00076     }
00077 
00078     inline void
00079     UContextImpl::addCleanup(boost::function0<void> op)
00080     {
00081       aver(cleanup_list_.get());
00082       aver(!cleanup_list_->empty());
00083       cleanup_list_->back().push_back(op);
00084     }
00085 
00086     inline void
00087     UContextImpl::pushCleanupStack()
00088     {
00089       CleanupList* cl = cleanup_list_.get();
00090       if (!cl)
00091       {
00092         cl = new CleanupList;
00093         cleanup_list_.reset(cl);
00094       }
00095       cl->resize(cl->size()+1);
00096     }
00097 
00098     inline void
00099     UContextImpl::popCleanupStack()
00100     {
00101       foreach (boost::function0<void>& f, cleanup_list_->back())
00102         f();
00103       cleanup_list_->pop_back();
00104     }
00105 
00106     inline
00107     UContextImpl::CleanupStack::CleanupStack(UContextImpl& owner)
00108       : owner_(owner)
00109     {
00110       owner_.pushCleanupStack();
00111     }
00112 
00113     inline
00114     UContextImpl::CleanupStack::~CleanupStack()
00115     {
00116       owner_.popCleanupStack();
00117     }
00118   }
00119 
00120 }