{"id":886,"date":"2018-04-02T11:37:51","date_gmt":"2018-04-02T11:37:51","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=886"},"modified":"2023-05-31T15:35:33","modified_gmt":"2023-05-31T15:35:33","slug":"modern-cpp-design-learn-with-loki-library","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/modern-cpp-design-learn-with-loki-library\/","title":{"rendered":"Modern C++ Design: Learn with Loki Library"},"content":{"rendered":"<p>If you decide to start learning the modern C++ design and you come from the OOP school, you can start by looking inside the <a href=\"http:\/\/loki-lib.sourceforge.net\/\">loki\u00a0<\/a>library.<\/p>\n<p><b>Loki<\/b>\u00a0is the name of a\u00a0C++\u00a0software library\u00a0written by\u00a0Andrei Alexandrescu\u00a0as part of his book\u00a0<i><a title=\"Modern C++ Design\" href=\"https:\/\/en.wikipedia.org\/wiki\/Modern_C%2B%2B_Design\">Modern C++ Design<\/a><\/i>.<!--more--><\/p>\n<p>The library makes extensive use of C++\u00a0template metaprogramming\u00a0and implements several commonly used tools:\u00a0typelist,\u00a0functor,\u00a0singleton,\u00a0smart pointer,\u00a0object factory,\u00a0visitor\u00a0and\u00a0multimethods.<\/p>\n<p>And here&#8217;s the author&#8217;s motivation behind the design of this library<\/p>\n<ul>\n<li>Loki users only have to pay for the features they want. Each and every of Loki&#8217;s components can be used in isolation from the others.<\/li>\n<li>&#8220;Small is beautiful&#8221; &#8211; Loki&#8217;s internal dependencies are small.<\/li>\n<li>&#8220;Multiplicative is splendid.&#8221; &#8211; Loki&#8217;s particular focus is on obtaining multiplicative behaviors (i.e., fine-grained, specialized designs), by combining small, abstract bits of behavior (policies).<\/li>\n<li>Loki focus on strategy not tactics. Design, not bits. Architectural components, not the kitchen sink.<\/li>\n<li>As Loki is a library of designs and not the design of a library, it should make minimum assumptions about its environment and provide appropriate hooks wherever some default decision is chosen.<\/li>\n<li>Loki doesn&#8217;t cater for lesser compilers. Loki aims at writing readable code within the realm of standard C++.<\/li>\n<li>The components ideally are small, easy to understand and use correctly, useful either in isolation or together, and of high impact in projects.<\/li>\n<\/ul>\n<p>But before going deep inside its modern C++ idioms, it&#8217;s better to master a central piece of the template programming, it&#8217;s the policy based design idiom.<\/p>\n<p>The central idiom in policy-based design is a class template (called the host class), taking several type parameters as input, which are instantiated with types selected by the user (called policy classes), each implementing a particular implicit interface (called a policy), and encapsulating some orthogonal (or mostly orthogonal) aspect of the behavior of the instantiated host class. By supplying a host class combined with a set of different, canned implementations for each policy, a library or module can support an exponential number of different behavior combinations, resolved at compile time, and selected by mixing and matching the different supplied policy classes in the instantiation of the host class template.<\/p>\n<p>Here&#8217;s a mini sample from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Policy-based_design\">Wikipedia\u00a0<\/a>to demonstrate\u00a0the use of this useful idiom<\/p>\n<pre>#include \r\n#include \r\n \r\ntemplate \r\nclass HelloWorld : private OutputPolicy, private LanguagePolicy\r\n{\r\n    using OutputPolicy::print;\r\n    using LanguagePolicy::message;\r\n \r\npublic:\r\n    \/\/ Behaviour method\r\n    void run() const\r\n    {\r\n        \/\/ Two policy methods\r\n        print(message());\r\n    }\r\n};\r\n \r\nclass OutputPolicyWriteToCout\r\n{\r\nprotected:\r\n    template\r\n    void print(MessageType const &amp;message) const\r\n    {\r\n        std::cout &lt;&lt; message &lt;&lt; std::endl;\r\n    }\r\n};\r\n \r\nclass LanguagePolicyEnglish\r\n{\r\nprotected:\r\n    std::string message() const\r\n    {\r\n        return \"Hello, World!\";\r\n    }\r\n};\r\n \r\nclass LanguagePolicyGerman\r\n{\r\nprotected:\r\n    std::string message() const\r\n    {\r\n        return \"Hallo Welt!\";\r\n    }\r\n};\r\n \r\nint main()\r\n{\r\n    \/* Example 1 *\/\r\n    typedef HelloWorld&lt;OutputPolicyWriteToCout, LanguagePolicyEnglish&gt; HelloWorldEnglish;\r\n \r\n    HelloWorldEnglish hello_world;\r\n    hello_world.run(); \/\/ prints \"Hello, World!\"\r\n \r\n    \/* Example 2 \r\n     * Does the same, but uses another language policy *\/\r\n    typedef HelloWorld&lt;OutputPolicyWriteToCout, LanguagePolicyGerman&gt; HelloWorldGerman;\r\n \r\n    HelloWorldGerman hello_world2;\r\n    hello_world2.run(); \/\/ prints \"Hallo Welt!\"\r\n}\r\n<\/pre>\n<p>After discovering the policy based design you can move to the other idioms used by the library:<\/p>\n<div id=\"wikitext\">\n<div>\n<ul>\n<li>Multiple dispatcher<\/li>\n<li>Pimpl<\/li>\n<li>Printf\u00a0(a typesafe printf replacement)<\/li>\n<li>Ordered static object creation<\/li>\n<li>Scope guard pointer<\/li>\n<li>Small Object\u00a0Allocator<\/li>\n<li>Smart pointer<\/li>\n<li>Compile time check<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div id=\"footer\">\u00a0You can get a detailed\u00a0explanation of the below idioms from this interesting <a href=\"https:\/\/en.wikibooks.org\/wiki\/More_C%2B%2B_Idioms\">resource.<\/a><\/div>\n<div><\/div>\n<div>To resume the loki library is not popular ast\u00a0STL or Boost, but it&#8217;s an interesting educational library to learn the basic modern c++ design idioms. It&#8217;s a good start to master the modern C++ design.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you decide to start learning the modern C++ design and you come from the OOP school, you can start by looking inside the loki\u00a0library. Loki\u00a0is the name of a\u00a0C++\u00a0software library\u00a0written by\u00a0Andrei Alexandrescu\u00a0as part of his book\u00a0Modern C++ Design.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13,60],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cpp","tag-loki"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=886"}],"version-history":[{"count":12,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":1470,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/886\/revisions\/1470"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}