<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Serializing on Christian Gmeiner</title>
    <link>https://christian-gmeiner.info/tags/serializing/</link>
    <description>Recent content in Serializing on Christian Gmeiner</description>
    <image>
      <title>Christian Gmeiner</title>
      <url>https://christian-gmeiner.info/papermod-cover.png</url>
      <link>https://christian-gmeiner.info/papermod-cover.png</link>
    </image>
    <generator>Hugo -- 0.159.0</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 25 May 2015 19:42:28 +0000</lastBuildDate>
    <atom:link href="https://christian-gmeiner.info/tags/serializing/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>QMetaEnum: Serializing C&#43;&#43; Enums</title>
      <link>https://christian-gmeiner.info/2015-05-25-qmetaenum-serializing-c-enums/</link>
      <pubDate>Mon, 25 May 2015 19:42:28 +0000</pubDate>
      <guid>https://christian-gmeiner.info/2015-05-25-qmetaenum-serializing-c-enums/</guid>
      <description>&lt;p&gt;I think that almost every C++ programmer looked into a nice and easy way to do C++ Enum serialization. You may want to store the Enum value in a human readable format (json, ini, ..) and later you may also want to read back the value into the object.&lt;/p&gt;
&lt;p&gt;You have here two way how to archive your goal. Use simple int values to represent the Enum value or use Strings. The big advantage with strings it that you do not have to lookup what the meaning of value 4 is, but you can name the value with a String.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I think that almost every C++ programmer looked into a nice and easy way to do C++ Enum serialization. You may want to store the Enum value in a human readable format (json, ini, ..) and later you may also want to read back the value into the object.</p>
<p>You have here two way how to archive your goal. Use simple int values to represent the Enum value or use Strings. The big advantage with strings it that you do not have to lookup what the meaning of value 4 is, but you can name the value with a String.</p>
<p>Here I will present you with an non QObject based way of doing it in Qt5. You may wonder why I do not want to use a QObject as base for my class. The answer is quite simple. I need to be able to have a copy constructor and assignment operator for my class.</p>
<p>So lets start with the simple class Item which has Enum called Type in it.</p>
<div class="oembed-gist"><script src="https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33.js?file=item.h"></script><noscript>View the code on [Gist](https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33).</noscript></div>The big goal is to get Item::type() and Item::setType(QString) working as expected. Here is a small demo main(), which will be used for testing.
<div class="oembed-gist"><script src="https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33.js?file=main.cpp"></script><noscript>View the code on [Gist](https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33).</noscript></div>We exspect the following output:
<p>“APPLE”
“CAR”
“BIRD”</p>
<p>Here Qt’s MetaObject system comes to help. We use the MetaObject to get the correct QMetaEnum and make use of its valueToKey(..) and keyToValue(..) methods.</p>
<div class="oembed-gist"><script src="https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33.js?file=item.cpp"></script><noscript>View the code on [Gist](https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33).</noscript></div>In order to get this up and working we need to modify our header file.
<div class="oembed-gist"><script src="https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33.js?file=item_2.h"></script><noscript>View the code on [Gist](https://gist.github.com/austriancoder/9bb0c0782aafa4bb1dbcc653e9442a33).</noscript></div>The needed pars are here
<ul>
<li>Q_GADGET to get the processed by the meta object compiler (moc)</li>
<li>Q_ENUMS to ‘export’ the enum to the metaObject</li>
</ul>
<p>Qt’s MetaObject system saved my day and helped me to easily serialize C++ Enums in a very elegant way. The other more hacky solution would have been a big if-castle to check for each possible value and do everything on my own. But this could lead to problems if the Enum gets extended by an other developer not aware of the hand-written conversion methods.</p>
<p>All in all I am very happy with this solution.</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
