<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2915984721603110573</atom:id><lastBuildDate>Sun, 08 Sep 2024 13:01:21 +0000</lastBuildDate><category>Chrome</category><category>V8</category><category>BSD</category><category>GPL</category><category>Google</category><category>JIT</category><category>JVM</category><category>Sun</category><category>c++</category><category>contract</category><category>defensive</category><category>first post</category><category>hello world</category><category>trick</category><title>SoftwareDesignStuff</title><description></description><link>http://softwaredesignstuff.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-4318821071255636049</guid><pubDate>Thu, 24 Sep 2009 20:45:00 +0000</pubDate><atom:updated>2009-09-24T13:56:52.347-07:00</atom:updated><title>Generic programming in C++: general stuff</title><description>Much has been said about C++ generic programming, some things good, some things not. In this post, which is the first in a series devoted to C++ generic programming, I would like to dig in the role templates play in the C++ programmer toolset.&lt;br /&gt;&lt;br /&gt;C++ is a multi-paradigm language, supporting  procedural, object oriented and generic programming, but most of the C++ projects I have ever seen use basically OOP, plus a bit of generic programming just enough to deal with STL and/or BOOST or to implement not more than containers of T &lt;a href=&#39;#EXPERIMENT&#39;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I see a big gap between C++ hardcode practitioners and the average C++ programmer concerning templates. While most C++ programmers can use runtime polymorphism with no problem, they are hardly able to code a simple container-of-T by themselves. On the other hand, C++ experts tend to evangelize the goods of template metaprogramming. Why is generic programming such an infrequent tool in the daily coding? What do C++ gurus see in the use of templates that other people &quot;miss&quot;?&lt;br /&gt;&lt;br /&gt;The main problems why -in my opinion- templates tend not to be used are:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Lack of knowledge on the templates mechanism and on its syntax&lt;/span&gt;, related to the fact that most mainstream languages do not have the capabilities for generic programming C++ does.&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;Inherent difficulty to get it right with templates: as everybody knows, errors in code that uses templates can be a nightmare to decypher, and &lt;span style=&quot;font-weight:bold;&quot;&gt;the more you dig into generic programming idioms, the longer the error messages you get&lt;/span&gt;.&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;They &lt;span style=&quot;font-weight:bold;&quot;&gt;break encapsulation&lt;/span&gt;: templatized code imposes implicit (sorry, no concepts in C++0x) constraints on the template parameters (e.g. if the templatized code uses the template parameter&#39;s copy constructor, every instantiation will require such constructor from the parameter) Thus code that uses the template, has to know its implementation to properly use it. Failure to provide template parameters that match those implicit expectations is the most frequent source of errors in code using templates.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;In the following posts, I will try to explore the very basic template syntax, template specialization common uses, usual generic programming idioms, and some metaprogramming spells...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name=&#39;EXPERIMENT&#39;&gt;1.&lt;/a&gt; The best test I came up with to challenge my opinion is to google code search the key terms for polymorphism and generic programming in C++...&lt;br /&gt;search: &lt;a href=&#39;http://www.google.com/codesearch?hl=en&amp;sa=N&amp;q=&quot;template+&amp;lt&quot;++lang:c%2B%2B&amp;ct=rr&amp;cs_r=lang:c%2B%2B&#39;&gt;&quot;template &amp;lt&quot; lang:c++&lt;/a&gt;  --&gt; 339000&lt;br /&gt;search: &lt;a href=&#39;http://www.google.com/codesearch?hl=en&amp;lr=&amp;q=&quot;virtual+&quot;+lang%3Ac%2B%2B&amp;sbtn=Search&#39;&gt;&quot;virtual &quot; lang:c++&lt;/a&gt; --&gt; 2130000 results&lt;br /&gt;If anybody thinks there is a better way to measure it, comment please!</description><link>http://softwaredesignstuff.blogspot.com/2009/09/much-has-been-said-about-c-generic.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-7603820911189204688</guid><pubDate>Sat, 28 Feb 2009 16:45:00 +0000</pubDate><atom:updated>2009-02-28T09:39:47.650-08:00</atom:updated><title>Managing object ownership in C++ with auto_ptr</title><description>I really like garbage collected languages, they make you feel safe, confident for not having to explicitly manage memory...but at some price: expressiveness on object ownership and lack of control of objects lifecycle; when an instance of a class is created, no one but the garbage collector is responsible for taking care of that object. &lt;br /&gt;&lt;br /&gt;We are fine as long as we are only dealing with memory, but lacking such ability to communicate responsibilities is a pain when resources (e.g. files, locks, connections to external systems) come to scene.&lt;br /&gt;&lt;br /&gt;C++ has mechanisms to express responsibilities a class has over other classes and to control when objects are destroyed. This first of a series of post in which I want to write about such mechanisms; today it&#39;s the turn for std::auto_ptr.&lt;br /&gt; &lt;br /&gt;Smart pointers are classes that mimic &quot;classic&quot; pointers by overloading operator* and operator-&gt;, but provide &quot;extra features&quot; (e.g. memory management, locking) to their pointees.&lt;br /&gt;&lt;br /&gt;std::auto_ptr is a smart pointer included in the Standard Template Library; its main features are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;it owns its pointee&lt;/b&gt;, which means that when an auto_ptr is destroyed, so is its pointee.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;it can transfer the ownership of its pointee to another auto_ptr&lt;/b&gt;.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;This is a simplified version of auto_ptr:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;namespace&lt;/b&gt;&lt;/font&gt; std {&lt;br /&gt;    &lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;template&lt;/b&gt;&lt;/font&gt; &amp;lt;&lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; X&amp;gt;&lt;br /&gt;    &lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; auto_ptr {&lt;br /&gt;    &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;        &lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;explicit&lt;/b&gt;&lt;/font&gt; auto_ptr(X* p =&lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;) : pointee_(p) { };&lt;br /&gt;        auto_ptr(auto_ptr&amp;amp; rhs) {&lt;br /&gt;            pointee_ = rhs.pointee_;&lt;br /&gt;            rhs.pointee_ = &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;;   &lt;font color=&quot;#009090&quot;&gt;&lt;b&gt;// relinquish ownership&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        ~auto_ptr() { &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;delete&lt;/b&gt;&lt;/font&gt; pointee_; }&lt;br /&gt;&lt;br /&gt;        X&amp;amp; &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;*() &lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/font&gt; { &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; *pointee_; }&lt;br /&gt;        X* &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;-&amp;gt;() &lt;font color=&quot;#00aa00&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/font&gt; { &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; pointee_; }&lt;br /&gt;    &lt;font color=&quot;#ffaa00&quot;&gt;&lt;b&gt;private&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;        X* pointee_;&lt;br /&gt;    };&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Notice how &lt;b&gt;pointee_&lt;/b&gt; is destroyed in the destructor of auto_ptr and how ownership of &lt;b&gt;pointee_&lt;/b&gt; is transfered in the copy constructor.&lt;br /&gt;&lt;br /&gt;auto_ptr&#39;s power arises when it is used with &lt;b&gt;value semantics&lt;/b&gt;, that is:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;as a local variable (stack allocated)&lt;/b&gt;: when the auto_ptr goes out of scope, it is destroyed and so its pointee is deleted, avoiding any explicit memory management on our side. This still holds true in presence of exceptions:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;void&lt;/b&gt;&lt;/font&gt; doStuff1 () {&lt;br /&gt;  MyClass *myClass (&lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/font&gt; MyClass);&lt;br /&gt;&lt;br /&gt;  &lt;font color=&quot;#009090&quot;&gt;&lt;b&gt;// some dangerous stuff here that likely&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color=&quot;#009090&quot;&gt;&lt;b&gt;//  throws an exception, leaking *myClass&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;  &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;delete&lt;/b&gt;&lt;/font&gt; myClass;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;void&lt;/b&gt;&lt;/font&gt; doStuff2 () {&lt;br /&gt;  auto_ptr&amp;lt;MyClass&amp;gt; myClass (&lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/font&gt; MyClass);&lt;br /&gt;&lt;br /&gt;  &lt;font color=&quot;#009090&quot;&gt;&lt;b&gt;// some dangeous stuff here that, even&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color=&quot;#009090&quot;&gt;&lt;b&gt;//   upon exceptions, does not cause leaks &lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;  &lt;font color=&quot;#009090&quot;&gt;&lt;b&gt;// no explicit delete needed&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is a form of the &lt;a href=&quot;http://en.wikipedia.org/wiki/Resource_acquisition_is_initialization&quot;&gt;RAII (Resource Acquisition Is Initialization)&lt;/a&gt; C++ idiom, which I plan to post about soon...&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;as a function parameter passed by value&lt;/b&gt;: when an invocation to that function occurs, and because of the copy of the auto_ptr performed to build the parameter, the caller relinquishes ownership on the auto_ptr in favor of the callee. The auto_ptr&#39;s pointee will be destroyed when the callee returns, unless it also relinquishes ownership within its body. This idiom is called the &lt;b&gt;&quot;auto_ptr sink&quot;&lt;/b&gt;.&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;as return value of a function&lt;/b&gt;: when a function returns an auto_ptr by value, it is relinquishing ownership on it in favor of the caller. This idiom is called the &lt;b&gt;&quot;auto_ptr source&quot;&lt;/b&gt;.&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;as a class member&lt;/b&gt;: this is often used to &lt;b&gt;tie the lifetime of the auto_ptr&#39;s pointee with that of the class instance the auto_ptr is member of&lt;/b&gt;; when that object is destroyed, so is the auto_ptr and so the pointee.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;These uses of auto_ptr enable the programmer to communicate how variables are meant to be used. Let&#39;s see some examples:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Object creation by factories: factories are meant to encapsulate object creation. They are often implemented this way:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; MyFactory {&lt;br /&gt;&lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;  MyObject* createMyObject();&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Seeing that code, you cannot be sure whether the caller is responsible or not for the destruction of the just created object. A possible solution would be to drop a comment depicting who the responsible one is, but instead let&#39;s use a smart pointer to replace the dumb one:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; MyFactory {&lt;br /&gt;&lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;  auto_ptr&amp;lt;MyObject&amp;gt; createMyObject();&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This way, using the &lt;b&gt;auto_ptr source&lt;/b&gt; idiom, the factory relinquishes ownership of the just created object in favour of the caller, so we are sure about who the one responsible for the object is just looking at the code.&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;Decorator pattern: with such design pattern, an object wraps another one to provide extra features; both them implement the same interface, so the wrapped version is used seamlessly as if it were not wrapped. In the usual implementation of this pattern, the decorator class receives a pointer to the wrapped one in its constructor, stores it as a member variable, and destroys it in its own destructor. As in the previous example, let&#39;s use a smart pointer instead of a dumb one to better communicate responsibilities on the wrapped object&#39;s lifetime.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; IStuff {&lt;br /&gt;  &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;    &lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;virtual&lt;/b&gt;&lt;/font&gt; std::string getStuff () = &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; Foo : &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt; IStuff {&lt;br /&gt;  &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;    Foo () { }&lt;br /&gt;    std::string getStuff () {&lt;br /&gt;      &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&amp;quot;foo&amp;quot;&lt;/b&gt;&lt;/font&gt;;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; Decorator : &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt; IStuff {&lt;br /&gt;  &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;    Decorator (std::auto_ptr&amp;lt;IStuff&amp;gt; decorated)&lt;br /&gt;       : decorated_ (decorated) { }&lt;br /&gt;    std::string getStuff () {&lt;br /&gt;      &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&amp;quot;decorated &amp;quot;&lt;/b&gt;&lt;/font&gt; + decorated_-&amp;gt;getStuff();&lt;br /&gt;    }&lt;br /&gt;  &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;private&lt;/b&gt;&lt;/font&gt;:&lt;br /&gt;    std::auto_ptr&amp;lt;IStuff&amp;gt; decorated_;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; main (&lt;font color=&quot;#009000&quot;&gt;&lt;b&gt;void&lt;/b&gt;&lt;/font&gt;) {&lt;br /&gt;  std::auto_ptr&amp;lt;IStuff&amp;gt; foo (&lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/font&gt; Foo);&lt;br /&gt;  std::auto_ptr&amp;lt;IStuff&amp;gt; decoratedFoo (&lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;new&lt;/b&gt;&lt;/font&gt; Decorator(foo));&lt;br /&gt;&lt;br /&gt;  std::cout &amp;lt;&amp;lt; decoratedFoo-&amp;gt;getStuff() &amp;lt;&amp;lt; std::endl;&lt;br /&gt;&lt;br /&gt;  &lt;font color=&quot;#aa9000&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note how the decorated object is passed inside an auto_ptr to Decorator&#39;s constructor, thus we are expressing Decorator is the one responsible for managing it. Also note how the auto_ptr is stored in a member variable, thus tying the lifetime of the decorated object with that of the decorator.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Some final thoughts...&lt;/b&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;A variation in how auto_ptr behaves, arises when it is declared const: a &lt;b&gt;const auto_ptr&lt;/b&gt; cannot transfer the ownership of its pointee. In this case, the pointee is tied to that very auto_ptr, no matter what happens. This is a somehow degenerated use of auto_ptr; for this, it is more appropriate to use boost&#39;s &lt;a href=&quot;http://www.boost.org/doc/libs/1_36_0/libs/smart_ptr/scoped_ptr.htm&quot;&gt;scoped_ptr&lt;/a&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Beware of auto_ptr when dealing with &lt;b&gt;STL containers&lt;/b&gt;, they are not auto_ptr friendly because they make internal copies of the data, sometimes maintaining more than one copy at the same time, which goes agains the auto_ptr allowed usage. So just avoid using lists (or vectors, or whatever STL container) of auto_ptr.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;I intentionally avoided mentioning a member function of auto_ptr called &quot;reset&quot; that enables reusing the auto_ptr for other pointee. I tend not to use it because I think it&#39;s quite misleading, as auto_ptr is often used to communicate durable ownership semantics.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Preparing for the future: unique_ptr&lt;/b&gt;&lt;br /&gt;Since its inclusion in the C++ standard, auto_ptr &lt;a href=&quot;http://www.aristeia.com/BookErrata/auto_ptr-update.html&quot;&gt;has hold much controversy&lt;/a&gt; due to both its semantics and its implementation. In the new C++ standard, known as &lt;b&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/C%2B%2B0x&quot;&gt;C++0x&lt;/a&gt;&lt;/b&gt;, auto_ptr is &lt;b&gt;DEPRECATED&lt;/b&gt;. It is to be replaced with class unique_ptr, &lt;a href=&quot;http://www.boost.org/doc/libs/1_37_0/doc/html/boost/interprocess/unique_ptr.html&quot;&gt;taken from boost&lt;/a&gt;, which basically work like auto_ptr but without its deficiencies. To do so, it makes use of &lt;b&gt;&lt;a href=&quot;http://www.artima.com/cppsource/rvalue.html&quot;&gt;rvalue references&lt;/a&gt;&lt;/b&gt;, one of the goodies C++0x will bring, which enable to express &quot;move semantics&quot;.</description><link>http://softwaredesignstuff.blogspot.com/2009/02/managing-object-ownership-in-c-with.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-8300544977840305005</guid><pubDate>Sat, 14 Feb 2009 11:55:00 +0000</pubDate><atom:updated>2009-02-15T23:48:52.781-08:00</atom:updated><title>Koenig lookup...wha?</title><description>This is the first piece of C++ code many programmers wrote:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;std::&lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(std::cout, &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&amp;quot;hello world!&amp;quot;&lt;/b&gt;&lt;/font&gt;).&lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(std::endl);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Don&#39;t believe me? Let&#39;s reorganize it a little bit:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;std::cout &amp;lt;&amp;lt; &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&amp;quot;Hello world!&amp;quot;&lt;/b&gt;&lt;/font&gt; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;More familiar now, isn&#39;t it?&lt;br /&gt;&lt;br /&gt;The dark feature that makes it possible for the latter to be equivalent to the former is a feature of C++ language called &quot;&lt;a href=&quot;http://www.acceleratedcpp.com/authors/koenig/&quot;&gt;Koenig&lt;/a&gt; lookup&quot; a.k.a. &quot;Argument Dependent name lookup&quot;.&lt;br /&gt;&lt;br /&gt;Let&#39;s unveil why without it you could not write that piece of code the easy way....&lt;br /&gt;&lt;br /&gt;This is the signature for operator&amp;lt;&amp;lt;:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#6060cc&quot;&gt;&lt;b&gt;// Extracted from libstdc++-v3, with minor modifications&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;namespace&lt;/b&gt;&lt;/font&gt; std {&lt;br /&gt;  &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;template&lt;/b&gt;&lt;/font&gt;&amp;lt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;typename&lt;/b&gt;&lt;/font&gt; _CharT, &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;typename&lt;/b&gt;&lt;/font&gt; _Traits&amp;&lt;br /&gt;      basic_ostream&amp;lt;_CharT, _Traits&amp;gt; &amp;amp;&lt;br /&gt;      &lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(basic_ostream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp; __out, &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/font&gt;  &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt;* __s);&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Better without the templates clutter...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;namespace&lt;/b&gt;&lt;/font&gt; std {&lt;br /&gt;  ostream&amp;amp; &lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(ostream&amp;amp;, &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt;*);&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You can see it&#39;s defined in namespace std. like the other stuff from the STL.&lt;br /&gt;&lt;br /&gt;When something is defined inside a namespace, we have to either use &quot;using&quot; directive or prefix it with that namespace and &quot;::&quot;, like with cout and endl in this piece of code...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;std::cout &amp;lt;&amp;lt; &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&amp;quot;Hello&amp;quot;&lt;/b&gt;&lt;/font&gt; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;But then why don&#39;t we need to use any prefix for operator&amp;lt;&amp;lt; ??? Because of Koenig lookup, which states that unqualified calls (i.e. not namespace-prefixed) to functions are also looked up in the namespaces associated to the types of the invocation arguments.&lt;br /&gt;&lt;br /&gt;This makes it possible for operators to be called using the classic infix syntax and to avoid annoying namespace prefixes all over the place, without the need of a &quot;using namespace&quot;.&lt;br /&gt;&lt;br /&gt;Koenig lookup relies on the &lt;a href=&quot;http://www.gotw.ca/publications/mill02.htm&quot;&gt;interface principle&lt;/a&gt;, which states that nonmember functions which mention a class and are supplied along with that very class (e.g. are declared in the same header file), are also part -logically speaking- of its interface.&lt;br /&gt;&lt;br /&gt;But &lt;a href=&quot;http://www.open-std.org/JTC1/sc22/wg21/docs/cwg_closed.html#102&quot;&gt;it can also lead to the opposite&lt;/a&gt;, forcing us to prefix a function call even when we already placed a &quot;using&quot; directive or directly being inside in the same namespace the function is declared in, due to the ambiguity it introduces (this is often referred to as &lt;a href=&quot;http://www.cantrip.org/cpp.html&quot;&gt;Myers&lt;/a&gt;&#39; example):&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;namespace&lt;/b&gt;&lt;/font&gt; A {&lt;br /&gt;  &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt; AClass { };&lt;br /&gt;  ostream&amp;amp; &lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(ostream&amp;amp;, &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/font&gt; AClass&amp;amp;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;namespace&lt;/b&gt;&lt;/font&gt; B {&lt;br /&gt;  ostream&amp;amp; &lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(ostream&amp;amp;, &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/font&gt; A::AClass&amp;amp;);&lt;br /&gt;  &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;void&lt;/b&gt;&lt;/font&gt; doStuff(A::AClass&amp;amp; a) {&lt;br /&gt;    cout &amp;lt;&amp;lt; a; &lt;font color=&quot;#6060cc&quot;&gt;&lt;b&gt;// ambiguous, operator&amp;lt;&amp;lt; from A or from B?&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;One last curious thing...do you know the type of std::end? it actually is a function template that receives an ostream as argument; it just appends an endline character to its input ostream and then flushes it:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#6060cc&quot;&gt;&lt;b&gt;// From libstdc++ v3.&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;template&lt;/b&gt;&lt;/font&gt; &amp;lt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;typename&lt;/b&gt;&lt;/font&gt; _CharT, &lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;typename&lt;/b&gt;&lt;/font&gt; _Traits&amp;gt;&lt;br /&gt;&lt;font color=&quot;#008000&quot;&gt;&lt;b&gt;inline&lt;/b&gt;&lt;/font&gt; basic_ostream &amp;lt;_CharT, _Traits&amp;gt;&amp;amp;&lt;br /&gt;endl(basic_ostream &amp;lt;_CharT, _Traits&amp;gt;&amp;amp; __os)&lt;br /&gt;{&lt;br /&gt;  &lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; flush(__os.put(__os.widen(&lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&#39;\n&#39;&lt;/b&gt;&lt;/font&gt;)));&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It is possible to append a function template to a series of invocations to &amp;lt;&amp;lt; because of class basic_ostream&#39;s member operator&amp;lt;&amp;lt;, defined as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color=&quot;#6060cc&quot;&gt;&lt;b&gt;// From libstdc++ v3.&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;__ostream_type&amp;amp;&lt;br /&gt;&lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;(__ostream_type&amp;amp; (*__pf)(__ostream_type&amp;amp;))&lt;br /&gt;{&lt;br /&gt;  &lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; __pf(*&lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To test it, just use std:endl as the function it actually is:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;std::endl( std::&lt;font color=&quot;#ff5000&quot;&gt;&lt;b&gt;operator&lt;/b&gt;&lt;/font&gt;&amp;lt;&amp;lt;( std::cout, &lt;font color=&quot;#ff40ff&quot;&gt;&lt;b&gt;&amp;quot;hello world!&amp;quot;&lt;/b&gt;&lt;/font&gt; ) );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;NOTE: for those historicians out there, just recall Koenig lookup was not there since the beginning; &lt;a href=&quot;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1995/N0777.pdf&quot;&gt;it replaced &quot;friend name injection&quot;&lt;/a&gt; which stated that when a class template is instantiated, the names of friend classes and functions are “injected” into the enclosing namespace. g++ still supports this feature with the -ffriend-injection flag.&lt;br /&gt;&lt;br /&gt;NOTE: cited pieces of the standard template library have been taken from &lt;a href=&quot;http://gcc.gnu.org/libstdc++&quot;&gt;GNU Standard C++ Library v3&lt;/a&gt;.</description><link>http://softwaredesignstuff.blogspot.com/2009/02/koenig-lookupwha.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-509659719840695214</guid><pubDate>Tue, 27 Jan 2009 05:28:00 +0000</pubDate><atom:updated>2009-01-28T23:49:30.829-08:00</atom:updated><title>operator-&gt;() ad infinitum</title><description>&lt;span style=&quot;font-weight: bold; color: rgb(0, 153, 0);&quot;&gt;Short version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In C++, if an implementation of operator-&gt;() returns something different than a raw pointer, operator-&gt;() will be invoked on that return value; this will happen again and again until at some point a raw pointer is returned.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 0, 0); font-weight: bold;&quot;&gt;Lil&#39; longer version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;One of the operators C++ allows to be overloaded is &quot;-&gt;&quot; . This enables syntactic constructions that mimic pointer usage, like smart pointers, which look like this...&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;template &amp;lt;typename T&gt;&lt;br /&gt;class MySmartPointer {&lt;br /&gt; public:&lt;br /&gt;   MySmartPointer(T* pointee) : pointee_(pointee) {}&lt;br /&gt;&lt;br /&gt;   T* operator-&gt;() { return pointee_; }&lt;br /&gt;&lt;br /&gt;   /* some magic here... */&lt;br /&gt;&lt;br /&gt; private:&lt;br /&gt;   T* pointee_;&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Thus, you can use MySmartPointer with the same syntax as a plain T* :&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;MyClass* x (new MyClass);&lt;br /&gt;x-&gt;doStuff();&lt;br /&gt;&lt;br /&gt;MySmartPointer &amp;lt;MyClass&gt; y (new MyClass);&lt;br /&gt;y-&gt;doStuff();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;However, if we declare operator-&gt;() to return something that is not a raw pointer, the compiler will generate code to invoke operator-&gt; on that object, and so on:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;struct Baz {&lt;br /&gt; void sayHello() { cout &lt;&lt; &quot;hello&quot; &lt;&lt;&gt;() { return baz_; }&lt;br /&gt;};&lt;br /&gt;struct Bar {&lt;br /&gt;  Baz* baz_;&lt;br /&gt;  Baz* operator-&gt;() { return baz_; }&lt;br /&gt;};&lt;br /&gt;struct Foo {&lt;br /&gt; Bar bar_;&lt;br /&gt; Bar operator-&gt;() { return bar_; }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;void doStuff () {&lt;br /&gt; Foo foo;&lt;br /&gt; foo-&gt;sayHello();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&#39;doStuff&#39; is equivalent to this:&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;void doStuff () {&lt;br /&gt; Foo foo;&lt;br /&gt; Bar bar (foo.operator-&gt;());&lt;br /&gt; Baz* baz = bar.operator-&gt;();&lt;br /&gt; baz-&gt;sayHello();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Bjarne_Stroustrup&quot;&gt;Bjarne Stroustrup&lt;/a&gt; used this language feature -plus a stack allocated auxiliary object- in a proposal for a &lt;a href=&quot;http://www.research.att.com/%7Ebs/wrapper.pdf&quot;&gt;C++ idiom for wrapping calls to member functions&lt;/a&gt;, enabling to perform stuff just before and just after any method call (i.e. intercept it). Let&#39;s see it in an example:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;template&amp;lt;typename T&gt;&lt;br /&gt;class LockSmartPtrAux {&lt;br /&gt; public:&lt;br /&gt;   LockSmartPtrAux(T* t, Lock&amp;amp; lock)&lt;br /&gt;         : t_(t), lock_(lock) {&lt;br /&gt;       lock_.lock();  /* lock at creation */&lt;br /&gt;   }&lt;br /&gt;   ~LockSmartPtrAux() {&lt;br /&gt;       lock_.unlock(); /* unlock at destruction */&lt;br /&gt;   }&lt;br /&gt;   T* operator-&gt;() { return t_; }&lt;br /&gt; private:&lt;br /&gt;   T* t_;&lt;br /&gt;   Lock&amp;amp; lock_;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;template&amp;lt;typename T&gt;&lt;br /&gt;class LockSmartPtr {&lt;br /&gt; public:&lt;br /&gt;  LockSmartPtr(T* t) : t_(t) {}&lt;br /&gt;  LockSmartPtrAux&amp;lt;T&gt; operator-&gt;() {&lt;br /&gt;    return LockSmartPtrAux&amp;lt;T&gt;(t_, lock_);&lt;br /&gt;  }&lt;br /&gt; private:&lt;br /&gt;   T* t_;&lt;br /&gt;   Lock lock_;&lt;br /&gt;};&lt;br /&gt;// LockSmartPtrAux should be declared inside LockSmartPtr to enhance symbol locality&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;With the class templates above, you can &quot;wrap&quot; any class so that every invocation to its member functions is guarded by a lock, thus achieving some degree of transparent thread safety:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;map&amp;lt;string, MyClass&gt; cache;&lt;br /&gt;/* fill cache with useful and hard to build stuff... */ &lt;br /&gt;LockSmartPtr&amp;lt;map&amp;lt;string,MyClass&gt; &gt; threadsafeCache(&amp;cache);&lt;br /&gt;doSomethingInAnotherThread1 (threadsafeCache);&lt;br /&gt;doSomethingInAnotherThread2 (threadsafeCache);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here follow some more details about the implementation...&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;To just perform some action (e.g. lock_.lock() ) BEFORE the &quot;intercepted&quot; call, you don&#39;t need an extra auxiliary object (e.g. LockSmartPtrAux), but only to invoke it before returning the pointee.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;However, to perform some action (e.g. lock_.unlock() ) just AFTER the intercepted call and before the return, you have to place that action in the destructor of an auxiliary object and use operator-&gt; trick.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;This technique can be used to implement a rudimentary form of &lt;a href=&quot;http://en.wikipedia.org/wiki/Aspect-oriented_programming&quot;&gt;Aspect Oriented Programming&lt;/a&gt; in C++ by expressing &lt;a href=&quot;http://en.wikipedia.org/wiki/Cross-cutting_concern&quot;&gt;cross-cutting concerns&lt;/a&gt; in these &quot;wrapper&quot; classes.&lt;br /&gt;&lt;br /&gt;That&#39;s all the C++ trickery for today, hope you liked it.&lt;br /&gt;&lt;br /&gt;NOTE: my implementation of LockSmartPtr assumes your compiler implements the name return value optimization (NRVO); if this does not hold true and you use non recursive locks, you will probably get a nasty deadlock due to temporary copies of LockSmartPtrAux locking an already locked mutex from the same thread. Check out &lt;a href=&quot;http://www.research.att.com/%7Ebs/wrapper.pdf&quot;&gt;Stroutstrup&#39;s paper&lt;/a&gt; to see how (managing lock ownership, forbidding assignment) he tackles this and other issues.</description><link>http://softwaredesignstuff.blogspot.com/2009/01/operator-ad-infinitum.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-3879822187421716248</guid><pubDate>Mon, 05 Jan 2009 01:15:00 +0000</pubDate><atom:updated>2009-01-11T12:47:55.487-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">c++</category><category domain="http://www.blogger.com/atom/ns#">Chrome</category><category domain="http://www.blogger.com/atom/ns#">trick</category><category domain="http://www.blogger.com/atom/ns#">V8</category><title>Embedding values in C++ pointers</title><description>&lt;span style=&quot;color: rgb(0, 153, 0); font-weight: bold;&quot;&gt;Short version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Read, compile and run the following piece of C++ source code:&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;class Smi {&lt;br /&gt;  public:&lt;br /&gt;    static Smi* fromInt(int value) {&lt;br /&gt;      return reinterpret_cast&amp;lt;Smi*&amp;gt;(value);&lt;br /&gt;    }&lt;br /&gt;    int value() {&lt;br /&gt;      return reinterpret_cast&amp;lt;int&amp;gt;(this);&lt;br /&gt;    }&lt;br /&gt;    void sayHello() {&lt;br /&gt;      std::cout &lt;&lt; &quot;Hello, my value is &quot;&lt;br /&gt;                &lt;&lt; this-&gt;value() &lt;&lt; &quot;.&quot;&lt;br /&gt;                &lt;&lt; std::endl;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main(void) {&lt;br /&gt;  Smi* five = Smi::fromInt(5);&lt;br /&gt;  Smi* seven = Smi::fromInt(7);&lt;br /&gt;  five-&gt;sayHello();&lt;br /&gt;  seven-&gt;sayHello();&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Notice the only state class &lt;span style=&quot;font-weight:bold;&quot;&gt;Smi&lt;/span&gt; has is the integer value &quot;embedded&quot; in &quot;this&quot; pointer.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(204, 0, 0); font-weight: bold;&quot;&gt;Lil&#39; longer version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;C++ allows you to do pure magic. From time to time I see a piece of C++ code that makes me think: &quot;Does this even compile?&quot;. A few days ago I discovered one of those &quot;gems&quot; within &lt;a href=&quot;http://code.google.com/p/v8/&quot;&gt;V8&lt;/a&gt; (google chrome&#39;s javascript engine) &lt;a href=&quot;http://code.google.com/p/v8/source/browse/branches/bleeding_edge#bleeding_edge/src&quot;&gt;source code&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Let&#39;s begin with a quiz: what do you think the following code could be for?&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt; reinterpret_cast&amp;lt;int&amp;gt;(this)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now with some more context...&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;int Smi::value() {&lt;br /&gt;  return reinterpret_cast&amp;lt;int&amp;gt;(this) &gt;&gt; kSmiTagSize;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Ummmm.....  :|&lt;br /&gt;Well, let&#39;s unveil the mystery...&lt;br /&gt;&lt;br /&gt;V8 models every representable entity available in javascript (&lt;a href=&quot;http://www.ecma-international.org/publications/standards/Ecma-262.htm&quot;&gt;ECMAscript&lt;/a&gt;), all deriving from class &lt;span style=&quot;font-weight:bold;&quot;&gt;Object&lt;/span&gt;, as comments in file &lt;a href=&quot;http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/objects.h&quot;&gt;objects.h&lt;/a&gt; sugest:&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;//&lt;br /&gt;// All object types in the V8 JavaScript are described in this file.&lt;br /&gt;//&lt;br /&gt;// Inheritance hierarchy:&lt;br /&gt;//   - Object&lt;br /&gt;//     - Smi          (immediate small integer)&lt;br /&gt;//     - Failure      (immediate for marking failed operation)&lt;br /&gt;//     - HeapObject   (superclass for everything allocated in the heap)&lt;br /&gt;//       - JSObject&lt;br /&gt;//         - JSArray&lt;br /&gt;//         - JSRegExp&lt;br /&gt;//         - JSFunction&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Every instance of such entities is allocated and managed by class &lt;a href=&quot;http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/heap.h&quot;&gt;Heap&lt;/a&gt;,  V8&#39;s runtime memory manager. When Heap is asked to allocate an Object, it returns an Object*, but such pointer carries a hidden surprise, as comments in &lt;a href=&quot;http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/objects.h&quot;&gt;objects.h&lt;/a&gt; depict:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;// Formats of Object*:&lt;br /&gt;//  Smi:        [31 bit signed int] 0&lt;br /&gt;//  HeapObject: [32 bit direct pointer] (4 byte aligned) | 01&lt;br /&gt;//  Failure:    [30 bit signed int] 11&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Such comments state three things (apart from the obvious one: what Heap returns as pointers to Object are not such...):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;the least significant bits of the &quot;pointer&quot; carry a &quot;&lt;span style=&quot;font-weight:bold;&quot;&gt;tag&lt;/span&gt;&quot; to indicate the kind of Object.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;In the case of &lt;span style=&quot;font-weight:bold;&quot;&gt;Smi*&lt;/span&gt; and &lt;span style=&quot;font-weight:bold;&quot;&gt;Failure*&lt;/span&gt;, the bits remaining are not used to store any kind of pointer, but a numeric value (31 and 30 bits long respectively). This is the way to create an Smi*...&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;Smi* Smi::FromInt(int value) {&lt;br /&gt;  ASSERT(Smi::IsValid(value));&lt;br /&gt;  return reinterpret_cast&amp;lt;Smi*&amp;gt;((value &lt;&lt; kSmiTagSize) | kSmiTag);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;...and this is how to retrieve the value...&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;int Smi::value() {&lt;br /&gt;  return reinterpret_cast&amp;lt;int&amp;gt;(this) &gt;&gt; kSmiTagSize;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Thus they avoid the &quot;overhead&quot; of storing the pointer and the pointee, as both are the same.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;When the &quot;pointer&quot; &quot;points&quot; to a HeapObject instance, the 30 most significant bits carry an actual pointer to a HeapObject that is aligned to 4 bytes, thus the two other bits are always zero, space which is used for the tag. To illustrate this, the following piece of code is the one that, from a true object address, makes up the tagged pointer:&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;HeapObject* HeapObject::FromAddress(Address address) { &lt;br /&gt;  ASSERT_TAG_ALIGNED(address);&lt;br /&gt;  return reinterpret_cast&lt;HeapObject*&gt;(address + kHeapObjectTag);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;The trick works as long as you don&#39;t try to dereference one of those Object*...&lt;br /&gt;&lt;br /&gt;Like some stuff in V8 native code generators &lt;a href=&quot;http://softwaredesignstuff.blogspot.com/2008/11/trace-of-suns-jvm-in-v8.html&quot;&gt;I blogged about some time ago&lt;/a&gt;, this &quot;tagged&quot; &quot;pointer&quot; trick is not new but can also be found in &lt;a href=&quot;http://code.google.com/p/strongtalk/source/browse/trunk/vm/oops/oop.hpp&quot;&gt;StrongTalk&lt;/a&gt; and SelfVM (respectively Smalltalk an Self virtual machines that share creators with V8 :p)&lt;br /&gt;&lt;br /&gt;Hope you enjoyed this curious trick!</description><link>http://softwaredesignstuff.blogspot.com/2009/01/embedding-values-in-c-pointers.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-7194024079995513921</guid><pubDate>Sun, 04 Jan 2009 16:54:00 +0000</pubDate><atom:updated>2009-01-05T17:26:50.298-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">contract</category><category domain="http://www.blogger.com/atom/ns#">defensive</category><title>My bugs love defensive programming</title><description>&lt;span style=&quot;color: rgb(0, 153, 0); font-weight: bold;&quot;&gt;Short version:&lt;/span&gt;&lt;br /&gt;Some forms of defensive programming hide bugs, YOUR bugs.&lt;br /&gt;Other forms enforce design-by-contract style enhancing both quality and maintainability of the software.&lt;br /&gt;Choose wisely...&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(204, 0, 0); font-weight: bold;&quot;&gt;Lil&#39; longer version:&lt;/span&gt;&lt;br /&gt;Of course you know what &quot;defensive programming&quot; is, but perhaps you haven&#39;t heard it being called so until now; let&#39;s see it in code:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;public class NetworkFoo {&lt;br /&gt;/**&lt;br /&gt; * Performs connection.&lt;br /&gt; * @param host Machine where server is running. Must not be null.&lt;br /&gt; * @param port Port server is listening to. Must be in the range 1-65535.&lt;br /&gt; */&lt;br /&gt;public void connect (String host, int port) {&lt;br /&gt;  if (host == null) {&lt;br /&gt;    return;&lt;br /&gt;  }&lt;br /&gt;  if (port &amp;lt; 1 || port &amp;gt; 65535) {&lt;br /&gt;    return;&lt;br /&gt;  }&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Sure you have seen -or written- code like that, and that is just a form of &lt;a href=&quot;http://en.wikipedia.org/wiki/Defensive_programming&quot;&gt;defensive programming&lt;/a&gt;. The purpose of the techniques named under such term aim at making software robust against unexpected situations.&lt;br /&gt;&lt;br /&gt;That sounds quite reasonable, however, many times the programmer does not fully understand the implications of how she chose to address the checks she wanted to be performed. To understand that, let&#39;s see a variant of the code above:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;prettyprint&quot;&gt;&lt;br /&gt;public class NetworkFoo {&lt;br /&gt;/**&lt;br /&gt; * Performs connection.&lt;br /&gt; * @param host Machine where server is running. Must not be null.&lt;br /&gt; * @param port Port server is listening to. Must be in the range 1-65535.&lt;br /&gt; */&lt;br /&gt;public void connect (String host, int port) {&lt;br /&gt;  assert host != null;&lt;br /&gt;  assert (port &amp;gt; 0 &amp;amp;&amp;amp; port &amp;lt;= 65535);&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here we have replaced &#39;if&#39; blocks with &lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html&quot;&gt;assertions&lt;/a&gt;, so now instead of silently returning upon malformed parameters, the execution simply stops, as one of the preconditions is not matched.&lt;br /&gt;&lt;br /&gt;What is the difference in method &#39;connect&#39; of these two pieces of code? &lt;span style=&quot;color: rgb(200, 0, 0); font-weight: bold;&quot;&gt;its preconditions and postconditions are NOT the same in both cases&lt;/span&gt;:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;In the &lt;span style=&quot;font-weight: bold;&quot;&gt;later&lt;/span&gt;, preconditions are: &#39;host&#39; shall not be null and &#39;port&#39; shall be between 1 and 65535.&lt;/li&gt;&lt;li&gt;In the &lt;span style=&quot;font-weight: bold;&quot;&gt;former&lt;/span&gt;, although specified in the method header,  there are no preconditions regarding parameters, but instead there is an extra implicit postcondition which states that when &#39;host&#39; is null or &#39;port&#39; is not between 1 and 65535, the method simply does nothing.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Thus, both ways of implementing such &quot;guards&quot; have different impact in our code and so we should be careful when choosing which approach we use in each case.&lt;br /&gt;&lt;br /&gt;As stated at the beginning, defensive programming techniques try to shield code against unexpected [dangerous] situations that might break our logic; the most frequent checks are performed on method parameters or return values after invocations. When choosing how we protect the code, we have to take into account the origin of the &quot;potentially malicious&quot; data:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;if such data comes directly from user input (or any other untrusted external interface), sure our logic should take into account malformed data,&lt;/li&gt;&lt;li&gt;but if we do the same checks on data coming from another method within our own code, we are hiding the fact that we were passed unproper parameters by our very selves!! and thus hiding our own bugs.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;And what if we directly remove our &quot;silently return&quot; &#39;if&#39; blocks and just rely on the caller meeting the preconditions we specified in the method header? If the method is called from our own code, there should be no problem, as long as we always wrote bug-free code, which for sure is not the case, so the behaviour of our method would be undefined for malformed input data, which means a hypothetical invocation to our method with unproper parameters could lead or not to a noticeable effect.&lt;br /&gt;&lt;br /&gt;The following table summarizes the consequences of applying or not defensive programming to our methods:&lt;table collapse=&quot;collapse&quot; border=&quot;1&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;tr&gt;&lt;br /&gt;&lt;th rowspan=&quot;2&quot;&gt;&lt;br /&gt;&lt;/th&gt;&lt;th colspan=&quot;2&quot;&gt;Source of the &quot;potentially dangerous&quot; data&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;br /&gt;&lt;th&gt; External source&lt;br /&gt;&lt;/th&gt;&lt;th&gt; Our own code&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;br /&gt;&lt;th&gt; No defensive programming at all&lt;br /&gt;&lt;/th&gt;&lt;th&gt; &lt;span style=&quot;color: rgb(160, 0, 0);&quot;&gt;Fragile code against malformed input&lt;/span&gt;&lt;br /&gt;&lt;/th&gt;&lt;th&gt; &lt;span style=&quot;color: rgb(160, 160, 160);&quot;&gt;Undefined behaviour upon caller bugs&lt;/span&gt;&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;br /&gt;&lt;th&gt; Silently return&lt;br /&gt;&lt;/th&gt;&lt;th&gt; &lt;span style=&quot;color: rgb(0, 160, 0);&quot;&gt;Robust code against malformed input&lt;/span&gt;&lt;br /&gt;&lt;/th&gt;&lt;th&gt; &lt;span style=&quot;color: rgb(160, 0, 0);&quot;&gt;Hide our own bugs&lt;/span&gt;&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;br /&gt;&lt;th&gt; Assertions&lt;br /&gt;&lt;/th&gt;&lt;th&gt; &lt;span style=&quot;color: rgb(160, 0, 0);&quot;&gt;Our code breaks upon malformed input&lt;/span&gt;&lt;br /&gt;&lt;/th&gt;&lt;th&gt; &lt;span style=&quot;color: rgb(0, 160, 0);&quot;&gt;Design-by-contract style&lt;/span&gt;&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;[&lt;a href=&quot;http://letmegooglethatforyou.com/?q=design+by+contract&quot;&gt;Ask google about design-by-contract to know more about it.&lt;/a&gt;]&lt;br /&gt;&lt;br /&gt;One common thing is to disable assertions when a system goes into production, my opinion is that if it&#39;s tolerable for the system to crash (sporadically) then they should remain enabled, to detect bugs that escaped development and validation phases.&lt;br /&gt;&lt;br /&gt;In the development of mission critical systems, it is not uncommon to enforce the &#39;silently do nothing&#39; flavour of defensive programming in order to avoid [unexpected] crashes. I have no formed opinion about this topic...what is the most acceptable choice in these cases? I suppose a system crash is unacceptable, but is it to enable silent bugs?&lt;br /&gt;&lt;br /&gt;Summing up, we can say defensive programming is a useful tool for chasing software quality and maintainability, but we should be wise when choosing the way we &quot;protect&quot; our code to assure we achieve code robustness without hiding our own bugs.</description><link>http://softwaredesignstuff.blogspot.com/2009/01/my-bugs-love-defensive-programming.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-1063793623090778691</guid><pubDate>Mon, 01 Dec 2008 18:57:00 +0000</pubDate><atom:updated>2008-12-05T08:17:39.741-08:00</atom:updated><title>Implementation inheritance is like playing russian roulette</title><description>&lt;span style=&quot;color: rgb(0, 153, 0); font-weight: bold;&quot;&gt;Short version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Implementation inheritance breaks encapsulation and leads to the Fragile Base Class (FBC) problem.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 0, 0); font-weight: bold;&quot;&gt;Lil&#39; longer version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here is the list of buzzwords I&#39;m going to use along this post: implementation inheritance, method precondition, method postcondition, class invariant, encapsulation, Liskov substitution principle. Let us give some &quot;&lt;span style=&quot;font-weight: bold;&quot;&gt;loose definitions&lt;/span&gt;&quot; to them:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold; color: rgb(255, 0, 0);&quot;&gt;implementation inheritance&lt;/span&gt; is when you use class inheritance as a mechanism for reusing behaviour. This concept is opossed to &quot;interface inheritance&quot; by which a class just inherits &quot;the contract&quot; it has to comply with.&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;method preconditions&lt;/span&gt; are the set of conditions that the caller of a method needs to assure before actually calling it (i.e. argument &#39;x&#39; is greater than 0, argument &#39;parent&#39; is not null, etc).&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;method postconditions&lt;/span&gt; are the result of a call to a method (either as a return value of as its effect over the state of the object it belongs to).&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;class invariant&lt;/span&gt; is the set of constraints that express what means for an object of such class to be in a consistent state (i.e. &#39;width&#39; atribute an instance of class &#39;Window&quot; must not be less than zero).&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold; color: rgb(0, 102, 0);&quot;&gt;encapsulation (a.k.a. Information hiding)&lt;/span&gt; is the principle by which in a class you &quot;hide&quot; anything (state/behaviour) that is not really needed to be known by other classes, thus reducing their dependence on the mentioned class to a bare minimum. This way, you reduce the probability of needing to change the former ones upon modification on the later.&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-weight: bold; color: rgb(0, 102, 0);&quot;&gt;Liskov Substituition principle (LSP) (a.k.a. Design by contract)&lt;/span&gt; states that is should be possible to treat an instance of a subclass as if it were a base class object, meaning a subclass must comply with everything that could be expected from its parent.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Now let&#39;s ask some &lt;span style=&quot;font-weight: bold; color: rgb(0, 0, 153);&quot;&gt;questions&lt;/span&gt; about these terms so we can get some ideas...&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;First question: How can the developer &quot;communicate&quot; the preconditions and postconditions of a method?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You can choose among some options to let the reader know the pre/postconditions of a method:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Using language facilities: type constraints (e.g. if a parameter must be between 0 and 65535 you can set it to be an unsigned short in C++), assertions, etc.&lt;/li&gt;&lt;li&gt;Using comments: method headers usually express in natural language what cannot be expressed using programming language syntax(e.g. /* this class is not thread safe */ ).&lt;/li&gt;&lt;li&gt;Not communicate them: either because you don&#39;t feel it necessary to communicate them or simply do not know about them (in this case pre/postconditions still exist, they just are implicit in the method body).&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Second question: What does LSP actually mean when it says a subclass must comply with everything that could be expected from its parent?&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;To call a method of an object, you first prepare its arguments as it expects them (preconditions) then perform the call and after that you expect some sort of result of the invocation (postconditions), either returned result or a variation in the object state. If such call were issued to a subclass, the mentioned preconditions and postconditions should still be perfectly valid.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Third question: So what is the relationship between the pre/postconditions and invariant of a class and its subclasses?&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Preconditions of a subclass should not be stronger than its parent&#39;s.&lt;/li&gt;&lt;li&gt;Postconditions of a subclass should not be weaker than its parent&#39;s.&lt;/li&gt;&lt;li&gt;Invariant of a subclass should not be weaker than its parent&#39;s.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Fourth question: But that means a subclass is also responsible of maintaining its parent&#39;s pre/postconditions and invariant, isn&#39;t it?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Yeah, right. That&#39;s precisely the point. This is the very reason why &lt;span style=&quot;font-weight: bold; color: rgb(255, 0, 0);&quot;&gt;implementation inheritance&lt;/span&gt; is said to &lt;span style=&quot;font-weight: bold; color: rgb(255, 0, 0);&quot;&gt;break encapsulation&lt;/span&gt;, as the subclass has to know details of the implementation of its base class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Fifth question: I guess such responsibility can be troublesome upon code changes, am I right?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You are quite right. Every change in the superclass&#39; pre/postconditions or invariant, either explicit (due to changes in method signatures) or implicit (due to changes in the code) forces to verify in each subclass&#39; pre/postconditions and invariant. To perform that verification task properly the coder needs to fully understand the intent of the code of both the base class and the derived one, which  is not likely to happen (even if he&#39;s the same person who wrote both classes); this is why  modifications to the base class often break subclasses. This is known as the &quot;&lt;span style=&quot;font-weight: bold; color: rgb(255, 0, 0);&quot;&gt;Fragile Base Class problem&lt;/span&gt;&quot;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Sixth question: How does this stuff apply to interface inheritance? &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;With interface inheritance you also have to comply with pre/postconditions of the interface you implement, however in this case there are no implicit conditions within code (as there is no code at all), everything you have to comply with is either expressed with language constructs or as comments in method headers.  Upon a change in methods signatures, implementors have also to be changed (otherwise they won&#39;t compile).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Seventh question: Your points seem to rely on the correctness of LSP, is it kinda dogma or what?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In &lt;a href=&quot;http://www.objectmentor.com/resources/articles/lsp.pdf&quot;&gt;Uncle Bob&#39;s words&lt;/a&gt;: &quot;It is only when derived types are completely substitutable for their base types that functions which use those base types can be reused with impunity, and the derived types can be changed with impunity&quot;. Sure you can violate LSP, but not if you want to achieve the code reuse promise from object oriented programming.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Summing up....&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Implementation inheritance breaks encapsulation because subclasses have to know every (either explicit or implicit) precondition and postcondition from its parent -plus its invariant- to comply with LSP; this knowledge that must be present in the subclass is likely to be flawed due to unknown (but existing) pre/post conditions, plus the mentioned elements can vary during the lifecycle of the project (e.g. maintenance phase), thus leading to the Fragile Base Class problem, by which modifications to the base class break subclasses.</description><link>http://softwaredesignstuff.blogspot.com/2008/12/implementation-inheritance-is-like.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-9149512803457532349</guid><pubDate>Wed, 26 Nov 2008 23:39:00 +0000</pubDate><atom:updated>2009-01-10T11:14:59.312-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">BSD</category><category domain="http://www.blogger.com/atom/ns#">Chrome</category><category domain="http://www.blogger.com/atom/ns#">Google</category><category domain="http://www.blogger.com/atom/ns#">GPL</category><category domain="http://www.blogger.com/atom/ns#">JIT</category><category domain="http://www.blogger.com/atom/ns#">JVM</category><category domain="http://www.blogger.com/atom/ns#">Sun</category><category domain="http://www.blogger.com/atom/ns#">V8</category><title>Trace of Sun&#39;s JVM in V8</title><description>&lt;a style=&quot;&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://upload.wikimedia.org/wikipedia/commons/4/47/SherlockHolmes.jpg&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 125px; height: 161px;&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/4/47/SherlockHolmes.jpg&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold; color: rgb(0, 153, 0);&quot;&gt;Short version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I suspect a few files within V8 source code have Sun&#39;s JVM as their origin.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(204, 0, 0); font-weight: bold;&quot;&gt;Lil&#39; longer version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let me introduce our guests...&lt;br /&gt;&lt;br /&gt;...&lt;a href=&quot;http://code.google.com/p/v8/&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;V8&lt;/span&gt;&lt;/a&gt; is the high-performance &lt;span style=&quot;font-weight: bold;&quot;&gt;javascript&lt;/span&gt; engine &lt;span style=&quot;font-weight: bold;&quot;&gt;Google&lt;/span&gt; developed for their web browser &lt;a href=&quot;http://www.google.com/chrome&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Chrome&lt;/span&gt;&lt;/a&gt;. One of the reasons why &lt;a href=&quot;http://letmegooglethatforyou.com/?q=v8+javascript+performance&quot;&gt;V8 seems to outperform other engines&lt;/a&gt; is it compiles javascript to native machine code (currently just for x86 and ARM) instead of interpreting it. V8 is written in C++ and is distributed under &lt;a href=&quot;http://www.opensource.org/licenses/bsd-license.php&quot;&gt;BSD license&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;...&lt;span style=&quot;font-weight: bold;&quot;&gt;Sun&lt;/span&gt;&#39;s Java Virtual Machine, codenamed &lt;a href=&quot;http://java.sun.com/javase/technologies/hotspot/&quot;&gt;hotspot&lt;/a&gt;, which was released &lt;a href=&quot;http://www.sun.com/2006-1113/feature/story.jsp&quot;&gt;some time ago&lt;/a&gt; as part of &lt;a href=&quot;http://openjdk.java.net/&quot;&gt;OpenJDK&lt;/a&gt;, among other things, provides a Just-in-Time (JIT) java bytecode compiler which on the fly &quot;translates&quot; bytecode fragments into machine code. Hotspot is written in C++ and is distributed under &lt;a href=&quot;http://www.opensource.org/licenses/gpl-2.0.php&quot;&gt;GPL license&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;A couple of days ago I was tinkering with V8&#39;s code, concretely with the x86 machine code generation stuff, and I came across a surprising copyright note in file &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/assembler-ia32.h&quot;&gt;assembler-ia32.h&lt;/a&gt;:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;// Copyright (c) 1994-2006 Sun Microsystems Inc.&lt;br /&gt;&lt;span class=&quot;com&quot;&gt;// All Rights Reserved.&lt;/span&gt;&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Yes, it is Sun&#39;s copyright note! And it can be also found in other files: &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/assembler-ia32.cc&quot;&gt;assembler-ia32.cc&lt;/a&gt;, &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/assembler.cc&quot;&gt;assembler.cc&lt;/a&gt;, &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/assembler.h&quot;&gt;assembler.h&lt;/a&gt;, &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/assembler-arm.h&quot;&gt;assembler-arm.h&lt;/a&gt;, etc.&lt;br /&gt;&lt;br /&gt;After that copyright note, you can find the BSD license, and after, this comment:&lt;pre&gt;&lt;code&gt;&lt;br /&gt;// The original source code covered by the above license above has been&lt;br /&gt;&lt;span class=&quot;com&quot;&gt;// modified significantly by Google Inc.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;com&quot;&gt;// Copyright 2006-2008 the V8 project authors. All rights reserved.&lt;/span&gt;&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;I immediately googled around to find out something more on that, and I found &lt;a href=&quot;http://zenisland.blogspot.com/2008/09/google-chrome-v8-javascript-engine.html&quot;&gt;someone&lt;/a&gt; who already noticed it, although he didn&#39;t go any further from the discovery thing.&lt;br /&gt;&lt;br /&gt;Intrigued for it, I searched which piece of software from Sun Google took the original files from, and I reached to OpenJDK: if you carefully examine files &lt;a href=&quot;http://code.google.com/p/v8/source/browse/trunk/src/assembler-ia32.h&quot;&gt;assembler-ia32.h&lt;/a&gt; from V8 and &lt;a href=&quot;https://openjdk.dev.java.net/source/browse/openjdk/jdk/trunk/hotspot/src/cpu/x86/vm/assembler_x86_32.hpp?view=markup&quot;&gt;assembler_x86_32.hpp&lt;/a&gt; from OpenJDK you&#39;ll notice how similar they are.&lt;br /&gt;&lt;br /&gt;But I found it strange that V8&#39;s supposedly derived code, released under BSD license, had been initially taken from Sun&#39;s JVM which is distributed under GPL, as Google would be violating the license. Also, hotspot lacks ARM support while V8&#39;s ARM assembler files do have Sun&#39;s copyright note.&lt;br /&gt;&lt;br /&gt;After taking some &lt;a href=&quot;http://www.niallkennedy.com/blog/2008/09/google-chrome.html&quot;&gt;history lessons&lt;/a&gt; I found the actual nexus between V8 and Sun: &lt;a href=&quot;http://www.businessweek.com/globalbiz/content/nov2008/gb20081112_082312.htm&quot;&gt;Lars Bak&lt;/a&gt;, V8 tech leader, is a former employee of Sun where he worked in &lt;a href=&quot;http://strongtalk.org/&quot;&gt;StrongTalk&lt;/a&gt;, &lt;a href=&quot;http://research.sun.com/self/&quot;&gt;Self&lt;/a&gt;, &lt;a href=&quot;http://java.sun.com/products/hotspot&quot;&gt;Hotspot&lt;/a&gt; and in...&lt;br /&gt;&lt;br /&gt;...&lt;a href=&quot;http://java.sun.com/products/cldc/wp/ProjectMontyWhitePaper.pdf&quot;&gt;Monty VM&lt;/a&gt; which powers &lt;a href=&quot;http://java.sun.com/products/cldc/&quot;&gt;CLDC&lt;/a&gt;, (Sun&#39;s high performance &lt;a href=&quot;http://java.sun.com/javame&quot;&gt;Java ME &lt;/a&gt;implementation) and supports x86 and ARM architectures. It was open sourced as part of &lt;a href=&quot;https://phoneme.dev.java.net/&quot;&gt;phoneMe&lt;/a&gt; under GPL license.&lt;br /&gt;&lt;br /&gt;BINGO!&lt;br /&gt;&lt;br /&gt;Although I still have no answer for the licensing thing (perhaps Sun licensed some parts of MontyVM to Google under BSD?), I think the origin of V8 Sun-copyrighted files is &lt;a href=&quot;https://phoneme.dev.java.net/source/browse/phoneme/components/cldc/trunk/src/vm/cpu/i386/&quot;&gt;MontyVM JIT compiler&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Maybe I am right, maybe not, who cares, but this &quot;investigation&quot; has granted me interesting background on where the concepts implemented in V8 come from and hopefully will help me understand V8&#39;s design decissions as I digg deeper and deeper in its code  :-)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;UPDATE:&lt;/span&gt;&lt;br /&gt;I found a more feasible origin for Sun&#39;s copyrighted stuff: StrongTalk VM (open sourced by Sun, now hosted at google code); I think this is the actual origin because:&lt;br /&gt;&lt;ul&gt;&lt;il&gt;has the suspect &lt;a href=&quot;http://code.google.com/p/strongtalk/source/browse/trunk/vm/asm/assembler.hpp&quot;&gt;file&lt;/a&gt;.&lt;br /&gt;&lt;il&gt;Lars Bak was part of its development team.&lt;/il&gt;&lt;br /&gt;&lt;il&gt;it is distributed under BSD license.&lt;/il&gt;&lt;/ul&gt;</description><link>http://softwaredesignstuff.blogspot.com/2008/11/trace-of-suns-jvm-in-v8.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2915984721603110573.post-2145133241815996189</guid><pubDate>Wed, 26 Nov 2008 20:22:00 +0000</pubDate><atom:updated>2008-11-26T15:53:49.067-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">first post</category><category domain="http://www.blogger.com/atom/ns#">hello world</category><title>Ready, steady, go!</title><description>&lt;a style=&quot;&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixirtVWBxT8hahGH-FVaeKliS2cbgHsbR5Zq_-VX-JuvbfRrOlJbcfBiZjNN567W5U4QbDZP5C8iPwJ6aWuDB6NB33iTQSUbIVWH-E3FZXspa7ZWVIOqJWJg8GbFR0LpZeUKZQA9AxM88/s1600-h/me.jpg&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 113px; height: 152px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixirtVWBxT8hahGH-FVaeKliS2cbgHsbR5Zq_-VX-JuvbfRrOlJbcfBiZjNN567W5U4QbDZP5C8iPwJ6aWuDB6NB33iTQSUbIVWH-E3FZXspa7ZWVIOqJWJg8GbFR0LpZeUKZQA9AxM88/s200/me.jpg&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5273106081999863458&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 153, 0); font-weight: bold; font-family: lucida grande;font-family:lucida grande;&quot; &gt;Short version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: lucida grande;font-family:lucida grande;&quot; &gt;Welcome to my blog. I invite you to visit it to read and discuss about &lt;span style=&quot;font-weight: bold;&quot;&gt;software design&lt;/span&gt;, &lt;span style=&quot;font-weight: bold;&quot;&gt;good/bad &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: lucida grande;font-family:lucida grande;&quot; &gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;programming practices&lt;/span&gt;,&lt;span style=&quot;font-weight: bold;&quot;&gt; design patterns, programming languages, frameworks, software technology&lt;/span&gt; and stuff alike.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(204, 0, 0); font-weight: bold; font-family: lucida grande;font-family:lucida grande;&quot; &gt;Lil&#39; longer version:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: lucida grande;font-family:lucida grande;&quot; &gt;Welcome to my blog.&lt;br /&gt;&lt;br /&gt;I am a young software engineer who enjoys creating software, gathering knowledge about it, sharing it with people and discussing it to enrich my own vision of stuff.&lt;br /&gt;&lt;br /&gt;I&#39;d like this blog to be a place to share my ideas and opinions on topics like software design, &lt;/span&gt;&lt;span style=&quot;font-family: lucida grande;font-family:lucida grande;&quot; &gt;good/bad &lt;/span&gt;&lt;span style=&quot;font-family: lucida grande;font-family:lucida grande;&quot; &gt;programming practices, design patterns, programming languages, frameworks, software technology and so on, and to discuss them with you.&lt;br /&gt;&lt;br /&gt;I&#39;ll be glad of reading and answering any comments.&lt;br /&gt;&lt;br /&gt;P.S.: please excuse my sometimes-crappy English :-p&lt;/span&gt;&lt;span style=&quot;font-family:lucida grande;&quot;&gt;&lt;/span&gt;</description><link>http://softwaredesignstuff.blogspot.com/2008/11/ready-steady-go.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixirtVWBxT8hahGH-FVaeKliS2cbgHsbR5Zq_-VX-JuvbfRrOlJbcfBiZjNN567W5U4QbDZP5C8iPwJ6aWuDB6NB33iTQSUbIVWH-E3FZXspa7ZWVIOqJWJg8GbFR0LpZeUKZQA9AxM88/s72-c/me.jpg" height="72" width="72"/><thr:total>0</thr:total></item></channel></rss>