{"id":408,"date":"2024-03-25T13:10:19","date_gmt":"2024-03-25T12:10:19","guid":{"rendered":"https:\/\/thurow.de\/?p=408"},"modified":"2024-04-03T09:23:44","modified_gmt":"2024-04-03T07:23:44","slug":"ausbruch-aus-der-macrohoelle-einsatz-von-enum-classes","status":"publish","type":"post","link":"https:\/\/thurow.de\/?p=408","title":{"rendered":"Ausbruch aus der Macroh\u00f6lle &#8211; Einsatz von enum classes"},"content":{"rendered":"\n<p>Ein bekanntes Problem beim Einsatz von C ist die &#8222;Macroh\u00f6lle&#8220;. Werte und ganze Funktionsvorlagen werden als Macros definiert. Dieser Beitrag geht auf den Ersatz von define flags durch enum classes ein. Als Beispiel ziehe ich die Funktion <code><a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/open.2.html\" target=\"_blank\" rel=\"noreferrer noopener\">open<\/a><\/code> heran, welche auf unterster Ebene im Userland eine Datei \u00f6ffnet (low-level call). Der Funktion werden eine Reihe von Flags \u00fcbergeben, welche per OR-Operator miteinander kombiniert werden k\u00f6nnen. Dazu ein Eingangsbeispiel (Linux und Windows):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [16]; title: ; notranslate\" title=\"\">\n#include &lt;fcntl.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;sys\/stat.h&gt;\n#include &lt;stdexcept&gt;\n#include &lt;assert.h&gt;\t\n\n#ifdef _WIN32\n#include &lt;io.h&gt;          \/* f\u00fcr MS-DOS\/WIN *\/\n#else\n#include &lt;unistd.h&gt;      \/* f\u00fcr UNIX *\/\n#endif\n\n\nint Open(char const* fileName, int openFlag, int permissionMode)\n{\n\treturn open(fileName, openFlag, permissionMode);\n}\n\nint main(int argc, char *argv&#x5B;])\n{\n\tint fd = Open(&quot;Test.bin&quot;, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);\n\t\n\treturn 0;\n}\n<\/pre><\/div>\n\n\n<p>Nun wird die Zeile 16 im Debugger n\u00e4her betrachtet:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"617\" height=\"245\" src=\"https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-16.png\" alt=\"\" class=\"wp-image-422\" srcset=\"https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-16.png 617w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-16-300x119.png 300w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-16-230x91.png 230w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-16-350x139.png 350w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-16-480x191.png 480w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><figcaption class=\"wp-element-caption\">Die Funktion im Debugger<\/figcaption><\/figure>\n\n\n\n<p>Zu sehen sind der Wert von openFlag mit 0x0242 und von permissionMode mit 0x0180. Die Werte werden als OR-Operatoren \u00fcber Integern zusammengef\u00fchrt. Diese werden per <code>#define<\/code> vordefiniert:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define O_ACCMODE\t   0003\n#define O_RDONLY\t     00\n#define O_WRONLY\t     01\n#define O_RDWR\t\t     02\n#ifndef O_CREAT\n# define O_CREAT\t   0100\t\/* Not fcntl.  *\/\n#endif\n#ifndef O_EXCL\n# define O_EXCL\t\t   0200\t\/* Not fcntl.  *\/\n#endif\n#ifndef O_NOCTTY\n# define O_NOCTTY\t   0400\t\/* Not fcntl.  *\/\n#endif\n#ifndef O_TRUNC\n# define O_TRUNC\t  01000\t\/* Not fcntl.  *\/\n#endif\n#ifndef O_APPEND\n# define O_APPEND\t  02000\n#endif\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#ifdef __USE_MISC\n# define S_IREAD\tS_IRUSR\n# define S_IWRITE\tS_IWUSR\n# define S_IEXEC\tS_IXUSR\n#endif\n<\/pre><\/div>\n\n\n<p>Dieser Weg ist der typische Weg der C-Programmierung. Nun wird ein alternativer Weg in C++ aufgezeigt. Hierzu werden der Filedeskriptor und seine zugeh\u00f6rigen low level Funktionen als Klasse gekapselt:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/**\n Basic file operation with file descriptor.\n \n \\author\tDr. Torsten Thurow\n \\date\t\t27.12.2020\n *\/\nclass LowLevelFileStream\n{\nprotected:\n\tint _fd = -1;     \/\/\/&gt; The file descriptor.\n\tsize_t _pos = 0;  \/\/\/&gt; The read \/ write position.\n<\/pre><\/div>\n\n\n<p>Innerhalb der Klasse werden nun die open flags und permission modes als enum classes realisiert:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\tenum class OpenFlags\n\t{\n\t\tCreate\t\t= O_CREAT,\t\t\t\t\/\/\/&gt; Creates a file and opens it for writing. Has no effect if the file specified by filename exists. The pmode argument is required when Create is specified.\n\t\tAppend\t\t= O_APPEND,\t\t\t\t\/\/\/&gt; Moves the file pointer to the end of the file before every write operation.\n\t\tTrunc\t\t= O_TRUNC,\t\t\t\t\/\/\/&gt; Opens a file and truncates it to zero length. The file must have write permission. Can&#039;t be specified with ReadOnly. Trunc used with Create opens an existing file or creates a file. Note: The Trunc flag destroys the contents of the specified file.\n#ifdef _WIN32\n\t\tBinary\t\t= O_BINARY,\t\t\t\t\/\/\/&gt; Opens the file in binary (untranslated) mode. Translations involving carriage-return and line feed characters are suppressed.\n\t\tText\t\t= O_TEXT,\t\t\t\t\/\/\/&gt; Opens a file in text (translated) mode.\n\t\tSequential\t= O_SEQUENTIAL,\t\t\t\/\/\/&gt; Specifies that caching is optimized for, but not restricted to, sequential access from disk.\n#endif\n\t\tReadOnly\t= O_RDONLY,\t\t\t\t\/\/\/&gt; Opens a file for reading only. Can&#039;t be specified with ReadWrite or WriteOnly.\n\t\tWriteOnly\t= O_WRONLY,\t\t\t\t\/\/\/&gt; Opens a file for writing only. Can&#039;t be specified with ReadWrite or ReadOnly.\n\t\tReadWrite\t= O_RDWR,\t\t\t\t\/\/\/&gt; Opens a file for both reading and writing. Can&#039;t be specified with ReadOnly or WriteOnly.\n\t};\n\n\tenum class PermissionMode\n\t{\n\t\tRead\t\t= S_IREAD,\t\t\t\t\/\/\/&gt; Only reading permitted.\n\t\tWrite\t\t= S_IWRITE,\t\t\t\t\/\/\/&gt; Writing permitted. (In effect, permits reading and writing.)\n\t\tReadWrite\t= S_IREAD | S_IWRITE \t\/\/\/&gt; Reading and writing permitted.\n\t};\n<\/pre><\/div>\n\n\n<p>Diese m\u00fcssen nun nur noch um einen OR-Operator erg\u00e4nzt werden:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ninline TL::LowLevelFileStream::LowLevelFileStream::OpenFlags operator | (const TL::LowLevelFileStream::LowLevelFileStream::OpenFlags a, const TL::LowLevelFileStream::LowLevelFileStream::OpenFlags b)\n{\n\tint res = (int)a | (int)b;\n\treturn (TL::LowLevelFileStream::LowLevelFileStream::OpenFlags)res;\n}\n<\/pre><\/div>\n\n\n<p>Das Ergebnis: der Aufruf der Kapselung von open ist nun typsicher und im Debugger werden statt der reinen Werte der Integers auch ihre Bedeutungen angezeigt:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1114\" height=\"535\" src=\"https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18.png\" alt=\"\" class=\"wp-image-429\" srcset=\"https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18.png 1114w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18-300x144.png 300w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18-768x369.png 768w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18-1000x480.png 1000w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18-230x110.png 230w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18-350x168.png 350w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-18-480x231.png 480w\" sizes=\"(max-width: 1114px) 100vw, 1114px\" \/><\/figure>\n\n\n\n<p>und noch einmal unter Windows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"715\" height=\"523\" src=\"https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-19.png\" alt=\"\" class=\"wp-image-431\" srcset=\"https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-19.png 715w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-19-300x219.png 300w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-19-230x168.png 230w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-19-350x256.png 350w, https:\/\/thurow.de\/wp-content\/uploads\/2024\/03\/grafik-19-480x351.png 480w\" sizes=\"(max-width: 715px) 100vw, 715px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Ein bekanntes Problem beim Einsatz von C ist die &#8222;Macroh\u00f6lle&#8220;. Werte und ganze Funktionsvorlagen werden als Macros definiert. Dieser Beitrag geht auf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,23,20,9],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-embedded-programmierung","category-fehlersuche","category-programmierung"],"_links":{"self":[{"href":"https:\/\/thurow.de\/index.php?rest_route=\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thurow.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thurow.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thurow.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thurow.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=408"}],"version-history":[{"count":17,"href":"https:\/\/thurow.de\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/thurow.de\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/thurow.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thurow.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thurow.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}