Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: pfc::map_t (Read 3228 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

pfc::map_t

is there a good example of using pfc::map_t? 
1-I'm having trouble figuring out how to list all the elements... this is for the purpose of serializing it basically, normally I'd use the map style methods to interact with the map
2-if I use a wchar_t * as the key, the map seems to stop functioning properly if I free the memory.  I guess that makes sense, however, it leaves me scratching my head about memory management.  My knowledge about that has been largely garbage collected since my life lost all references to c++ for several years.  When I'm done with a map do I need to somehow free the keys I used?  Or if I free the map does it free it's keys?  What about if my keys\values are NOT pointers, what then?

pfc::map_t

Reply #1
1) enumerate() takes a callback which it will run for each element in the collection, or use an iterator and iterate that.
2) You should use data types for key and value that have proper RAII semantics. That is, they should have a proper cctor, op= and dtor. For text keys, use one of the pfc string types like pfc::string/pfc::string8, or a std::wstring/std::string. The map owns its keys and values, but it's only shallow. It's up to you to give them proper RAII semantics.
Stay sane, exile.

pfc::map_t

Reply #2
thanks... what is the proper signature of the method to send to enumerate?

pfc::map_t

Reply #3
Anything callable with the signature void (t_key const& key, t_value& value).
Like say, boost::bind(&S::f, this, _1, _2), &g, or T().
Code: [Select]
struct S {
  void f(int key, float& value) { }
};

void g(int const& key, float value) {}

struct T {
  void operator () (int const& key, float& value) { }
};
Stay sane, exile.

pfc::map_t

Reply #4
it looks to me like the iterator may be the better way to go but I can't figure out how to use that either.  I need my hand held apparently.  I've tried this:

static void make_preset(pfc::map_t<std::wstring, dsp_chain_config_impl *, pfc::comparator_strcmp> *cMap, std::wstring tfString, dsp_preset & out) {
      int count=cMap->get_count();
      dsp_preset_builder builder; builder << tfString; builder<<count;
      for(pfc::iterator<pfc::map_t<std::wstring, dsp_chain_config_impl*, pfc::comparator_strcmp>> i=cMap->first(); i.is_valid(); i.next())
      {
         std::wstring g;
         i->g;
      }
      builder.finish(g_get_guid(), out);
   }

two things to note
1- I get a compilation error stating:
Error   3   error C2440: 'initializing' : cannot convert from 'pfc::iterator<t_item>' to 'pfc::iterator<t_item>'   c:\users\jrichard\downloads\foo\foobar2000\foo_sample\dsp.cpp   132   foo_sample

when to my understanding my iterator template exactly equals my map type

2- I can't figure how to exactly get my keys and values from the iterator.  there is a promising looking function, "_node()" which the documentation tells me not to use.

 

pfc::map_t

Reply #5
That is not the complete error, it should tell you "with t_item = Foo and t_item = Bar" on the lines below.
As for the iterator, map_t's t_storage has a const m_key and a m_value.
Stay sane, exile.