c++ - Return pointer from type and string of name -


How do I return a (n current) pointer to a string named in the known type? Say I got some maps & lt; String, double & gt; to map1 and some vector & lt; String & gt; named vec1 is created. I would like to write a task that will return map1 to "map1" and vec1 to "vec1" ). (And additional logic to specify each related type).

Is this possible?

My final goal is to get a QWidget by its name QString form, because I am working in a large number of them, though an answer The structure should be independent, right?

You need to do some type of dynamic transmission. To do this, you can simply start with what you offer:

  enum class type {A = 0, B = 1}; Zero (* Handler []) (Zero *) = {& amp; HandleA; And HandleB}; :: std :: unordered_map & lt; :: std :: string, :: std :: tuple & lt; Type, Zero * & gt; & Gt; Registry;  

Anything that remains is to be searched:

  zero lookup (:: std :: string const & amp; name) {auto & Amp; T = Registry (Name); Operators [static_cast & lt; Size_t> (:: std :: Get & lt; 0 & gt; (T))] (:: get std :: & lt; 1 & gt; (T)); }  

Casting automagic logic for the handler

The handler is called zero * with all types of logic - adding a little template magic :

  template & lt; Typename t, zero (* f) (t *) & gt; Zero handle (Zero * arg) {f (static_cast & lt; T * & gt; (RGR)); } Zero (* handler []) (zero *) = {& amp; Handle & lt; A, and handle A & gt; And & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp;; };  

Now, the prototype is for example zero handles (A *) .

Simple adding objects to the registry

With the current code, you can add objects to your registry in this way:

  AA ; Registry.emplace ("A # 1", :: std :: make_tuple (type :: A, & A));  

Although it works perfectly, we want to do something a bit more elegant, let's start by changing the enum class type , which we would like to do We also know that we want to present it to:

  template & lt; Typename T & gt; Structure type; The template's & lt; & Gt; Structure type & lt; A & gt; {Static command size_t id = 0; }; The template's & lt; & Gt; Structure type & lt; B & gt; {Static command size_t id = 1; };  

Of course, now we have to fix the registry type:

  :: std :: unordered_map < :: std :: string, :: std :: Tuplicate & lt; Size_t, null *> & Gt; Registry;  

And finally we can provide a simple insert function:

  template & lt; Typename T & gt; Zero entry (:: std :: string const & amp; name, T * object) {registry.emplace (name, :: std :: make_tuple (type & lt; T & gt; :: id, static_cast & lt; void * & Gt; (object))}  

last use example

  aa; enter ("A # 1", & amp; a;) ; Lookup ("A # 1");  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -