<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Pimpl-Design]]></title><description><![CDATA[<p>Aloha!</p>
<p>Ich doktore gerade ein wenig mit dem Pimpl-Idiom herum (<a href="http://en.wikipedia.org/wiki/Opaque_pointer#C.2B.2B" rel="nofollow">wer's nicht kennt</a>) und weiß noch nicht ganz, wie ich's mit den .cpp- und .h-Dateien handhaben würde, wenn ich eine Bibliothek schreiben <em>würde</em>. Aber auch unter Beachtung, dass ich verschiedene Implementationen für unterschiedliche Betriebssysteme haben würde.</p>
<p>Mein favorisiertes Design sieht momentan so aus, hat aber nicht mehr soo viel mit dem Pimpl-Idiom zu tun (ich hab der Einfachheit halber einfach mal eine simple Mutex-Klasse verpackt):</p>
<pre><code class="language-cpp">// Mutex.h
#pragma once

class Mutex
{
	public:
		Mutex();
		~Mutex();
		void lock();
		void unlock();

	private:
		Mutex( const Mutex&amp; other );
		Mutex&amp; operator=( const Mutex&amp; other );

	private:
		void* m_data;
};

// Mutex.cpp
#include &quot;Mutex.h&quot;
#include &quot;OS.h&quot;

// Könnten genauso gut .cpp-Dateien sein, grad egal
#if defined SYSTEM_WINDOWS
	#include &quot;Mutex_Windows.h&quot;
#elif defined SYSTEM_LINUX
	#include &quot;Mutex_Linux.h&quot;
#elif defined SYSTEM_MACOS
	#include &quot;Mutex_MacOS.h&quot;
#endif

// Mutex_Windows.h
#include &lt;windows.h&gt;

struct Data
{
	CRITICAL_SECTION cs;
};
#define data (*static_cast&lt;Data*&gt;(m_data))

Mutex::Mutex() : m_data(new Data())
{
	InitializeCriticalSection( &amp;data.cs );
}

Mutex::~Mutex()
{
	DeleteCriticalSection( &amp;data.cs );
	delete m_data;
}

void Mutex::lock()
{
	EnterCriticalSection( &amp;data.cs );
}

void Mutex::unlock()
{
	LeaveCriticalSection( &amp;data.cs );
}
</code></pre>
<p>Hier mache ich mir halt Sorgen wegen dem Makro und dem void*-Pointer, ansonsten finde ich das eine schöne Lösung - weil's imo einfach erweiterbar und einfach wartbar ist.<br />
Was sagt ihr dazu und kennt jemand noch bessere (wie auch immer geartete) Lösungen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/220607/pimpl-design</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 01:02:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/220607.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 18 Aug 2008 15:37:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Pimpl-Design on Mon, 18 Aug 2008 15:37:39 GMT]]></title><description><![CDATA[<p>Aloha!</p>
<p>Ich doktore gerade ein wenig mit dem Pimpl-Idiom herum (<a href="http://en.wikipedia.org/wiki/Opaque_pointer#C.2B.2B" rel="nofollow">wer's nicht kennt</a>) und weiß noch nicht ganz, wie ich's mit den .cpp- und .h-Dateien handhaben würde, wenn ich eine Bibliothek schreiben <em>würde</em>. Aber auch unter Beachtung, dass ich verschiedene Implementationen für unterschiedliche Betriebssysteme haben würde.</p>
<p>Mein favorisiertes Design sieht momentan so aus, hat aber nicht mehr soo viel mit dem Pimpl-Idiom zu tun (ich hab der Einfachheit halber einfach mal eine simple Mutex-Klasse verpackt):</p>
<pre><code class="language-cpp">// Mutex.h
#pragma once

class Mutex
{
	public:
		Mutex();
		~Mutex();
		void lock();
		void unlock();

	private:
		Mutex( const Mutex&amp; other );
		Mutex&amp; operator=( const Mutex&amp; other );

	private:
		void* m_data;
};

// Mutex.cpp
#include &quot;Mutex.h&quot;
#include &quot;OS.h&quot;

// Könnten genauso gut .cpp-Dateien sein, grad egal
#if defined SYSTEM_WINDOWS
	#include &quot;Mutex_Windows.h&quot;
#elif defined SYSTEM_LINUX
	#include &quot;Mutex_Linux.h&quot;
#elif defined SYSTEM_MACOS
	#include &quot;Mutex_MacOS.h&quot;
#endif

// Mutex_Windows.h
#include &lt;windows.h&gt;

struct Data
{
	CRITICAL_SECTION cs;
};
#define data (*static_cast&lt;Data*&gt;(m_data))

Mutex::Mutex() : m_data(new Data())
{
	InitializeCriticalSection( &amp;data.cs );
}

Mutex::~Mutex()
{
	DeleteCriticalSection( &amp;data.cs );
	delete m_data;
}

void Mutex::lock()
{
	EnterCriticalSection( &amp;data.cs );
}

void Mutex::unlock()
{
	LeaveCriticalSection( &amp;data.cs );
}
</code></pre>
<p>Hier mache ich mir halt Sorgen wegen dem Makro und dem void*-Pointer, ansonsten finde ich das eine schöne Lösung - weil's imo einfach erweiterbar und einfach wartbar ist.<br />
Was sagt ihr dazu und kennt jemand noch bessere (wie auch immer geartete) Lösungen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1567040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1567040</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Mon, 18 Aug 2008 15:37:39 GMT</pubDate></item><item><title><![CDATA[Reply to Pimpl-Design on Mon, 18 Aug 2008 15:45:38 GMT]]></title><description><![CDATA[<p>Und wieso lässt du die Vorwärtsdeklaration weg, wodurch du den void-Zeiger und das Makro vernichten könntest? Das leuchtet mir irgendwie nicht so ein.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1567043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1567043</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 18 Aug 2008 15:45:38 GMT</pubDate></item><item><title><![CDATA[Reply to Pimpl-Design on Mon, 18 Aug 2008 15:53:10 GMT]]></title><description><![CDATA[<p>Oh je, wie recht du hast <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>edit: Dann hat sich der Thread ja gegessen, weil's ja direkt nach dem Pimpl spielt.. Danke Dravere..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1567048</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1567048</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Mon, 18 Aug 2008 15:53:10 GMT</pubDate></item></channel></rss>