serializing C++ into Cocoa objects to send over DO

Apprentice
Posts: 8
Joined: 2007.02
Post: #1
I've slowly been wrestling with this problem and am getting closer.

I'm trying to serialize a C++ object into a standard cocoa object so that I can send it over DO. As I understand it, if I can write my C++ objects into a byte buffer, I can create an NSArray of NSData objects and tell the encoder inside encodeWithCoder: to encode the array and all the objects will be encoded automatically, and send those over DO.

I have a PatternWrapper class that encapsulates a single c++ pattern and does encode/decode. Then I will stick those in NSArray and send it over DO.

My problem is the encodeWithCoder: and initWithCoder: methods, I'm not sure how to get my C++ object in them.

Can anyone help out here?

---
using namespace TX;

@interface PatternWrapper : NSObject {
TX::Pattern *pattern;
}

- (id)initWithPattern:(TX::Pattern *)nativePattern;
- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder;
@end


@implementation PatternWrapper

- (id)initWithPattern:(TX::Pattern *)nativePattern
{
if (self) {
pattern = new TX::Pattern(nativePattern);
}

return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
[super encodeWithCoder:encoder];
// ???
}

- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];

if (self) {
pattern = new TX::Pattern();
// ???
}

return self;
}

---

Any help much appreciated.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #2
AFAIK you can't directly serialize C++ object via Cocoa. Instead you would create a new NSObject that's key-value compliant and the use NSValue and NSNumber to convert C types to NSObject types.

Example:

typedef struct Pattern {
int type;
char *name;
CustomObject extras;
}

The easiest way to serialize this is by using an NSDictionary:

Suppose you have: Pattern *pattern = new Pattern();

myPattern = [NSMutableDictionary dictionary];
[myPattern setObject:[NSNumber numberWithInt:pattern->type] forKey:@"type"]];
...

as for adding the extras field you would do
myPatternExtras = [NSMutableDictionary dictionary];
[myPatternExtras setObject:[NSString stringWithCString:pattern->extras.name] forKey:@"name"];
...

[myPattern setObject:myPatternExtras forKey:@"extras"];

etc..

"When you dream, there are no rules..."
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2007.02
Post: #3
Why not? I should be able to serialize it from C++ into cocoa, or ... indirectly using native C++ methods to store the data into cocoa objects, and then have it encode/decode via the standard encodeWithCoder: and initWithCoder: methods.

My TX::Pattern classes (c++) are quite a bit more complicated than a simple key/value or name, type, so I think I need to really to serialize it into a cocoa object ... like NSData or NSString, and then put several hundreds of these into an NSArray and use the encoders.

Or maybe I didn't quite understand what you were saying if that's not what you meant. I just don't know how to serialize natively in c++ and pass this to my cocoa objects.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  NSStream Send & Receive Handler jasonjohnson 3 6,956 Sep 29, 2005 09:14 AM
Last Post: jasonjohnson
  BSD Socket: Send Data but never Receive kodex 2 7,864 Jul 1, 2005 10:56 PM
Last Post: kodex