<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daulet Nassipkali</title>
    <description>The latest articles on DEV Community by Daulet Nassipkali (@nassipkali).</description>
    <link>https://dev.to/nassipkali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1119477%2Fe29cb715-d87d-4833-9b59-343df9430ed6.png</url>
      <title>DEV Community: Daulet Nassipkali</title>
      <link>https://dev.to/nassipkali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nassipkali"/>
    <language>en</language>
    <item>
      <title>Harnessing Optional Polymorphism with the Polymorph Class in Platform::Interfaces Library</title>
      <dc:creator>Daulet Nassipkali</dc:creator>
      <pubDate>Fri, 14 Jul 2023 06:57:54 +0000</pubDate>
      <link>https://dev.to/nassipkali/harnessing-optional-polymorphism-with-the-polymorph-class-in-platforminterfaces-library-7kh</link>
      <guid>https://dev.to/nassipkali/harnessing-optional-polymorphism-with-the-polymorph-class-in-platforminterfaces-library-7kh</guid>
      <description>&lt;p&gt;In object-oriented programming, polymorphism is an essential principle that provides a means to treat objects of various types as objects of a common parent type. Typically, polymorphism comes in two flavors: static and dynamic, each with their own advantages and drawbacks. However, with the &lt;code&gt;Polymorph&lt;/code&gt; class in the Platform::Interfaces library, we have the possibility of optional polymorphism, a powerful design concept allowing a class to choose between static or dynamic polymorphism, as needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing the Platform::Interfaces Library
&lt;/h3&gt;

&lt;p&gt;To make use of the &lt;code&gt;Polymorph&lt;/code&gt; class and other features provided by the Platform::Interfaces library, you need to install the library in your local development environment. &lt;/p&gt;

&lt;p&gt;Platform::Interfaces is a C++ header-only library, which means that it's composed entirely of header files that are included in your C++ source files at compile time. &lt;/p&gt;

&lt;p&gt;To install the Platform::Interfaces library, we recommend using Conan, a popular open-source package manager for C++.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Conan for Installation
&lt;/h4&gt;

&lt;p&gt;Here are the steps to install the Platform::Interfaces library with Conan:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install Conan. If you have not installed Conan on your system, you can download it from the &lt;a href="https://conan.io/"&gt;official website&lt;/a&gt;. Make sure to follow the instructions suitable for your operating system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;conanfile.txt&lt;/code&gt; file in your project directory. This file specifies the dependencies of your project. For the Platform::Interfaces library, your &lt;code&gt;conanfile.txt&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   [requires]
   platform.interfaces/0.3.41
   [generators]
   CMakeDeps
   CMakeToolchain
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open a terminal in your project directory and run the following command to install the dependencies specified in &lt;code&gt;conanfile.txt&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   conan &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Integrating with CMake
&lt;/h4&gt;

&lt;p&gt;After installing the Platform::Interfaces library with Conan, you can use it in your project. The library integrates with CMake, a cross-platform tool that manages the build process in an operating system-independent manner.&lt;/p&gt;

&lt;p&gt;Here are the steps to use the Platform::Interfaces library in your CMake project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In your &lt;code&gt;CMakeLists.txt&lt;/code&gt; file, use the &lt;code&gt;find_package&lt;/code&gt; function to locate the Platform::Interfaces library:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;   &lt;span class="nb"&gt;find_package&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Platform.Interfaces REQUIRED&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Then, link the library to your executable using the &lt;code&gt;target_link_libraries&lt;/code&gt; function:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;   &lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Executable PRIVATE Platform.Interfaces::Platform.Interfaces&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now, in your C++ source files, you can include the header file of the Platform::Interfaces library as follows:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;   &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;Platform.Interfaces.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! You have successfully installed and set up the Platform::Interfaces library for your project. You can now leverage the power of optional polymorphism with the &lt;code&gt;Polymorph&lt;/code&gt; class and explore other advanced features provided by the library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optional Polymorphism: The Best of Both Worlds
&lt;/h3&gt;

