Using boost::uuids::uuid as a map index
Hi, I'm new to using boost and have hit a problem using UUID. I have a set of classes that have UUID variables to provide unique identifiers. I wish to achieve two things: 1 - Allow objects to reference each other based on their UUID values (imagine the usual databasing student/teacher/course models); 2 - Maintain std::map collections of objects of the same class. Initially, I made the mistake of creating a class instance containing my UUID then inserting it into a map: Std::map <boost::uuid::uid, myClass> myMap; The index of the entry was set as follows: Std::pair <boost::uuids::uuid, myClass> myPair; myPair = std::make_pair( myClassInstance.UUIDField, myClassInstance); myMap.insert( myPair); All look-ups within the map based on the UUID then fail, as adding the object to the map is essentially a copy operation and UUID is non-copyable, so new unique identifiers were generated. I then tried: Std::map <boost::uuids::uuid *, myClass*> myMap; I thought that this should work as the actual UUID value would only ever be held in a single instance of myClass and the relationships between objects could be maintained by following the pointers. Unfortunately, look-ups using this scheme also fail and I can't see where I'm going wrong. Can anybody advise, please? Best wishes. Tim Burgess Raised Bar Ltd Phone: +44 (0)1827 719822 Don't forget to vote for improved access to music and music technology at http://www.raisedbar.net/petition.htm -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Cromwell Enage Sent: 10 December 2011 05:42 To: boost-users@lists.boost.org Subject: [Boost-users] [boost-users][container] associative placement insertion Hi, all! I'm wondering how placement insertion works for pair-associative Boost.Container types. For example, given: //[ex_code struct D { D(char,char) {} // Insert Boost.Move awareness here... }; boost::container::map<int,D> my_map; //] Will my_map.emplace(0, 't', 'p') call D('t', 'p') and move the object into the map? Will Boost.Unordered work the same way? TIA! Cromwell D. Enage _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi, On Sat, Dec 10, 2011 at 17:22, Tim Burgess <tim@raisedbar.net> wrote:
All look-ups within the map based on the UUID then fail, as adding the object to the map is essentially a copy operation and UUID is non-copyable, so new unique identifiers were generated. I then tried:
Std::map <boost::uuids::uuid *, myClass*> myMap;
AFAIK uuid is copyable as it's a POD. However, I don't know about this problem but I remember having failed to make uuid a key of map (I don't remember the reason). If that can help you, I'm using a workaround to allow this : I convert the uuid to std::string (using the generator function) and use it in my whole application to allow finding by uuid from maps. That's obviously more expensive but it works fine. Joël Lamotte
OK, I’m recoding for the string solution now – thanks for the tip. Best wishes. Tim Burgess Raised Bar Ltd Phone: +44 (0)1827 719822 Don't forget to vote for improved access to music and music technology at http://www.raisedbar.net/petition.htm From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Klaim - Joël Lamotte Sent: 10 December 2011 23:22 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Using boost::uuids::uuid as a map index Hi, On Sat, Dec 10, 2011 at 17:22, Tim Burgess <tim@raisedbar.net> wrote: All look-ups within the map based on the UUID then fail, as adding the object to the map is essentially a copy operation and UUID is non-copyable, so new unique identifiers were generated. I then tried: Std::map <boost::uuids::uuid *, myClass*> myMap; AFAIK uuid is copyable as it's a POD. However, I don't know about this problem but I remember having failed to make uuid a key of map (I don't remember the reason). If that can help you, I'm using a workaround to allow this : I convert the uuid to std::string (using the generator function) and use it in my whole application to allow finding by uuid from maps. That's obviously more expensive but it works fine. Joël Lamotte
On Sun, Dec 11, 2011, at 12:22 AM, Klaim - Joël Lamotte wrote: Hi, On Sat, Dec 10, 2011 at 17:22, Tim Burgess <[1]tim@raisedbar.net> wrote: All look-ups within the map based on the UUID then fail, as adding the object to the map is essentially a copy operation and UUID is non-copyable, so new unique identifiers were generated. I then tried: Std::map <boost::uuids::uuid *, myClass*> myMap; AFAIK uuid is copyable as it's a POD. Correct, boost::uuids::uuid is copyable, it is a POD. It can be used as the key in an std::map. I have created a simple test for this and checked it into trunk ([2]http://svn.boost.org/svn/boost/trunk/libs/uuid/test/test_uuid _in_map.cpp). It passes. I have also attached the file. I'm not sure where the problem is with your code. Could you provide a more complete example? However, I don't know about this problem but I remember having failed to make uuid a key of map (I don't remember the reason). If that can help you, I'm using a workaround to allow this : I convert the uuid to std::string (using the generator function) and use it in my whole application to allow finding by uuid from maps. That's obviously more expensive but it works fine. Joël Lamotte Regards, Andy. References 1. mailto:tim@raisedbar.net 2. http://svn.boost.org/svn/boost/trunk/libs/uuid/test/test_uuid_in_map.cpp
On Thu, Dec 22, 2011 at 18:12, Andy Tompkins <atompkins@fastmail.fm> wrote:
Correct, boost::uuids::uuid is copyable, it is a POD.
It can be used as the key in an std::map. I have created a simple test for this and checked it into trunk ( http://svn.boost.org/svn/boost/trunk/libs/uuid/test/test_uuid_in_map.cpp). It passes. I have also attached the file.
Ok that's what I thought at the time.
I'm not sure where the problem is with your code. Could you provide a more complete example?
I think my case was a mix of different library values not playing well together so in the end I used the only type commonly used in both libraries, and it was std::string. So I think it was not directly a problem with uuid. My memory is not clear on the exact problem, sorry. Joël Lamotte
participants (3)
-
Andy Tompkins
-
Klaim - Joël Lamotte
-
Tim Burgess