If we want to add our own extensions to APLevelDB or the other wrappers, the internal leveldb::DB*
has to be exposed. Just getting that pointer is enough to be able use all the C++ logic we saw in earlier chapters. This requires a minor change to APLevelDB.h
.
Firstly declare a return type for getDB
which can be safely included in pure Objective-C so we don't force people to move to Objective-C++ with .mm
files:
#ifdef __cplusplus // forward declaration namespace leveldb { class DB; } typedef leveldb::DB* leveldbDBPtr; #else typedef void* leveldbDBPtr; #endif
Then inside @interface APLevelDB : NSObject
add a public getter:
- (leveldbDBPtr) getDB;
This is simply implemented in ApLevelDB.mm
:
- (leveldbDBPtr) getDB { return _db; }
Now we can add a
class category to extend APLevelDB
with other methods. Just one is shown here. We declare a method taking a prefix string and applying a block to the keys which match that prefix, passing a BOOL
parameter...