&lt;p&gt;Optional polymorphism is a powerful design pattern that allows a class to selectively decide between static and dynamic polymorphism. This pattern is useful when you want the compile-time efficiency of static polymorphism while also requiring the flexibility of dynamic polymorphism, which allows for method resolution during runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unlocking Optional Polymorphism with Polymorph
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Polymorph&lt;/code&gt; template class, part of the Platform::Interfaces library, encapsulates optional polymorphism by combining the principles of the Curiously Recurring Template Pattern (CRTP) and traditional polymorphism.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;TSelf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;TBase&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Polymorph&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;TBase&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;protected:&lt;/span&gt;
  &lt;span class="n"&gt;THIS_REFERENCE_WRAPPER_METHODS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TSelf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Located in the "Platform.Interfaces.h" header file, &lt;code&gt;Polymorph&lt;/code&gt; enables you to specify a set of base classes for a derived class to inherit from. If a base class has virtual functions that aren't entirely overridden in the derived class, &lt;code&gt;Polymorph&lt;/code&gt; facilitates a combination of static and dynamic polymorphism.&lt;/p&gt;

&lt;h3&gt;
  
  
  Delving Deeper: Polymorph in Action
&lt;/h3&gt;

&lt;p&gt;When deriving from &lt;code&gt;Polymorph&lt;/code&gt;, the derived class acts as a base class, forwarding method calls to further derived classes using the &lt;code&gt;this-&amp;gt;object.method()&lt;/code&gt; convention.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BaseInterface1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Pure virtual function&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Derived&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Platform&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Interfaces&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Polymorph&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Derived&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BaseInterface1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Delegation to the next class in the inheritance chain&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;Derived&lt;/code&gt; acts as an intermediary base class, forwarding the method call &lt;code&gt;foo()&lt;/code&gt; to the next class in the inheritance hierarchy.&lt;/p&gt;

&lt;p&gt;Let's see how &lt;code&gt;Polymorph&lt;/code&gt; enables optional polymorphism through static and dynamic polymorphism. Consider a &lt;code&gt;FurtherDerived&lt;/code&gt; class that overrides &lt;code&gt;foo()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FurtherDerived&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Derived&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"FurtherDerived::foo() called.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Dynamic polymorphism&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this &lt;code&gt;FurtherDerived&lt;/code&gt; class, &lt;code&gt;foo()&lt;/code&gt; overrides the pure virtual function from &lt;code&gt;BaseInterface1&lt;/code&gt;, exemplifying dynamic polymorphism. &lt;/p&gt;

&lt;p&gt;Next, consider a static case where a method &lt;code&gt;bar()&lt;/code&gt; is introduced:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DerivedStatic&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Platform&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Interfaces&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Polymorph&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;DerivedStatic&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Delegation to the next class in the inheritance chain&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FurtherDerivedStatic&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DerivedStatic&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"FurtherDerivedStatic::bar() called.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Static polymorphism&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;DerivedStatic&lt;/code&gt; serves as the intermediary base class that calls the &lt;code&gt;bar()&lt;/code&gt; method of the next class in the inheritance chain. Since &lt;code&gt;bar()&lt;/code&gt; is a non-virtual function in &lt;code&gt;FurtherDerivedStatic&lt;/code&gt;, it will be resolved at compile time, demonstrating static polymorphism.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;By offering the power of optional polymorphism, the &lt;code&gt;Polymorph&lt;/code&gt; class in the Platform::Interfaces library empowers developers with the flexibility to determine the type of method resolution (compile-time or runtime) on a per-method basis.&lt;/p&gt;

&lt;p&gt;This potent capability, however, demands a solid understanding of advanced C++ concepts including templates, variadic templates, perfect forwarding, type traits, template metaprogramming, multiple inheritance, and polymorphism. As you master these advanced concepts, you'll be able to harness the full potential of &lt;code&gt;Polymorph&lt;/code&gt; and the entire Platform::Interfaces library.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
