Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not so long ago I made some trick to have enums properly displayed in QComboBox and to have definition of enum and string representations as one statement</p> <pre><code>#pragma once #include &lt;boost/unordered_map.hpp&gt; namespace enumeration { struct enumerator_base : boost::noncopyable { typedef boost::unordered_map&lt;int, std::wstring&gt; kv_storage_t; typedef kv_storage_t::value_type kv_type; kv_storage_t const &amp; kv() const { return storage_; } LPCWSTR name(int i) const { kv_storage_t::const_iterator it = storage_.find(i); if(it != storage_.end()) return it-&gt;second.c_str(); return L"empty"; } protected: kv_storage_t storage_; }; template&lt;class T&gt; struct enumerator; template&lt;class D&gt; struct enum_singleton : enumerator_base { static enumerator_base const &amp; instance() { static D inst; return inst; } }; } #define QENUM_ENTRY(K, V, N) K, N storage_.insert(std::make_pair((int)K, V)); #define QBEGIN_ENUM(NAME, C) \ enum NAME \ { \ C \ } \ }; \ } \ #define QEND_ENUM(NAME) \ }; \ namespace enumeration \ { \ template&lt;&gt; \ struct enumerator&lt;NAME&gt;\ : enum_singleton&lt; enumerator&lt;NAME&gt; &gt;\ { \ enumerator() \ { //usage /* QBEGIN_ENUM(test_t, QENUM_ENTRY(test_entry_1, L"number uno", QENUM_ENTRY(test_entry_2, L"number dos", QENUM_ENTRY(test_entry_3, L"number tres", QEND_ENUM(test_t))))) */ </code></pre> <p>Now you've got <code>enumeration::enum_singleton&lt;your_enum&gt;::instance()</code> able to convert enums to strings. If you replace <code>kv_storage_t</code> with <code>boost::bimap</code>, you will also be able to do backward conversion. Common base class for converter was introduced to store it in Qt object, because Qt objects couldn't be templates</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload