Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're talking about causing a new method to be called before/after a function body, without changing the function body, you can base it on <a href="http://www.boost.org/doc/libs/release/libs/smart_ptr/sp_techniques.html#wrapper" rel="nofollow">this</a>, which uses a custom <code>shared_ptr</code> deleter to trigger the after-body function. It cannot be used for <code>try/catch</code>, since the before and after need to be separate functions using this technique.</p> <p>Also, the version below uses <code>shared_ptr</code>, but with C++11 you should be able to use <code>unique_ptr</code> to get the same effect without the cost of creating and destroying a shared pointer every time you use it.</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/chrono/chrono.hpp&gt; #include &lt;boost/chrono/system_clocks.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; template &lt;typename T, typename Derived&gt; class base_wrapper { protected: typedef T wrapped_type; Derived* self() { return static_cast&lt;Derived*&gt;(this); } wrapped_type* p; struct suffix_wrapper { Derived* d; suffix_wrapper(Derived* d): d(d) {}; void operator()(wrapped_type* p) { d-&gt;suffix(p); } }; public: explicit base_wrapper(wrapped_type* p) : p(p) {}; void prefix(wrapped_type* p) { // Default does nothing }; void suffix(wrapped_type* p) { // Default does nothing } boost::shared_ptr&lt;wrapped_type&gt; operator-&gt;() { self()-&gt;prefix(p); return boost::shared_ptr&lt;wrapped_type&gt;(p,suffix_wrapper(self())); } }; template&lt;typename T&gt; class timing_wrapper : public base_wrapper&lt; T, timing_wrapper&lt;T&gt; &gt; { typedef base_wrapper&lt; T, timing_wrapper&lt;T&gt; &gt; base; typedef boost::chrono::time_point&lt;boost::chrono::system_clock, boost::chrono::duration&lt;double&gt; &gt; time_point; time_point begin; public: timing_wrapper(T* p): base(p) {} void prefix(T* p) { begin = boost::chrono::system_clock::now(); } void suffix(T* p) { time_point end = boost::chrono::system_clock::now(); std::cout &lt;&lt; "Time: " &lt;&lt; (end-begin).count() &lt;&lt; std::endl; } }; template &lt;typename T&gt; class logging_wrapper : public base_wrapper&lt; T, logging_wrapper&lt;T&gt; &gt; { typedef base_wrapper&lt; T, logging_wrapper&lt;T&gt; &gt; base; public: logging_wrapper(T* p): base(p) {} void prefix(T* p) { std::cout &lt;&lt; "entering" &lt;&lt; std::endl; } void suffix(T* p) { std::cout &lt;&lt; "exiting" &lt;&lt; std::endl; } }; template &lt;template &lt;typename&gt; class wrapper, typename T&gt; wrapper&lt;T&gt; make_wrapper(T* p) { return wrapper&lt;T&gt;(p); } class X { public: void f() const { sleep(1); } void g() const { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; std::endl; } }; int main () { X x1; make_wrapper&lt;timing_wrapper&gt;(&amp;x1)-&gt;f(); make_wrapper&lt;logging_wrapper&gt;(&amp;x1)-&gt;g(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload