{"id":1151,"date":"2013-07-18T01:00:00","date_gmt":"2013-07-17T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1151"},"modified":"2013-07-20T12:34:15","modified_gmt":"2013-07-20T11:34:15","slug":"cloners","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1151","title":{"rendered":"Cloners"},"content":{"rendered":"<p>Let\u00e2\u20ac\u2122s say we are reading tagged blocks from within some sort of packet. The blocks being tagged means we won\u00e2\u20ac\u2122t care whether they\u00e2\u20ac\u2122re present, what order they come in, and can have each tag be a different length.<\/p>\n<pre><code>&lt;tag2&gt; &lt;tag2_data0&gt; &lt;tag2_data1&gt; ... &lt;tag2_data11&gt;\n&lt;tag1&gt; &lt;tag1_data0&gt; &lt;tag1_data1&gt; ... &lt;tag1_data8&gt;\n&lt;tag6&gt; &lt;tag6_data0&gt; &lt;tag6_data1&gt; ... &lt;tag6_data20&gt;\n&lt;tag3&gt; &lt;tag3_data0&gt; &lt;tag3_data1&gt; ... &lt;tag3_data6&gt;\n&lt;tag0&gt; &lt;tag0_data0&gt; &lt;tag0_data1&gt; ... &lt;tag0_data10&gt;\n&lt;tag5&gt; &lt;tag5_data0&gt; &lt;tag5_data1&gt; ... &lt;tag5_data7&gt;\n&lt;tag4&gt; &lt;tag4_data0&gt; &lt;tag4_data1&gt; ... &lt;tag4_data3&gt;<\/code><\/pre>\n<p>Each tag might represent data for a particular sensor, say.<\/p>\n<p>We want to write an object-oriented reader. Let\u00e2\u20ac\u2122s first consider a naive implementation:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    list&lt;TTag*&gt; TagList;\n    <span class=\"kw\">while<\/span>(stream) {\n        tag_code = stream.read(<span class=\"dv\">1<\/span>);\n        <span class=\"kw\">switch<\/span>( tag_code ) {\n            <span class=\"kw\">case<\/span> tag_0:\n                TagList.push_back(new TTag0);\n                <span class=\"kw\">break<\/span>;\n            <span class=\"kw\">case<\/span> tag_1:\n                TagList.push_back(new TTag1);\n                <span class=\"kw\">break<\/span>;\n            <span class=\"co\">\/\/ ... etc ...<\/span>\n            <span class=\"kw\">default<\/span>:\n                throw tag_read_error();\n                <span class=\"kw\">break<\/span>;\n        }\n        TagList.back().read(stream);\n    }<\/code><\/pre>\n<p>We\u00e2\u20ac\u2122ve made the tag objects derive from <code>TTag<\/code>, and each implements a virtual <code>read()<\/code> member that knows how to read itself from a stream. The tag will know its own length, so will leave the stream pointing at the next <code>tag_code<\/code>.<\/p>\n<p>It\u00e2\u20ac\u2122s a naive implementation because it\u00e2\u20ac\u2122s putting information about the tag, outside the <code>TTag<\/code> object \u00e2\u20ac\u201c i.e.\u00c2\u00a0the <code>tag_code<\/code>. We shouldn\u00e2\u20ac\u2122t break layering by having any knowledge of the code outside the TTag child. Let\u00e2\u20ac\u2122s make an improvement then:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    tag_code = stream.read(<span class=\"dv\">1<\/span>);\n    <span class=\"kw\">switch<\/span>( tag_code ) {\n        <span class=\"kw\">case<\/span> TTag0::tag_code:\n            TTagList.push_back(new TTag0);\n        <span class=\"kw\">case<\/span> TTag1::tag_code:\n            TTagList.push_back(new TTag1);\n        <span class=\"co\">\/\/ ... etc ...<\/span>\n    }<\/code><\/pre>\n<p>This is better, but not much. We\u00e2\u20ac\u2122ve made a little use of object orientation, but not enough. In fact, as a rule, any time you see a <code>switch()<\/code> in C++, you should ask whether or not you can push what its doing into an object.<\/p>\n<p>Here\u00e2\u20ac\u2122s a solution I end up using a lot. This code would go in some sort of factory class:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    list&lt;<span class=\"dt\">const<\/span> TTag*&gt; TagTemplates;\n    TagTemplate.push_back(new TTag0);\n    TagTemplate.push_back(new TTag2);\n    <span class=\"co\">\/\/ ... etc ...<\/span>\n    TagTemplate.push_back(new TTag6);\n\n    list&lt;TTag*&gt; TagList;\n    tag_code = stream.read(<span class=\"dv\">1<\/span>);\n    <span class=\"kw\">for<\/span>( <span class=\"dt\">auto<\/span> tag : TagTemplates ) {\n        <span class=\"kw\">if<\/span>( tag-&gt;isThisYourCode(tag_code) ) {\n            TagList.push_back(tag);\n            tag-&gt;read(stream);\n        }\n    }<\/code><\/pre>\n<p>Very nice. We\u00e2\u20ac\u2122ve replaced a hard-coded list, with an iteration. The template tags can be loaded elsewhere (probably the factory constructor) and could be different for different factory classes. The problem is that the above won\u00e2\u20ac\u2122t work. In particular note:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    TagList.push_back(tag);\n    tag-&gt;read(stream);<\/code><\/pre>\n<p>Here <code>tag<\/code> is a <code>const TTag*<\/code> from the <code>TagTemplate<\/code> list; and is being pushed onto a <code>TTag*<\/code> list. The compiler will block it. Worse, we\u00e2\u20ac\u2122re trying to read from the stream into our one-and-only template <code>TTag<\/code>. What we need instead is the ability to copy the tag template to give us a modifiable instance that is independent.<\/p>\n<p>That poses its own difficulty. What we have is a <code>const TTag*<\/code>, point at (say) a <code>TTag0<\/code>, we might wish we could do this:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    TTag *copyTag = new TTag;\n    *copyTag = *tag;<\/code><\/pre>\n<p>This absolutely won\u00e2\u20ac\u2122t work. We\u00e2\u20ac\u2122ve made a new <code>TTag<\/code>, when we wanted a <code>TTag0<\/code>. We probably won\u00e2\u20ac\u2122t even get as far as compilation because <code>TTag<\/code> is bound to be abstract, and so it\u00e2\u20ac\u2122s not possible to make a <code>new<\/code> one. Even if we could, <code>*copyTag = *tag<\/code> would use <code>TTag::operator=()<\/code> not <code>TTag0::operator=()<\/code>. In short: a mess.<\/p>\n<p>The solution in cases like this is to think about what it is you want: the ability to copy a derived class via a pointer with base-class type. Just like any other operation where we have a base-class pointer to access derived-class functionality, we use a virtual. This is often called the <a href=\"http:\/\/en.wikibooks.org\/wiki\/More_C%2B%2B_Idioms\/Virtual_Constructor\"><em>Virtual Constructor Idiom<\/em><\/a>.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">class TTag {\n  public:\n    virtual TTag *clone() <span class=\"dt\">const<\/span> = <span class=\"dv\">0<\/span>;\n};\n\nclass TTag0 {\n  public:\n    TTag0(<span class=\"dt\">const<\/span> TTag&amp;) { <span class=\"co\">\/* ... your implementation ... *\/<\/span> }\n    TTag0 *clone() <span class=\"dt\">const<\/span> override { <span class=\"kw\">return<\/span> new TTag0(*this); }\n};<\/code><\/pre>\n<p>Some points:<\/p>\n<ul>\n<li>We\u00e2\u20ac\u2122re using the derived class copy-constructor within the derived class. This is perfectly acceptable, it knows its own type.<\/li>\n<li>Note the change of return type. C++ lets you use the fact that a function signature doesn\u00e2\u20ac\u2122t include the return type to change the base class pointer type to a compatible derived-type return. <code>TTag0*<\/code> is a valid <code>TTag*<\/code>. This is a language feature called <em>co-variant return types<\/em>.<\/li>\n<\/ul>\n<p>Our factory code then becomes:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    list&lt;<span class=\"dt\">const<\/span> TTag*&gt; TagTemplates;\n    TagTemplate.push_back(new TTag0);\n    TagTemplate.push_back(new TTag2);\n    <span class=\"co\">\/\/ ... etc ...<\/span>\n    TagTemplate.push_back(new TTag6);\n\n    list&lt;TTag*&gt; TagList;\n    tag_code = stream.read(<span class=\"dv\">1<\/span>);\n    <span class=\"kw\">for<\/span>( <span class=\"dt\">auto<\/span> tag : TagTemplates ) {\n        <span class=\"kw\">if<\/span>( tag-&gt;isThisYourCode(tag_code) ) {\n            TTag *copyTag = tag-&gt;clone();\n            copyTag-&gt;read(stream);\n            TagList.push_back(copyTag);\n        }\n    }<\/code><\/pre>\n<p>This code will work. This is approximately the solution I\u00e2\u20ac\u2122ve been using for problems like this for a while now. You can see it in action for implementing handlers for multiple versions of the bitcoin protocol in my (incomplete) bitcoin client <a href=\"https:\/\/github.com\/andyparkins\/additup\/\">additup<\/a> on github.<\/p>\n<p>C++11 offers us some improvements. We should note that we have a classic instance of <a href=\"http:\/\/www.aristeia.com\/Papers\/resourceReturnProblem.txt\">the Resource Return Problem<\/a>. We are returning a resource and there is, as things are, no way to force the caller to return that resource when they are done with it. The solution is, of course, not to use naked pointers. We should return a <code>unique_ptr&lt;&gt;<\/code>. That unfortunately then denies us the use of a covariant return types, but that\u00e2\u20ac\u2122s only really important if we want to use one of our derived classes as another base class.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">class TTag {\n  public:\n    virtual unique_ptr&lt;TTag&gt; clone() <span class=\"dt\">const<\/span> = <span class=\"dv\">0<\/span>;\n};\n\nclass TTag0 {\n  public:\n    TTag0(<span class=\"dt\">const<\/span> TTag&amp;) { <span class=\"co\">\/* ... your implementation ... *\/<\/span> }\n    unique_ptr&lt;TTag&gt; clone() <span class=\"dt\">const<\/span> override {\n        <span class=\"kw\">return<\/span> unique_ptr&lt;TTag&gt;(new TTag0(*this));\n    }\n};<\/code><\/pre>\n<p>Note that this only works because C++11 offers us move semantics for <code>unique_ptr&lt;&gt;<\/code>, otherwise when it went out of scope the clone would be destroyed as soon as it is made.<\/p>\n<p>Let\u00e2\u20ac\u2122s see one final extension. Implementing the <code>clone()<\/code> method for every single class is a chore. We can make use of the <em><a href=\"http:\/\/en.wikipedia.org\/wiki\/Curiously_recurring_template_pattern\">Curiously Recurring Template Pattern<\/a> (CRTP)<\/em> to save ourselves that trouble.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">class TTag {\n  public:\n    virtual unique_ptr&lt;TTag&gt; clone() <span class=\"dt\">const<\/span> = <span class=\"dv\">0<\/span>;\n};\n\ntemplate &lt;typename TTag_t&gt;\nclass TTagClonable : public TTag {\n  public:\n    unique_ptr&lt;TTag&gt; clone() <span class=\"dt\">const<\/span> override {\n        <span class=\"kw\">return<\/span> unique_ptr&lt;TTag&gt;(\n            new TTag_t(\n                static_cast&lt;<span class=\"dt\">const<\/span> TTag_t&amp;&gt;(*this)\n                );\n    }\n};\n\nclass TTag0 : public TTagCloneable&lt;TTag0&gt; {\n  public:\n    TTag0(<span class=\"dt\">const<\/span> TTag&amp;) { <span class=\"co\">\/* ... your implementation ... *\/<\/span> }\n};<\/code><\/pre>\n<p>The CRTP is clever because rather than put a <code>clone()<\/code> in every derived class, we have the template system automatically create an intermediate class with a <code>clone()<\/code> that calls the derived class copy-constructor.<\/p>\n<pre><code>      TTag\n       ^\n       |\nTTagClonable&lt;TTag0&gt;\n       ^\n       |\n     TTag0<\/code><\/pre>\n<p>The <code>clone()<\/code> itself is a little messy because a cast is needed to force the compiler to pick the correct copy constructor; but the advantage is that we never need to write another clone() for any derived class.<\/p>\n<p>There is a disadvantage: it only works for one level of derivation; if you derive again from <code>TTag0<\/code> you\u00e2\u20ac\u2122ll have to make another CRTP-implementing class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u00e2\u20ac\u2122s say we are reading tagged blocks from within some sort of packet. The blocks being tagged means we won\u00e2\u20ac\u2122t care whether they\u00e2\u20ac\u2122re present, what order they come in, and can have each tag be a different length. &lt;tag2&gt; &lt;tag2_data0&gt; &lt;tag2_data1&gt; &#8230; &lt;tag2_data11&gt; &lt;tag1&gt; &lt;tag1_data0&gt; &lt;tag1_data1&gt; &#8230; &lt;tag1_data8&gt; &lt;tag6&gt; &lt;tag6_data0&gt; &lt;tag6_data1&gt; &#8230; &lt;tag6_data20&gt; &lt;tag3&gt; &lt;tag3_data0&gt;\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1151\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[66,82,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1151"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1151"}],"version-history":[{"count":4,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1151\/revisions"}],"predecessor-version":[{"id":1155,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1151\/revisions\/1155"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}