Wed, 09 Aug 2006
C++ Wrapper for libsndfile, Part 3.
Thinking about the C++ wrapper continues. In the current candidate version, a SndfileHandle contains a pointer to a private reference counted struct which contains the actual data. The SndfileHandle class also has a copy constructor and an assignment operator.
In addition to the above, a number of people on the mailing list have asked for the SndfileHandle class to have open() and close() methods. This seems reasonable on the face of it, but Daniel Schmitt points out that the combination of copy/assign and open/close results in a rather strange ambiguity.
Daniel gives the following example using copy/assign (ie no open/close methods):
SndfileHandle file1 ("foo.wav") ; // Make file2 == file1 SndfileHandle file2 = file1 ; // Now reuse file1 file1 = SndfileHandle("bar.wav");
At the end of that block of code we now have file1 and file2 operating on different handles, which is exactly what any reasonable programmer would expect.
Now look at what happens if we have open/close methods:
SndfileHandle file1 ("foo.wav") ; // Make file2 == file1 SndfileHandle file2 = file1 ; // Now reuse file1 file1.open ("bar.wav") ;
The open method can be implemented in one of the following two ways :
- Decrease the reference count on what file1 used to operate on and then initialize a one.
- Modify the data that both SndfileHandles point to.
After the block of code above, the two different implementations would result in the following state :
- file1 and file2 refer to different handles
- file2 now refers to "bar.wav" which is even worse
Obviously, the second implementation is completely wrong, but the first implementation is at least questionable. In terms of providing something which balances utility and consistency I'm tending to favor the idea of keeping the copy/assign operations and not providing open/close methods.
Posted at: 20:20 | Category: CodeHacking/libsndfile | Permalink