<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C++ Callback Comparisons</title>
	<atom:link href="http://codeplea.com/cpp-callback-comparisons/feed" rel="self" type="application/rss+xml" />
	<link>http://codeplea.com/cpp-callback-comparisons</link>
	<description>Random Thoughts on Programming</description>
	<lastBuildDate>Sun, 29 Apr 2012 10:12:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Michael</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-531</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Tue, 29 Jun 2010 15:20:54 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-531</guid>
		<description>Thank you Lewis.  I&#039;m checking out StackOverflow now.</description>
		<content:encoded><![CDATA[<p>Thank you Lewis.  I&#8217;m checking out StackOverflow now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lewis</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-530</link>
		<dc:creator>Lewis</dc:creator>
		<pubDate>Tue, 29 Jun 2010 14:53:57 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-530</guid>
		<description>Michael:

No, PlusCallback back won&#039;t help you. libusb is a C library, so it&#039;s using C callbacks.

The pointer typedef you have, is a pointer to a function returning void and taking one argument. The single argument is a pointer to a libusb_transfer struct. So just define a function following those guidelines. It must be a static or free function. Member functions aren&#039;t supported by C.

To pass the function&#039;s address around, generally you just use its name.

BTW, &lt;a href=&quot;http://stackoverflow.com/&quot; rel=&quot;nofollow&quot;&gt;StackOverflow&lt;/a&gt; is an excellent site for these types of questions. You would probably get this question answered there in about 90 seconds flat.</description>
		<content:encoded><![CDATA[<p>Michael:</p>
<p>No, PlusCallback back won&#8217;t help you. libusb is a C library, so it&#8217;s using C callbacks.</p>
<p>The pointer typedef you have, is a pointer to a function returning void and taking one argument. The single argument is a pointer to a libusb_transfer struct. So just define a function following those guidelines. It must be a static or free function. Member functions aren&#8217;t supported by C.</p>
<p>To pass the function&#8217;s address around, generally you just use its name.</p>
<p>BTW, <a href="http://stackoverflow.com/" rel="nofollow">StackOverflow</a> is an excellent site for these types of questions. You would probably get this question answered there in about 90 seconds flat.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-527</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Tue, 29 Jun 2010 03:45:47 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-527</guid>
		<description>I&#039;m going to venture out on a limb here and see if I can get a bit of help.  I am a rookie at this C++ language, and I have written a few straightforward programs to accomplish a few different tasks.  Most of them have used the libusb library to interface with various usb devices.  But all of them so far use the synchronous communication interface which does not use callbacks.

I now have a device which requires that I use the asynchronous interface.  Therefore I am trying (rather slowly) to understand templates, function pointers, etc. in c++.

To the point, tho:

The libusb library (written in c) requires me to create a structure and then set a member of this structure to a pointer to a function with the following definition:

typedef void(* libusb_transfer_cb_fn)(struct libusb_transfer  *transfer)

I have tried several different syntaxes, methods, etc. and I seem unable, with my limited knowledge, to make this work.  I have tried member functions as well as static functions, and I cannot seem to get the required function to execute when the appropriate time comes.

Would PlusCallback assist me in this, and if so, would you be willing to provide an example as to how?</description>
		<content:encoded><![CDATA[<p>I&#8217;m going to venture out on a limb here and see if I can get a bit of help.  I am a rookie at this C++ language, and I have written a few straightforward programs to accomplish a few different tasks.  Most of them have used the libusb library to interface with various usb devices.  But all of them so far use the synchronous communication interface which does not use callbacks.</p>
<p>I now have a device which requires that I use the asynchronous interface.  Therefore I am trying (rather slowly) to understand templates, function pointers, etc. in c++.</p>
<p>To the point, tho:</p>
<p>The libusb library (written in c) requires me to create a structure and then set a member of this structure to a pointer to a function with the following definition:</p>
<p>typedef void(* libusb_transfer_cb_fn)(struct libusb_transfer  *transfer)</p>
<p>I have tried several different syntaxes, methods, etc. and I seem unable, with my limited knowledge, to make this work.  I have tried member functions as well as static functions, and I cannot seem to get the required function to execute when the appropriate time comes.</p>
<p>Would PlusCallback assist me in this, and if so, would you be willing to provide an example as to how?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lewis</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-217</link>
		<dc:creator>Lewis</dc:creator>
		<pubDate>Thu, 11 Mar 2010 00:38:05 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-217</guid>
		<description>PlusCallback has been updated with an optimization, and there is a &lt;a href=&quot;http://codeplea.com/cpp-callback-benchmarks&quot; rel=&quot;nofollow&quot;&gt;benchmark here&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>PlusCallback has been updated with an optimization, and there is a <a href="http://codeplea.com/cpp-callback-benchmarks" rel="nofollow">benchmark here</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lewis</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-215</link>
		<dc:creator>Lewis</dc:creator>
		<pubDate>Tue, 09 Mar 2010 09:40:28 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-215</guid>
		<description>Ben, thank you very much for the bug report. This was just a rookie mistake on my part.

I added the fix and a test to detect the problem.</description>
		<content:encoded><![CDATA[<p>Ben, thank you very much for the bug report. This was just a rookie mistake on my part.</p>
<p>I added the fix and a test to detect the problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-214</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Tue, 09 Mar 2010 08:49:57 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-214</guid>
		<description>Hi,
looks nice, but actually your ref count code is broken. In  your  CallbackX::operator=:
&lt;pre lang=&quot;cpp&quot;&gt;
if (mCallback)
  mCallback-&gt;deinc();
mCallback = rhs.mCallback;
mCallback-&gt;inc();
&lt;/pre&gt;

Since you deincrement the refcount before incrementing, the code does not work in case of self-assignment:
&lt;pre lang=&quot;cpp&quot;&gt;
cb::Callback1 callback(&amp;func);
callback(4);
callback = callback;
callback(4); // SEGFAULT!
&lt;/pre&gt;
Once mCallback-&gt;deinc() is called, the ref counted object is destroyed leaving callback pointing to a dead object.

The correct sequence would be:
&lt;pre lang=&quot;cpp&quot;&gt;
rhs.mCallback-&gt;inc();
if (mCallback) mCallback-&gt;deinc();
mCallback = rhs.mCallback;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi,<br />
looks nice, but actually your ref count code is broken. In  your  CallbackX::operator=:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mCallback<span style="color: #008000;">&#41;</span>
  mCallback<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>deinc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
mCallback <span style="color: #000080;">=</span> rhs.<span style="color: #007788;">mCallback</span><span style="color: #008080;">;</span>
mCallback<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>inc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Since you deincrement the refcount before incrementing, the code does not work in case of self-assignment:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">cb<span style="color: #008080;">::</span><span style="color: #007788;">Callback1</span> callback<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>func<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
callback<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
callback <span style="color: #000080;">=</span> callback<span style="color: #008080;">;</span>
callback<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// SEGFAULT!</span></pre></div></div>

<p>Once mCallback-&gt;deinc() is called, the ref counted object is destroyed leaving callback pointing to a dead object.</p>
<p>The correct sequence would be:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">rhs.<span style="color: #007788;">mCallback</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>inc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>mCallback<span style="color: #008000;">&#41;</span> mCallback<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>deinc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
mCallback <span style="color: #000080;">=</span> rhs.<span style="color: #007788;">mCallback</span><span style="color: #008080;">;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Lewis</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-213</link>
		<dc:creator>Lewis</dc:creator>
		<pubDate>Mon, 08 Mar 2010 22:45:48 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-213</guid>
		<description>I&#039;ll do some tests later and update the post. It&#039;ll be interesting to know.</description>
		<content:encoded><![CDATA[<p>I&#8217;ll do some tests later and update the post. It&#8217;ll be interesting to know.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kevstev</title>
		<link>http://codeplea.com/cpp-callback-comparisons#comment-212</link>
		<dc:creator>kevstev</dc:creator>
		<pubDate>Mon, 08 Mar 2010 22:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://codeplea.com/?p=306#comment-212</guid>
		<description>how does the performance compare to boost::function? The syntax is certainly a lot nicer.</description>
		<content:encoded><![CDATA[<p>how does the performance compare to boost::function? The syntax is certainly a lot nicer.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

