views:

29

answers:

3

I'm building an Objective-C wrapper for a nasty legacy C-library. Assume I have two Objective-C classes A and B wrapping some related legacy structures. Now I would like to provide a function that converts a given object of class A into an object of class B. The problem is, that the conversion process destroys the internal state of the source object.

What is the best solution for implementing this conversion function regarding the standard Objective-C naming convention. I.e. it should be clear that the conversion function will do a release on the given source object.

A: 

Instead of asking about a name, perhaps you should ask for strategies on how to do the conversion process without destroying the state of the source object. I can't imagine any sort of C data that can't just by memcpy'd to a new location. I'd fiddle around with doing that and seeing if duplicating the data before conversion will fix this issue for you.

Dave DeLong
The "structure" is a rather complicated set of objects with pointer references. Duplicating is possible, but more or less "expensive". Also in most cases the source object will be destroyed anyways after the conversion.However. It seems that I have to go that way.
Halbanonym
OK, you are right! It bugged me anyways that it wasn't possible to create multiple B objects from a single A object while its state changes over time. Internal duplication is the best way around the whole problem.
Halbanonym
A: 

releasing the source is not a good idea I believe. Why don't you make a copy of your source and release that (if that is neccesary). In that case you could call you method

MyObjectB * b = [MyObjectB bFromA:(MyObjectA *)a];

or

MyObjectB * b = [[MyObjectB alloc] initFromA:(MyObjectA *)a];

I guess ;)

nacho4d
A: 

There is no such naming convention. All the Objective-C methods/functions I've seen in standard APIs either copy their sources, leaving original source intact, or use original, but do not destroy it.

The naming convention that does exist, persists to whether the caller should release the new object.

Piotr Kalinowski