<?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[Ersatz fuer static object in einem cpp File]]></title><description><![CDATA[<p>Halli hallo,</p>
<p>ich wollte mir eine Enum helper Klasse schreiben mit der ich von enume_type -&gt; string, vice versa und die vorhandenen values als string zurueck bekommen kann schreiben.</p>
<p>Dabei ist folgendes entstanden:</p>
<pre><code>template&lt;typename T&gt;
struct enumHelper
{
public:
	typedef std::map&lt;typename T ,std::string&gt; enumToStr_t;
	typedef std::function&lt;void (T,const std::string&amp;)&gt; add_callback;
	typedef std::vector&lt;std::string&gt; nameVec;

	static std::string toString(T algorithm) {
		return map().at( algorithm );
	}
	static T fromString(const std::string&amp; algorithm) {
		auto static_map = map();
		for( auto iter = std::begin( static_map  ); iter != std::end( static_map ); iter++) {
			if(iter-&gt;second == algorithm)
				return iter-&gt;first;
		}
		assert(0);		
	}

	static nameVec values() {
		nameVec ret;		
		auto static_map = map();	

		std::for_each( std::begin(static_map), std::end(static_map), 
			[&amp;ret] (enumToStr_t::value_type iter) {
				ret.push_back( iter.second); 
		});

		return ret;
	}

	enumHelper(std::function&lt;void ( typename add_callback ) &gt; initFunc) { 
		enumToStr_t&amp; m = map();

		initFunc( [&amp;m] ( T v,const std::string&amp; str) {
			m[v] = str;
		}); 
	}
private:
	static enumToStr_t&amp; map() { 
		static enumToStr_t nameMap;
		return nameMap;
	};
};
</code></pre>
<p>Um meine static map mit values zu fuellen, erstell ich in einem cpp File zB. sowas:</p>
<p>hash.h</p>
<pre><code>enum algo_t { sha_256, sha_512, whirlpool };
        typedef enumHelper&lt;algo_t&gt; algo_enum;
</code></pre>
<p>hash.cpp</p>
<pre><code>static void nameMap(enumHelper&lt;algo_t&gt;::add_callback cb) {

	cb(sha_256, &quot;SHA_256&quot;);
	cb(sha_512, &quot;SHA_512&quot;);
	//cb(sha3_512, &quot;SHA3_512&quot;);
	cb(whirlpool, &quot;WHIRLPOOL&quot;);

}
static algo_enum g_hashAlgorithmEnumHelper( &amp;nameMap );
</code></pre>
<p>Jetzt hab ich aber das Problem, dass ich eine statische lib erstelle und (laut stackoverflow) da das &quot;g_hash..&quot; in meinem normalen Programm nicht referenziert wird auch nicht mit ins Programm kommt. Und somit ist meine map leer :(.</p>
<p>Jetzt ist die Frage, wie ich das static object ersetzen kann aber nicht extra eine init Funktion fuer alles enums aufrufen muss fuer die ich das bereitstellen will?</p>
<p>Gruessle</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/316660/ersatz-fuer-static-object-in-einem-cpp-file</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Jul 2026 17:25:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/316660.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 12 May 2013 06:21:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 06:21:48 GMT]]></title><description><![CDATA[<p>Halli hallo,</p>
<p>ich wollte mir eine Enum helper Klasse schreiben mit der ich von enume_type -&gt; string, vice versa und die vorhandenen values als string zurueck bekommen kann schreiben.</p>
<p>Dabei ist folgendes entstanden:</p>
<pre><code>template&lt;typename T&gt;
struct enumHelper
{
public:
	typedef std::map&lt;typename T ,std::string&gt; enumToStr_t;
	typedef std::function&lt;void (T,const std::string&amp;)&gt; add_callback;
	typedef std::vector&lt;std::string&gt; nameVec;

	static std::string toString(T algorithm) {
		return map().at( algorithm );
	}
	static T fromString(const std::string&amp; algorithm) {
		auto static_map = map();
		for( auto iter = std::begin( static_map  ); iter != std::end( static_map ); iter++) {
			if(iter-&gt;second == algorithm)
				return iter-&gt;first;
		}
		assert(0);		
	}

	static nameVec values() {
		nameVec ret;		
		auto static_map = map();	

		std::for_each( std::begin(static_map), std::end(static_map), 
			[&amp;ret] (enumToStr_t::value_type iter) {
				ret.push_back( iter.second); 
		});

		return ret;
	}

	enumHelper(std::function&lt;void ( typename add_callback ) &gt; initFunc) { 
		enumToStr_t&amp; m = map();

		initFunc( [&amp;m] ( T v,const std::string&amp; str) {
			m[v] = str;
		}); 
	}
private:
	static enumToStr_t&amp; map() { 
		static enumToStr_t nameMap;
		return nameMap;
	};
};
</code></pre>
<p>Um meine static map mit values zu fuellen, erstell ich in einem cpp File zB. sowas:</p>
<p>hash.h</p>
<pre><code>enum algo_t { sha_256, sha_512, whirlpool };
        typedef enumHelper&lt;algo_t&gt; algo_enum;
</code></pre>
<p>hash.cpp</p>
<pre><code>static void nameMap(enumHelper&lt;algo_t&gt;::add_callback cb) {

	cb(sha_256, &quot;SHA_256&quot;);
	cb(sha_512, &quot;SHA_512&quot;);
	//cb(sha3_512, &quot;SHA3_512&quot;);
	cb(whirlpool, &quot;WHIRLPOOL&quot;);

}
static algo_enum g_hashAlgorithmEnumHelper( &amp;nameMap );
</code></pre>
<p>Jetzt hab ich aber das Problem, dass ich eine statische lib erstelle und (laut stackoverflow) da das &quot;g_hash..&quot; in meinem normalen Programm nicht referenziert wird auch nicht mit ins Programm kommt. Und somit ist meine map leer :(.</p>
<p>Jetzt ist die Frage, wie ich das static object ersetzen kann aber nicht extra eine init Funktion fuer alles enums aufrufen muss fuer die ich das bereitstellen will?</p>
<p>Gruessle</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322753</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322753</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 12 May 2013 06:21:48 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 09:16:17 GMT]]></title><description><![CDATA[<pre><code>typedef std::map&lt;typename T ,std::string&gt; enumToStr_t;
</code></pre>
<p>Ich verstehe natürlich den Trick, aber ist das nicht ill-formed? Oder hat mein GCC 4.8 noch nicht genug C++11-Unterstützung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322764</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322764</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 12 May 2013 09:16:17 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 09:56:01 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<pre><code>typedef std::map&lt;typename T ,std::string&gt; enumToStr_t;
</code></pre>
<p>ist das nicht ill-formed?</p>
</blockquote>
<p>ist es, ebenso wie Zeile 33.<br />
typename ist zwar an einigen Stellen erlaubt, an denen es nicht erforderlich wäre. Trotzdem erlaubt die Grammatik</p>
<p><em>typename-specifier:</em><br />
<code>typename</code> <em>nested-name-specifier identifier</em><br />
<code>typename</code> <em>nested-name-specifier</em> <code>template</code> <sub>opt</sub> <em>simple-template-id</em></p>
<p>weiterhin nicht einen einfachen Bezeichner an dieser Stelle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322766</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322766</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 12 May 2013 09:56:01 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 12:10:57 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<pre><code>typedef std::map&lt;typename T ,std::string&gt; enumToStr_t;
</code></pre>
<p>Ich verstehe natürlich den Trick, aber ist das nicht ill-formed? Oder hat mein GCC 4.8 noch nicht genug C++11-Unterstützung?</p>
</blockquote>
<p>Hmm ja weiss jetzt auch ned so genau warum ich da gleich nochmal ein &quot;typename&quot; hingeschrieben hab. Dummerweise sagt mir da mein MSVC2010 nix dazu das, dass nicht erlaubt waere. Aber kennt man ja das der den Standard nicht immer so genau nimmt.</p>
<p>Aber jetz nochmal zu meinem Problem:</p>
<p>Und zwar hab ich das jetzt so geloest:</p>
<pre><code>template&lt;typename T, void(*initFunction)(std::map&lt;T, std::string&gt;&amp; ) &gt;
struct enumHelper
{
//....
private:
	static enumToStr_t&amp; map() { 
		static enumToStr_t nameMap;

		if( nameMap.empty() ) {
			initFunction( nameMap );
		}

		return nameMap;
	};
</code></pre>
<p>Und dann in hash</p>
<pre><code>//hash.h
	enum algo_t { sha_256, sha_512, whirlpool };	

	namespace detail {
		void initHashEnum(std::map&lt;algo_t, std::string&gt;&amp; map);
	}
	typedef enumHelper&lt;algo_t, &amp;detail::initHashEnum&gt; algo_enum;

//hash.cpp
namespace detail {
	void initHashEnum(std::map&lt;algo_t, std::string&gt;&amp; map) {

		map[sha_256] = &quot;SHA_256&quot;;
		map[sha_512] = &quot;SHA_512&quot;;	
		map[whirlpool] = &quot;WHIRLPOOL&quot;;

	}
}
</code></pre>
<p>Und sowmit kann ich jetzt ueber:</p>
<pre><code>std::cout &lt;&lt; algo_enum::toString( sha_256) &lt;&lt; &quot;\n&quot;;
// bzw
std::string algo;
std::cin &gt;&gt; algo;
algo_t hashType = algo_enum::fromString( algo );
</code></pre>
<p>meine Enums fuer die GUI besser aufbereiten.</p>
<p>Aber jetzt ist die Frage macht das Sinn und ist es einigermassen elegant oder hatte ihr schon mal das gleiche Problem und hab das um einiges besser geloest?</p>
<p>Gruessle</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322787</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 12 May 2013 12:10:57 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 14:03:13 GMT]]></title><description><![CDATA[<p>Gibt es einen vernünftigen Grund, nicht einfach ein array (sortiert aus pair&lt;enum,const char*&gt; oder einfach aus const char*, falls die Aufzählungskonstanten klein sind) zu verwenden? Die ganze Komplexität dynamischer Initialisierung wird vermieden, der Code wird erheblich kürzer und wahrscheinlich ist es auch noch schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322799</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 12 May 2013 14:03:13 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 15:02:01 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Gibt es einen vernünftigen Grund, nicht einfach ein array (sortiert aus pair&lt;enum,const char*&gt; oder einfach aus const char*, falls die Aufzählungskonstanten klein sind) zu verwenden? Die ganze Komplexität dynamischer Initialisierung wird vermieden, der Code wird erheblich kürzer und wahrscheinlich ist es auch noch schneller.</p>
</blockquote>
<p>Muss ehrlich sagen ich steh grad aufn Schlauch. Soll ich dann statt der map ein const char* array nehmen oder statt meiner enumHelper class.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322806</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 12 May 2013 15:02:01 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 15:42:19 GMT]]></title><description><![CDATA[<p>Sorry, mein Bruder war zu Besuch.</p>
<p>Mach niemals enum-helpers!<br />
Oder wechsle nach Java/C#.</p>
<p>In C++ reicht nötigenfalls zur lesbaren reinen Ausgabe für den ISO-Prüfer ein Array von C-Strings.</p>
<p>Siehe YAGNI</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322814</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322814</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 12 May 2013 15:42:19 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 15:49:50 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Sorry, mein Bruder war zu Besuch.</p>
<p>Mach niemals enum-helpers!<br />
Oder wechsle nach Java/C#.</p>
<p>In C++ reicht nötigenfalls zur lesbaren reinen Ausgabe für den ISO-Prüfer ein Array von C-Strings.</p>
<p>Siehe YAGNI</p>
</blockquote>
<p>Hmm naja mir gehts drum das ich den User einstellen lassen will was er zB. fuer einen Hash Algorithmus fuer HMAC verwenden will.</p>
<p>Es gibt jetz wie oben zu sehen ist zB. diese 3 Algorithmen: sha_256, sha_512, whirlpool.<br />
Wie lassen ich jetzt dann am besten den User waehlen bzw lese die Settings aus einem config file (in json gehalten) wieder raus?</p>
<p>Da brauch ich doch sowas wie toString(enumType) und fromString(enumType) oder seh ich das falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322816</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322816</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 12 May 2013 15:49:50 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 15:58:51 GMT]]></title><description><![CDATA[<p>Ne, es reicht wenn du in der Eingabe die Strings prüfst.<br />
Du brauchst doch nicht beide Richtungen, oder? Also wieder ausgeben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322819</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 12 May 2013 15:58:51 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 16:00:27 GMT]]></title><description><![CDATA[<p>stuxn schrieb:</p>
<blockquote>
<p>Da brauch ich doch sowas wie toString(enumType) und fromString(enumType) oder seh ich das falsch?</p>
</blockquote>
<p>Ja!!!!!!!!!<br />
Und warum machste das nicht? Warum willst Du das in einen Typ verkleiden? Warum willste neue Begriffe dafür erfinden, neue Klassen?</p>
<p>Ich wierderhole:</p>
<p>stuxn schrieb:</p>
<blockquote>
<p>Da brauch ich doch sowas wie toString(enumType) und fromString(enumType) oder seh ich das falsch?</p>
</blockquote>
<p>Naja, fromstring(string), aber wir wußten, was gemeint war. Für komlpexere Lösungen verweise ich Dich an Sone. Der geht zur Zeit ab wie Schmidts Kätzchen und kann Dir alles sehr <s>komlpex</s>idiomatisch darstellen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322820</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322820</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 12 May 2013 16:00:27 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 16:23:09 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Ne, es reicht wenn du in der Eingabe die Strings prüfst.<br />
Du brauchst doch nicht beide Richtungen, oder? Also wieder ausgeben?</p>
</blockquote>
<p>Ja doch eigentlich schon, da ich ja von meinen Settings Objekt in dem zB steht ich soll fuer HMAC sha_256 verwenden, in das config File ( ist im json Format) schreiben muss.</p>
<p>volkard schrieb:</p>
<blockquote>
<p>stuxn schrieb:</p>
<blockquote>
<p>Da brauch ich doch sowas wie toString(enumType) und fromString(enumType) oder seh ich das falsch?</p>
</blockquote>
<p>Ja!!!!!!!!!<br />
Und warum machste das nicht? Warum willst Du das in einen Typ verkleiden? Warum willste neue Begriffe dafür erfinden, neue Klassen?</p>
<p>Ich wierderhole:</p>
<p>stuxn schrieb:</p>
<blockquote>
<p>Da brauch ich doch sowas wie toString(enumType) und fromString(enumType) oder seh ich das falsch?</p>
</blockquote>
<p>Naja, fromstring(string), aber wir wußten, was gemeint war. Für komlpexere Lösungen verweise ich Dich an Sone. Der geht zur Zeit ab wie Schmidts Kätzchen und kann Dir alles sehr <s>komlpex</s>idiomatisch darstellen.</p>
</blockquote>
<p>Sollte das dann ca so ausschauen?</p>
<pre><code>//hash.h
namespace hash {
   enum algo_t { sha_256, sha_512, whirlpool };  
   const char* toString(algo_t algo);
   algo_t fromString(const std::string&amp; algo);
}

//hash.cpp
namespace hash {

   static std::pair&lt;algo_t, const char*&gt; hashNamePair[] = { std::make_pair(sha_256, &quot;sha_256&quot;),std::make_pair(sha_512, &quot;sha_512&quot;),std::make_pair(whirlpool, &quot;whirlpool&quot;) };

   const char* toString(algo_t algo) {

      auto r = std::find_if( std::begin( hashNamePair ), std::end( hashNamePair), [algo] (std::pair&lt;algo_t, const char*&gt;&amp; value) { return value.first == algo; });
      return r-&gt;second;
   }

   algo_t fromString(const std::string&amp; algo) {

      auto r = std::find_if( std::begin( hashNamePair ), std::end( hashNamePair), [algo] (std::pair&lt;algo_t, const char*&gt;&amp; value) { return value.second == algo; });
      return r-&gt;first;
   }

}
</code></pre>
<p>Und das dann fuer jedes enum das ich dem User bereitstellen will?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322825</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322825</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 12 May 2013 16:23:09 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 16:54:03 GMT]]></title><description><![CDATA[<p>stuxn schrieb:</p>
<blockquote>
<p>Sollte das dann ca so ausschauen?</p>
</blockquote>
<p>Jo, normalerweise schon. Ist recht schlicht, jeder Mitarbeiter vesteht es und es ist falls mehr Performace gebraucht wird, jederzeit unter Beibehalteng der Schnittstelle ausbaubar. Was will man mehr?</p>
<p>Konkretere Namen, ja.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322827</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 12 May 2013 16:54:03 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 17:21:05 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>stuxn schrieb:</p>
<blockquote>
<p>Sollte das dann ca so ausschauen?</p>
</blockquote>
<p>Jo, normalerweise schon. Ist recht schlicht, jeder Mitarbeiter vesteht es und es ist falls mehr Performace gebraucht wird, jederzeit unter Beibehalteng der Schnittstelle ausbaubar. Was will man mehr?</p>
<p>Konkretere Namen, ja.</p>
</blockquote>
<p>Hmm ja ok versteh ich schon ein bissle. Aber ich hab hier ca 5 Enums. Jetzt schreib ich fuer alle die 2 Funktionen. Nach einer weile faellt mir auf das es zu langsam ist wie du gesagt hast. Jetzt muss ich alle 10 Funktionen aendern. Genau sowas wollte ich halt mit meiner enumHelper class vermeiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322835</guid><dc:creator><![CDATA[stuxn]]></dc:creator><pubDate>Sun, 12 May 2013 17:21:05 GMT</pubDate></item><item><title><![CDATA[Reply to Ersatz fuer static object in einem cpp File on Sun, 12 May 2013 17:50:33 GMT]]></title><description><![CDATA[<p>stuxn schrieb:</p>
<blockquote>
<p>Hmm ja ok versteh ich schon ein bissle. Aber ich hab hier ca 5 Enums.</p>
</blockquote>
<p>Oh, 5. Mistige Arbeitgeber.</p>
<p>stuxn schrieb:</p>
<blockquote>
<p>Jetzt schreib ich fuer alle die 2 Funktionen.</p>
</blockquote>
<p>Null Problemo. Insbesondere dürfen sie ja sogar, wenn sie im Prinzip das selbe tun, eine andere Funkjtion aufrufen zum Beispiel.</p>
<p>stuxn schrieb:</p>
<blockquote>
<p>Nach einer weile faellt mir auf das es zu langsam ist wie du gesagt hast. Jetzt muss ich alle 10 Funktionen aendern. Genau sowas wollte ich halt mit meiner enumHelper class vermeiden.</p>
</blockquote>
<p>Das sehe ich nicht so. Und das siehst Du auch nicht, nachdem Du es hier ausgesprochen hast. Oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322840</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322840</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 12 May 2013 17:50:33 GMT</pubDate></item></channel></rss>