5

I have a C++ variable of type std::vector<std::bitset<128> > which is defined and populated in a C++ class (which is called from my Objective-C++ class.)

I would like to store this object in an NSDictionary - or some equivalent of. I clearly can't simply add the std::vector<std::bitset<128> > to the NSDictionary because it's not of type id.

So my question is this: how can I achieve the same concept? How can I store a std::vector<std::bitset<128> > in a dictionary of sorts? Can I wrap the vector object in an id type somehow? Even if it's not a direct dictionary, is there another method I could use? I also need this to be mutable, so I can add key/object's at runtime.

I'v seen std::map<std::string, std::string>, but I'm not sure if it's what I'm looking for. Nor have I found any examples on it being mutable.

Does anyone have any idea how to achieve this?

Brett
  • 11,637
  • 34
  • 127
  • 213

2 Answers2

5

Actually NSValue seems to be the solution. @Taum's advice is correct for some cases (when you store your objects somewhere and sure your pointers are working). In case you've created some object and it's lifetime is limited, but you need to push this object further, use another NSValue method

std::vector<std::bitset<128>> cppVector;
NSValue *value=[NSValue valueWithBytes:&cppVector objCType:@encode(std::vector<std::bitset<128>>)];

When you need to get this value back:

std::vector<std::bitset<128>> cppVector;
[value getValue:&cppVector];

That works perfectly for simple structs too.

Daniyar
  • 2,975
  • 2
  • 26
  • 39
  • I can get first object but next I get: `malloc: *** error for object 0x16f9a8288: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug` – user924 Mar 26 '18 at 10:34
  • @user924 look at the place where `next` object was created, it was free'd somewhere further. – Daniyar Mar 27 '18 at 11:06
  • @Astoria they all created in one function – user924 Mar 27 '18 at 11:24
  • @user924 probably two cases: 1. It was already released. 2. It wasn't allocated. – Daniyar Mar 29 '18 at 08:10
1

You could store a pointer to it in a NSValue object. You'll just have to be careful about what owns the std::vector and when it should be free'd.

Store with:

std::vector<std::bitset<128>> *cppVector = myCppObject->methodReturningVector();
NSValue *value = [NSValue valueWithPointer:cppVector];
[myObjcDictionary setObject:value forKey:@"myKey"];

Get C++ object back with:

NSValue *value = [myObjcDictionary objectForKey:@"myKey"];
std::vector<std::bitset<128>> *cppVector = [value pointerValue]
Taum
  • 2,511
  • 18
  • 18
  • My C++/ObjC++ is a little rusty but I edited the answer to try and give some code examples. Hope that helps. – Taum Jan 31 '14 at 20:22
  • Unfortunately this doesn't seem to be working. I don't have a pointer coming back for my `cppVector` like your example. So I can fix it by using the line `NSValue *value = [NSValue valueWithPointer:&cppVector]` but then I can't seem to retrieve it using `[value pointerValue]`. Any ideas? – Brett Jan 31 '14 at 20:42