{"id":5497,"date":"2012-01-24T13:58:46","date_gmt":"2012-01-24T19:58:46","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=5497"},"modified":"2012-01-26T03:40:06","modified_gmt":"2012-01-26T09:40:06","slug":"firing-up-c-functors-beautiful-solution-to-the-problem","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/5497","title":{"rendered":"Firing Up C++ Functors &#8211; Beautiful Solution to the Problem"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2007\/10\/cplusplus.jpg\" alt=\"cplusplus.jpg\" title=\"cplusplus.jpg\" border=\"0\" width=\"150\" height=\"149\" style=\"float:right;\" \/><\/p>\n<p>This morning I've been battling a really nasty problem in my Greek Engine - at the start of the business day, I need to clear out the previous day's values for some daily summary values - like open\/high\/low. I do that fine in the instruments themselves, but there's an component in the engine that listens to <em>all<\/em> trades and creates this summary data. And it <strong>wasn't<\/strong> getting cleared out. So when I reloaded all the instruments at the beginning of the day, and flushed all the messages from the exchanges to them, these \"lingering\" values from the previous day were slipping in.<\/p>\n<p>Ideally, we'd just clear out these values, and I <em>could<\/em> just clear out the cache in the component. But then I'd loose the last valid trade as well - and that I <strong>don't<\/strong> want to loose. So I really to operate on all the messages in the component. This seemed to <em>scream<\/em> \"Iterator!\" to me, and I spent several hours on it. The problem is not with the basic iterator concept, it's that we're trying to iterate over a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trie\">trie<\/a>, and that's not really all that easy when your <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trie\">trie<\/a> is lockless, and it's possible for someone to change the data as soon as you're sure it's there.<\/p>\n<p>Yeah, it's nice to think of an iterator on the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trie\">trie<\/a>, but it's not really all that practical.<\/p>\n<p>Sure didn't stop me from wasting several hours <em>trying<\/em> to get it to work, though.<\/p>\n<p>What I really wanted was to be able to <em>pass in<\/em> some function to operate on all the messages in the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trie\">trie<\/a>, and then let the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trie\">trie<\/a> itself, handle all the thread-safety issues. This seemed like a <em>much better<\/em> idea, so I started looking into something I haven't used up till now - functors.<\/p>\n<p>The basic idea is just to have a simple class that has a few standard operators on it, and then use this base class to derive all the operations that you need. For example, in my case what I needed was to operate on Messages. Sure, I only needed to operate on <em>one kind<\/em> of Message - the Summary Message, but if I'm going to make this, I might as well make it capable of dealing with all the message functor usage I'm going to need.<\/p>\n<p>So I create the following header:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">namespace<\/span> msg\n  <span style=\"color: #008000;\">&#123;<\/span>\n  <span style=\"color: #0000ff;\">struct<\/span> MessageFunction\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #666666;\">\/\/ this is the overt method that can be called<\/span>\n    <span style=\"color: #0000ff;\">bool<\/span> doIt<span style=\"color: #008000;\">&#40;<\/span> Message <span style=\"color: #000040;\">*<\/span>aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #666666;\">\/\/ \u2026and this is the simple operator to make it look like a function<\/span>\n    <span style=\"color: #0000ff;\">bool<\/span> operator<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#40;<\/span> Message <span style=\"color: #000040;\">*<\/span>aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n&nbsp;\n    <span style=\"color: #666666;\">\/\/ these are the methods to override for the different message types<\/span>\n    <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> doHello<span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Hello<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> doGoodBye<span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">GoodBye<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> doQuote<span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Quote<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> doPrint<span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Print<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>      <span style=\"color: #666666;\">\/\/ end of namespace msg<\/span><\/pre>\n<p>and I implemented all the specific message type methods so that if you don't <em>need<\/em> to implement it, you don't have to, and it'll just be a no-op on the message:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">namespace<\/span> msg\n  <span style=\"color: #008000;\">&#123;<\/span>\n  <span style=\"color: #666666;\">\/\/ this is the overt method that can be called<\/span>\n  <span style=\"color: #0000ff;\">bool<\/span> MessageFunction<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">doIt<\/span><span style=\"color: #008000;\">&#40;<\/span> Message <span style=\"color: #000040;\">*<\/span>aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">bool<\/span>      error <span style=\"color: #000080;\">=<\/span> <span style=\"color: #0000ff;\">false<\/span><span style=\"color: #008080;\">;<\/span>\n&nbsp;\n    <span style=\"color: #0000ff;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>aMsg <span style=\"color: #000040;\">!<\/span><span style=\"color: #000080;\">=<\/span> <span style=\"color: #0000ff;\">NULL<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008000;\">&#123;<\/span>\n      <span style=\"color: #0000ff;\">switch<\/span> <span style=\"color: #008000;\">&#40;<\/span>aMsg<span style=\"color: #000040;\">-<\/span><span style=\"color: #000080;\">&gt;<\/span>getType<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008000;\">&#123;<\/span>\n        <span style=\"color: #0000ff;\">case<\/span> eHello<span style=\"color: #008080;\">:<\/span>\n          error <span style=\"color: #000080;\">=<\/span> <span style=\"color: #000040;\">!<\/span>doHello<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#40;<\/span>message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Hello<\/span> <span style=\"color: #000040;\">&amp;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">*<\/span>aMsg<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n          <span style=\"color: #0000ff;\">break<\/span><span style=\"color: #008080;\">;<\/span>\n        <span style=\"color: #0000ff;\">case<\/span> eGoodBye<span style=\"color: #008080;\">:<\/span>\n          error <span style=\"color: #000080;\">=<\/span> <span style=\"color: #000040;\">!<\/span>doGoodBye<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#40;<\/span>message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">GoodBye<\/span> <span style=\"color: #000040;\">&amp;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">*<\/span>aMsg<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n          <span style=\"color: #0000ff;\">break<\/span><span style=\"color: #008080;\">;<\/span>\n        <span style=\"color: #0000ff;\">case<\/span> eQuote<span style=\"color: #008080;\">:<\/span>\n          error <span style=\"color: #000080;\">=<\/span> <span style=\"color: #000040;\">!<\/span>doQuote<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#40;<\/span>message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Quote<\/span> <span style=\"color: #000040;\">&amp;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">*<\/span>aMsg<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n          <span style=\"color: #0000ff;\">break<\/span><span style=\"color: #008080;\">;<\/span>\n        <span style=\"color: #0000ff;\">case<\/span> ePrint<span style=\"color: #008080;\">:<\/span>\n          error <span style=\"color: #000080;\">=<\/span> <span style=\"color: #000040;\">!<\/span>doPrint<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#40;<\/span>message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Print<\/span> <span style=\"color: #000040;\">&amp;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">*<\/span>aMsg<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n          <span style=\"color: #0000ff;\">break<\/span><span style=\"color: #008080;\">;<\/span>\n        <span style=\"color: #0000ff;\">default<\/span><span style=\"color: #008080;\">:<\/span>\n          error <span style=\"color: #000080;\">=<\/span> <span style=\"color: #0000ff;\">true<\/span><span style=\"color: #008080;\">;<\/span>\n          <span style=\"color: #0000ff;\">break<\/span><span style=\"color: #008080;\">;<\/span>\n      <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #000040;\">!<\/span>error<span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n  <span style=\"color: #666666;\">\/\/ \u2026and this is the simple operator to make it look like a function<\/span>\n  <span style=\"color: #0000ff;\">bool<\/span> MessageFunction<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">operator<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#40;<\/span> Message <span style=\"color: #000040;\">*<\/span>aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> doIt<span style=\"color: #008000;\">&#40;<\/span>aMsg<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n  <span style=\"color: #666666;\">\/\/ these are the methods to override for the different message types<\/span>\n  <span style=\"color: #0000ff;\">bool<\/span> MessageFunction<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">doHello<\/span><span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Hello<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">true<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n  <span style=\"color: #0000ff;\">bool<\/span> MessageFunction<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">doGoodBye<\/span><span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">GoodBye<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">true<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n  <span style=\"color: #0000ff;\">bool<\/span> MessageFunction<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">doQuote<\/span><span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Quote<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">true<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n  <span style=\"color: #0000ff;\">bool<\/span> MessageFunction<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">doPrint<\/span><span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Print<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">true<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>      <span style=\"color: #666666;\">\/\/ end of namespace msg<\/span><\/pre>\n<p>Now in my code, I only needed to implement a subclass that did the one thing I needed it to do - reset the values. In this case, I'll use the Quote as an example:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">struct<\/span> QuoteClear <span style=\"color: #008080;\">:<\/span>\n    MessageFunction\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> doQuote<span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Quote<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><span style=\"color: #008080;\">;<\/span><\/pre>\n<p>implemented as:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">bool<\/span> QuoteClear<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">doQuote<\/span><span style=\"color: #008000;\">&#40;<\/span> message<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">Quote<\/span> <span style=\"color: #000040;\">&amp;<\/span> aMsg <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    aMsg.<span style=\"color: #007788;\">clear<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">true<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><\/pre>\n<p>Then all I needed to do was to add in an <tt>apply()<\/tt> method to the trie, and have it run through all the values and on each one that is non-NULL, call the functor. The signature for the <tt>apply()<\/tt> method is simple, and it makes it clear how to use it:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> apply<span style=\"color: #008000;\">&#40;<\/span> MessageFunction <span style=\"color: #000040;\">&amp;<\/span> aFunctor <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span><\/pre>\n<p>This was an <em>excellent<\/em> use of the idea, as it allowed me to pass in an arbitrarily complex function to be operated on a general Message, and the <tt>apply()<\/tt> method simply knows how to scan it's internal data structure, and call this application as needed. Very sweet.<\/p>\n<p>While it's not as generally useful as an iterator on a trie, it's perfectly suited to what I needed. Love the idea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This morning I&#8217;ve been battling a really nasty problem in my Greek Engine &#8211; at the start of the business day, I need to clear out the previous day&#8217;s values for some daily summary values &#8211; like open\/high\/low. I do that fine in the instruments themselves, but there&#8217;s an component in the engine that listens [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-5497","post","type-post","status-publish","format-standard","hentry","category-coding","category-cube-life"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=5497"}],"version-history":[{"count":4,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5497\/revisions"}],"predecessor-version":[{"id":5510,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5497\/revisions\/5510"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=5497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=5497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=5497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}