{"id":5817,"date":"2012-05-11T15:05:06","date_gmt":"2012-05-11T20:05:06","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=5817"},"modified":"2012-05-11T15:18:19","modified_gmt":"2012-05-11T20:18:19","slug":"added-source-and-sink-template-classes-to-dkit","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/5817","title":{"rendered":"Added Source and Sink Template Classes to DKit"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2012\/03\/DKit.jpg\" alt=\"DKit Laboratory\" title=\"DKit.jpg\" border=\"0\" width=\"150\" height=\"98\" style=\"float:right;\" \/><\/p>\n<p>Today I wanted to add a little bit more to DKit, so I decided that the next best thing to add were the concepts of a <em>template<\/em> source and sink. When I built the <tt>MessageSource<\/tt> and <tt>MessageSink<\/tt> back at PEAK6, I did it in the context of a <tt>Message<\/tt> object. That was fine, because that's all we needed, but this time I wanted to make it a little better - no <strong><em>a lot<\/em><\/strong> better, so I made the source and sink template classes.<\/p>\n<p>This will allow me to use the pointers like I did before, but it will also make it very easy to use integers, or doubles - or anything. This will be a far better solution to the problem than a fixed class, or pointer to a class.<\/p>\n<p>The only real problem I ran into - if there was indeed <em>any<\/em> real problems, was the syntax for specifying the template class methods in the implementation file. The header was pretty clear and straight forward, but the implementation was a bit trickier.<\/p>\n<p>Thankfully, I was able to find some examples on the web, but the syntax was pretty bad. I mean <em>really<\/em> bad. For example, let's say I had the following template class:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">namespace<\/span> dkit <span style=\"color: #008000;\">&#123;<\/span>\n  <span style=\"color: #0000ff;\">template<\/span> <span style=\"color: #000080;\">&lt;<\/span><span style=\"color: #0000ff;\">class<\/span> T<span style=\"color: #000080;\">&gt;<\/span> <span style=\"color: #0000ff;\">class<\/span> source\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">public<\/span><span style=\"color: #008080;\">:<\/span>\n&nbsp;\n      <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">void<\/span> setName<span style=\"color: #008000;\">&#40;<\/span> <span style=\"color: #0000ff;\">const<\/span> std<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">string<\/span> <span style=\"color: #000040;\">&amp;<\/span> aName <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n      <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">const<\/span> std<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">string<\/span> <span style=\"color: #000040;\">&amp;<\/span> getName<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #0000ff;\">const<\/span><span style=\"color: #008080;\">;<\/span>\n      <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #0000ff;\">bool<\/span> addToListeners<span style=\"color: #008000;\">&#40;<\/span> sink<span style=\"color: #000080;\">&lt;<\/span>T<span style=\"color: #000080;\">&gt;<\/span> <span style=\"color: #000040;\">*<\/span>aSink <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n&nbsp;\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 dkit<\/span><\/pre>\n<p>then the implementation file would have to look something like this:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  <span style=\"color: #0000ff;\">namespace<\/span> dkit <span style=\"color: #008000;\">&#123;<\/span>\n&nbsp;\n  <span style=\"color: #0000ff;\">template<\/span> <span style=\"color: #000080;\">&lt;<\/span><span style=\"color: #0000ff;\">class<\/span> T<span style=\"color: #000080;\">&gt;<\/span> <span style=\"color: #0000ff;\">void<\/span> source<span style=\"color: #000080;\">&lt;<\/span>T<span style=\"color: #000080;\">&gt;<\/span><span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">setName<\/span><span style=\"color: #008000;\">&#40;<\/span> <span style=\"color: #0000ff;\">const<\/span> std<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">string<\/span> <span style=\"color: #000040;\">&amp;<\/span> aName <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    boost<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">detail<\/span><span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">spinlock<\/span><span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">scoped_lock<\/span>  lock<span style=\"color: #008000;\">&#40;<\/span>mMutex<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n    mName <span style=\"color: #000080;\">=<\/span> aName<span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n&nbsp;\n  <span style=\"color: #0000ff;\">template<\/span> <span style=\"color: #000080;\">&lt;<\/span><span style=\"color: #0000ff;\">class<\/span> T<span style=\"color: #000080;\">&gt;<\/span> <span style=\"color: #0000ff;\">const<\/span> std<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">string<\/span> <span style=\"color: #000040;\">&amp;<\/span> source<span style=\"color: #000080;\">&lt;<\/span>T<span style=\"color: #000080;\">&gt;<\/span><span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">getName<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #0000ff;\">const<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> mName<span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n&nbsp;\n  <span style=\"color: #0000ff;\">template<\/span> <span style=\"color: #000080;\">&lt;<\/span><span style=\"color: #0000ff;\">class<\/span> T<span style=\"color: #000080;\">&gt;<\/span> <span style=\"color: #0000ff;\">bool<\/span> source<span style=\"color: #000080;\">&lt;<\/span>T<span style=\"color: #000080;\">&gt;<\/span><span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">addToListeners<\/span><span style=\"color: #008000;\">&#40;<\/span> sink<span style=\"color: #000080;\">&lt;<\/span>T<span style=\"color: #000080;\">&gt;<\/span> <span style=\"color: #000040;\">*<\/span>aSink <span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">bool<\/span>       added <span style=\"color: #000080;\">=<\/span> <span style=\"color: #0000ff;\">false<\/span><span style=\"color: #008080;\">;<\/span>\n    <span style=\"color: #666666;\">\/\/ first, make sure there's something to do<\/span>\n    <span style=\"color: #0000ff;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>aSink <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: #666666;\">\/\/ next, see if we can add us as a source to him<\/span>\n      <span style=\"color: #0000ff;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>aSink<span style=\"color: #000040;\">-<\/span><span style=\"color: #000080;\">&gt;<\/span>addToSources<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #0000dd;\">this<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008000;\">&#123;<\/span>\n        <span style=\"color: #666666;\">\/\/ finally, make sure we can add him to us<\/span>\n        added <span style=\"color: #000080;\">=<\/span> addToSinks<span style=\"color: #008000;\">&#40;<\/span>aSink<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n      <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> added<span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n  <span style=\"color: #008000;\">&#125;<\/span>     <span style=\"color: #666666;\">\/\/ end of namespace dkit<\/span><\/pre>\n<p>Now I understand the need to clearly identify the class - hence the <tt>template &lt;class T&gt;<\/tt> on the front, but then it seems to really go overboard there. While I'm sure there's a wonderful reason for all this, it seems to have not undergone any simplification over the years. That's probably part of the problem that folks have with C++ - it's complicated looking. And if you don't get it <em>just right<\/em>, you're not going to even get it to compile.<\/p>\n<p>But for me, it's just what has to be done in order to have template programming. It's a pain, yes, but it's so incredibly worth it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I wanted to add a little bit more to DKit, so I decided that the next best thing to add were the concepts of a template source and sink. When I built the MessageSource and MessageSink back at PEAK6, I did it in the context of a Message object. That was fine, because that&#8217;s [&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,7,4],"tags":[],"class_list":["post-5817","post","type-post","status-publish","format-standard","hentry","category-coding","category-everything-else","category-open-source-software"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5817","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=5817"}],"version-history":[{"count":2,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5817\/revisions"}],"predecessor-version":[{"id":5819,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/5817\/revisions\/5819"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=5817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=5817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=5817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}