Permalink
Browse files

API cleanup: enable better compile-time checks

* Use 'instancetype' instead of 'id' as return type where appropriate.
* Add 'nonnull' attribute, to flag pointer params that can't be null
  (only in public API and CBLMisc so far)
  • Loading branch information...
1 parent 81223f6 commit 320f8f761ef159815c4a6a89e832d3897aaee2d8 @snej snej committed Feb 5, 2013
Showing with 348 additions and 328 deletions.
  1. +1 −1 Demo-Mac/DemoQuery.h
  2. +1 −1 Demo-Mac/DemoQuery.m
  3. +1 −1 Demo-iOS/ConfigViewController.m
  4. +1 −1 Listener/CBLHTTPResponse.h
  5. +1 −1 Listener/CBLHTTPResponse.m
  6. +1 −1 Listener/CBLListener.h
  7. +1 −1 Listener/CBLListener.m
  8. +6 −3 Source/API/CBLAttachment.h
  9. +7 −7 Source/API/CBLAttachment.m
  10. +13 −11 Source/API/CBLDatabase.h
  11. +2 −2 Source/API/CBLDatabase.m
  12. +3 −3 Source/API/CBLDocument.h
  13. +2 −2 Source/API/CBLDocument.m
  14. +12 −10 Source/API/CBLManager.h
  15. +8 −7 Source/API/CBLManager.m
  16. +11 −11 Source/API/CBLModel.h
  17. +4 −4 Source/API/CBLModel.m
  18. +5 −5 Source/API/CBLModelFactory.h
  19. +2 −2 Source/API/CBLModelFactory.m
  20. +8 −8 Source/API/CBLQuery.m
  21. +1 −1 Source/API/CBLReplication.h
  22. +4 −4 Source/API/CBLReplication.m
  23. +6 −5 Source/API/CBLRevision.h
  24. +4 −4 Source/API/CBLRevision.m
  25. +3 −3 Source/API/CBLUITableSource.h
  26. +1 −1 Source/API/CBLUITableSource.m
  27. +7 −4 Source/API/CBLView.h
  28. +9 −9 Source/API/CBLView.m
  29. +27 −21 Source/API/CouchbaseLitePrivate.h
  30. +6 −6 Source/CBLAuthorizer.h
  31. +6 −7 Source/CBLAuthorizer.m
  32. +3 −3 Source/CBLBatcher.h
  33. +4 −3 Source/CBLBatcher.m
  34. +1 −1 Source/CBLBrowserIDAuthorizer.h
  35. +1 −1 Source/CBLBrowserIDAuthorizer.m
  36. +2 −2 Source/CBLCache.h
  37. +2 −2 Source/CBLCache.m
  38. +1 −1 Source/CBLCanonicalJSON.h
  39. +1 −1 Source/CBLCanonicalJSON.m
  40. +1 −1 Source/CBLGNUstep.m
  41. +7 −7 Source/CBLInternal.h
  42. +3 −3 Source/CBLJSFunction.h
  43. +4 −4 Source/CBLJSFunction.m
  44. +1 −1 Source/CBLJSON.h
  45. +1 −1 Source/CBLJSON.m
  46. +1 −1 Source/CBLJSViewCompiler.m
  47. +11 −11 Source/CBLMisc.h
  48. +3 −3 Source/CBLMultiStreamWriter.m
  49. +1 −1 Source/CBLMultipartDocumentReader.h
  50. +1 −1 Source/CBLMultipartDocumentReader.m
  51. +4 −4 Source/CBLMultipartDownloader.h
  52. +4 −4 Source/CBLMultipartDownloader.m
  53. +2 −2 Source/CBLMultipartReader.h
  54. +2 −2 Source/CBLMultipartReader.m
  55. +4 −4 Source/CBLMultipartUploader.h
  56. +4 −4 Source/CBLMultipartUploader.m
  57. +1 −1 Source/CBLMultipartWriter.h
  58. +1 −1 Source/CBLMultipartWriter.m
  59. +5 −5 Source/CBLOAuth1Authorizer.h
  60. +5 −5 Source/CBLOAuth1Authorizer.m
  61. +1 −1 Source/CBLReachability.h
  62. +1 −1 Source/CBLReachability.m
  63. +5 −5 Source/CBLRemoteRequest.h
  64. +5 −5 Source/CBLRemoteRequest.m
  65. +1 −1 Source/CBLSequenceMap.h
  66. +1 −1 Source/CBLSequenceMap.m
  67. +1 −1 Source/CBL_Attachment.h
  68. +1 −1 Source/CBL_Attachment.m
  69. +2 −2 Source/CBL_BlobStore.h
  70. +2 −2 Source/CBL_BlobStore.m
  71. +5 −5 Source/CBL_Body.h
  72. +5 −5 Source/CBL_Body.m
  73. +6 −6 Source/CBL_Database+Insertion.m
  74. +1 −1 Source/CBL_Database.h
  75. +2 −2 Source/CBL_Database.m
  76. +2 −2 Source/CBL_DatabaseChange.h
  77. +2 −2 Source/CBL_DatabaseChange.m
  78. +4 −4 Source/CBL_Replicator.h
  79. +4 −4 Source/CBL_Replicator.m
  80. +1 −1 Source/CBL_ReplicatorManager.h
  81. +1 −1 Source/CBL_ReplicatorManager.m
  82. +8 −8 Source/CBL_Revision.h
  83. +8 −8 Source/CBL_Revision.m
  84. +3 −1 Source/CBL_Router.h
  85. +5 −6 Source/CBL_Router.m
  86. +4 −4 Source/CBL_Server.h
  87. +6 −6 Source/CBL_Server.m
  88. +2 −2 Source/CBL_View.h
  89. +3 −3 Source/CBL_View.m
  90. +5 −5 Source/ChangeTracker/CBLChangeTracker.h
  91. +6 −5 Source/ChangeTracker/CBLChangeTracker.m
View
@@ -29,7 +29,7 @@
Class _modelClass;
}
-- (id) initWithQuery: (CBLQuery*)query modelClass: (Class)modelClass;
+- (instancetype) initWithQuery: (CBLQuery*)query modelClass: (Class)modelClass;
/** The documents returned by the query, wrapped in DemoItem objects.
An NSArrayController can be bound to this property. */
View
@@ -25,7 +25,7 @@ - (void) loadEntriesFrom: (CBLQueryEnumerator*)rows;
@implementation DemoQuery
-- (id) initWithQuery: (CBLQuery*)query modelClass: (Class)modelClass {
+- (instancetype) initWithQuery: (CBLQuery*)query modelClass: (Class)modelClass {
NSParameterAssert(query);
self = [super init];
if (self != nil) {
@@ -25,7 +25,7 @@ @implementation ConfigViewController
@synthesize urlField, versionField, autoSyncSwitch;
-- (id)init {
+- (instancetype) init {
self = [super initWithNibName: @"ConfigViewController" bundle: nil];
if (self) {
// Custom initialization
@@ -25,7 +25,7 @@
UInt64 _offset; // Offset in response for next readData
}
-- (id) initWithRouter: (CBL_Router*)router forConnection:(CBLHTTPConnection*)connection;
+- (instancetype) initWithRouter: (CBL_Router*)router forConnection:(CBLHTTPConnection*)connection;
@property UInt64 offset;
@@ -33,7 +33,7 @@ - (void) onFinished;
@implementation CBLHTTPResponse
-- (id) initWithRouter: (CBL_Router*)router forConnection:(CBLHTTPConnection*)connection {
+- (instancetype) initWithRouter: (CBL_Router*)router forConnection:(CBLHTTPConnection*)connection {
self = [super init];
if (self) {
//EnableLog(YES);
View
@@ -16,7 +16,7 @@
/** Initializes a CBLListener.
@param manager The CBLManager whose databases to serve.
@param port The TCP port number to listen on. Use 0 to automatically pick an available port (you can get the port number after the server starts by getting the .port property.) */
-- (id) initWithManager: (CBLManager*)manager port: (UInt16)port;
+- (instancetype) initWithManager: (CBLManager*)manager port: (UInt16)port;
/** The TCP port number that the listener is listening on.
If the listener has not yet started, this will return 0. */
View
@@ -36,7 +36,7 @@ @implementation CBLListener
@synthesize readOnly=_readOnly, requiresAuth=_requiresAuth, realm=_realm;
-- (id) initWithManager: (CBLManager*)manager port: (UInt16)port {
+- (instancetype) initWithManager: (CBLManager*)manager port: (UInt16)port {
self = [super init];
if (self) {
_tdServer = manager.backgroundServer;
@@ -19,8 +19,8 @@
This object can then be added as a value in a new revision's _attachments dictionary; it will be converted to JSON when saved.
@param contentType The MIME type
@param body The attachment body; this can either be an NSData object, or an NSURL pointing to a (local) file. */
-- (id) initWithContentType: (NSString*)contentType
- body: (id)body;
+- (instancetype) initWithContentType: (NSString*)contentType
+ body: (id)body __attribute__((nonnull));
/** The owning document revision. */
@property (readonly, retain) CBLRevisionBase* revision;
@@ -48,7 +48,10 @@
@property (readonly) NSURL* bodyURL;
/** Updates the body, creating a new document revision in the process.
- If all you need to do to a document is update a single attachment this is an easy way to do it; but if you need to change multiple attachments, or change other body properties, do them in one step by calling -putProperties:error: on the revision or document. */
+ If all you need to do to a document is update a single attachment this is an easy way to do it; but if you need to change multiple attachments, or change other body properties, do them in one step by calling -putProperties:error: on the revision or document.
+ @param body The new body, or nil to delete the attachment.
+ @param contentType The new content type, or nil to leave it the same.
+ @param outError On return, the error (if any). */
- (CBLRevision*) updateBody: (NSData*)body
contentType: (NSString*)contentType
error: (NSError**)outError;
View
@@ -23,9 +23,9 @@ @implementation CBLAttachment
}
-- (id) initWithRevision: (CBLRevisionBase*)rev
- name: (NSString*)name
- metadata: (NSDictionary*)metadata
+- (instancetype) initWithRevision: (CBLRevisionBase*)rev
+ name: (NSString*)name
+ metadata: (NSDictionary*)metadata
{
NSParameterAssert(rev);
NSParameterAssert(name);
@@ -40,8 +40,8 @@ - (id) initWithRevision: (CBLRevisionBase*)rev
}
-- (id) initWithContentType: (NSString*)contentType
- body: (id)body
+- (instancetype) initWithContentType: (NSString*)contentType
+ body: (id)body
{
NSParameterAssert(contentType);
NSParameterAssert(body);
@@ -131,8 +131,8 @@ - (NSURL*) bodyURL {
- (CBLRevision*) updateBody: (NSData*)body
- contentType: (NSString*)contentType
- error: (NSError**)outError
+ contentType: (NSString*)contentType
+ error: (NSError**)outError
{
Assert(_rev);
CBL_BlobStoreWriter* writer = body ? blobStoreWriterForBody(_rev.database.tddb, body) : nil;
View
@@ -72,17 +72,17 @@ typedef BOOL (^CBLFilterBlock) (CBLRevision* revision, NSDictionary* params);
Doesn't touch the on-disk database; a document with that ID doesn't even need to exist yet.
CBLDocuments are cached, so there will never be more than one instance (in this database)
at a time with the same documentID. */
-- (CBLDocument*) documentWithID: (NSString*)docID;
+- (CBLDocument*) documentWithID: (NSString*)docID __attribute__((nonnull));
/** Same as -documentWithID:. Enables "[]" access in Xcode 4.4+ */
-- (CBLDocument*)objectForKeyedSubscript: (NSString*)key;
+- (CBLDocument*)objectForKeyedSubscript: (NSString*)key __attribute__((nonnull));
/** Creates a CBLDocument object with no current ID.
The first time you PUT to that document, it will be created on the server (via a POST). */
- (CBLDocument*) untitledDocument;
/** Returns the already-instantiated cached CBLDocument with the given ID, or nil if none is yet cached. */
-- (CBLDocument*) cachedDocumentWithID: (NSString*)docID;
+- (CBLDocument*) cachedDocumentWithID: (NSString*)docID __attribute__((nonnull));
/** Empties the cache of recently used CBLDocument objects.
API calls will now instantiate and return new instances. */
@@ -99,24 +99,26 @@ typedef BOOL (^CBLFilterBlock) (CBLRevision* revision, NSDictionary* params);
anonymous CBLView and then deleting it immediately after querying it. It may be useful during
development, but in general this is inefficient if this map will be used more than once,
because the entire view has to be regenerated from scratch every time. */
-- (CBLQuery*) slowQueryWithMap: (CBLMapBlock)mapBlock;
+- (CBLQuery*) slowQueryWithMap: (CBLMapBlock)mapBlock __attribute__((nonnull));
/** Returns a CBLView object for the view with the given name.
(This succeeds even if the view doesn't already exist, but the view won't be added to the database until the CBLView is assigned a map function.) */
-- (CBLView*) viewNamed: (NSString*)name;
+- (CBLView*) viewNamed: (NSString*)name __attribute__((nonnull));
/** An array of all existing views. */
@property (readonly) NSArray* allViews;
/** Defines or clears a named document validation function.
Before any change to the database, all registered validation functions are called and given a
chance to reject it. (This includes incoming changes from a pull replication.) */
-- (void) defineValidation: (NSString*)validationName asBlock: (CBLValidationBlock)validationBlock;
+- (void) defineValidation: (NSString*)validationName asBlock: (CBLValidationBlock)validationBlock
+ __attribute__((nonnull(1)));
/** Defines or clears a named filter function.
Filters are used by push replications to choose which documents to send. */
-- (void) defineFilter: (NSString*)filterName asBlock: (CBLFilterBlock)filterBlock;
+- (void) defineFilter: (NSString*)filterName asBlock: (CBLFilterBlock)filterBlock
+ __attribute__((nonnull(1)));
/** Registers an object that can compile source code into executable filter blocks. */
@@ -128,7 +130,7 @@ typedef BOOL (^CBLFilterBlock) (CBLRevision* revision, NSDictionary* params);
/** Runs the block within a transaction. If the block returns NO, the transaction is rolled back.
Use this when performing bulk operations like multiple inserts/updates; it saves the overhead of multiple SQLite commits. */
-- (BOOL) inTransaction: (BOOL(^)(void))block;
+- (BOOL) inTransaction: (BOOL(^)(void))bloc __attribute__((nonnull(1)));
#pragma mark - REPLICATION:
@@ -138,14 +140,14 @@ typedef BOOL (^CBLFilterBlock) (CBLRevision* revision, NSDictionary* params);
/** Creates a replication that will 'push' to a database at the given URL.
It will initially be non-persistent; set its .persistent property to YES to make it persist. */
-- (CBLReplication*) pushToURL: (NSURL*)url;
+- (CBLReplication*) pushToURL: (NSURL*)url __attribute__((nonnull));
/** Creates a replication that will 'pull' from a database at the given URL.
It will initially be non-persistent; set its .persistent property to YES to make it persist. */
-- (CBLReplication*) pullFromURL: (NSURL*)url;
+- (CBLReplication*) pullFromURL: (NSURL*)url __attribute__((nonnull));
/** Creates a pair of replications to both pull and push to database at the given URL.
- @param otherDbURL The URL of the remote database.
+ @param otherDbURL The URL of the remote database, or nil for none.
@param exclusively If YES, any previously existing replications to or from otherDbURL will be deleted.
@return An array whose first element is the "pull" replication and second is the "push".
It will initially be non-persistent; set its .persistent property to YES to make it persist. */
View
@@ -39,8 +39,8 @@ @implementation CBLDatabase
@synthesize tddb=_tddb, manager=_manager, unsavedModelsMutable=_unsavedModelsMutable;
-- (id) initWithManager: (CBLManager*)manager
- CBL_Database: (CBL_Database*)tddb
+- (instancetype) initWithManager: (CBLManager*)manager
+ CBL_Database: (CBL_Database*)tddb
{
self = [super init];
if (self) {
View
@@ -75,10 +75,10 @@
@property (readonly, copy) NSDictionary* userProperties;
/** Shorthand for [self.properties objectForKey: key]. */
-- (id) propertyForKey: (NSString*)key;
+- (id) propertyForKey: (NSString*)key __attribute__((nonnull));
/** Same as -propertyForKey:. Enables "[]" access in Xcode 4.4+ */
-- (id)objectForKeyedSubscript:(NSString*)key;
+- (id)objectForKeyedSubscript:(NSString*)key __attribute__((nonnull));
/** Saves a new revision. The properties dictionary must have a "_rev" property whose ID matches the current revision's (as it will if it's a modified copy of this document's .properties property.) */
- (CBLRevision*) putProperties: (NSDictionary*)properties error: (NSError**)outError;
@@ -99,7 +99,7 @@
/** Protocol that CBLDocument model objects must implement. See the CBLModel class. */
@protocol CBLDocumentModel <NSObject>
/** If a CBLDocument's modelObject implements this method, it will be called whenever the document posts a kCBLDocumentChangeNotification. */
-- (void) tdDocumentChanged: (CBLDocument*)doc;
+- (void) tdDocumentChanged: (CBLDocument*)doc __attribute__((nonnull));
@end
View
@@ -32,8 +32,8 @@ @implementation CBLDocument
#endif
-- (id)initWithDatabase: (CBLDatabase*)database
- documentID: (NSString*)docID
+- (instancetype) initWithDatabase: (CBLDatabase*)database
+ documentID: (NSString*)docID
{
self = [super init];
if (self) {
View
@@ -25,22 +25,23 @@ typedef struct CBLManagerOptions {
@interface CBLManager : NSObject <NSCopying>
/** A shared per-process instance. This should only be used on the main thread. */
-+ (CBLManager*) sharedInstance;
++ (instancetype) sharedInstance;
-+ (BOOL) isValidDatabaseName: (NSString*)name;
++ (BOOL) isValidDatabaseName: (NSString*)name __attribute__((nonnull));
+ (NSString*) defaultDirectory;
/** Default initializer. Stores databases in the default Application Support directory. */
-- (id)init;
+- (instancetype) init;
/** Initializes a CouchbaseLite manager that stores its data at the given path.
@param directory Path to data directory. If it doesn't already exist it will be created.
@param options If non-NULL, a pointer to options (read-only and no-replicator).
@param outError On return, the error if any. */
-- (id) initWithDirectory: (NSString*)directory
- options: (const CBLManagerOptions*)options
- error: (NSError**)outError;
+- (instancetype) initWithDirectory: (NSString*)directory
+ options: (const CBLManagerOptions*)options
+ error: (NSError**)outError
+ __attribute__((nonnull(1)));
/** Releases all resources used by the CBLManager instance and closes all its databases. */
@@ -50,15 +51,16 @@ typedef struct CBLManagerOptions {
/** Returns the database with the given name, or nil if it doesn't exist.
Multiple calls with the same name will return the same CouchDatabase instance. */
-- (CBLDatabase*) databaseNamed: (NSString*)name;
+- (CBLDatabase*) databaseNamed: (NSString*)name __attribute__((nonnull));
/** Same as -databaseNamed:. Enables "[]" access in Xcode 4.4+ */
-- (CBLDatabase*) objectForKeyedSubscript: (NSString*)key;
+- (CBLDatabase*) objectForKeyedSubscript: (NSString*)key __attribute__((nonnull));
/** Returns the database with the given name, creating it if it didn't already exist.
Multiple calls with the same name will return the same CouchDatabase instance.
NOTE: Database names may not contain capital letters! */
-- (CBLDatabase*) createDatabaseNamed: (NSString*)name error: (NSError**)outError;
+- (CBLDatabase*) createDatabaseNamed: (NSString*)name error: (NSError**)outError
+ __attribute__((nonnull(1)));
/** An array of the names of all existing databases. */
@property (readonly) NSArray* allDatabaseNames;
@@ -79,7 +81,7 @@ typedef struct CBLManagerOptions {
- (BOOL) replaceDatabaseNamed: (NSString*)databaseName
withDatabaseFile: (NSString*)databasePath
withAttachments: (NSString*)attachmentsPath
- error: (NSError**)outError;
+ error: (NSError**)outError __attribute__((nonnull(1,2)));
@end
View
@@ -64,7 +64,7 @@ + (NSString*) defaultDirectory {
}
-+ (CBLManager*) sharedInstance {
++ (instancetype) sharedInstance {
static CBLManager* sInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@@ -74,7 +74,7 @@ + (CBLManager*) sharedInstance {
}
-- (id) init {
+- (instancetype) init {
NSError* error;
self = [self initWithDirectory: [[self class] defaultDirectory]
options: NULL
@@ -85,9 +85,10 @@ - (id) init {
}
-- (id) initWithDirectory: (NSString*)directory
- options: (const CBLManagerOptions*)options
- error: (NSError**)outError {
+- (instancetype) initWithDirectory: (NSString*)directory
+ options: (const CBLManagerOptions*)options
+ error: (NSError**)outError
+{
if (outError) *outError = nil;
self = [super init];
if (self) {
@@ -121,7 +122,7 @@ - (id) initWithDirectory: (NSString*)directory
#if DEBUG
-+ (CBLManager*) createEmptyAtPath: (NSString*)path {
++ (instancetype) createEmptyAtPath: (NSString*)path {
[[NSFileManager defaultManager] removeItemAtPath: path error: NULL];
NSError* error;
CBLManager* dbm = [[self alloc] initWithDirectory: path
@@ -132,7 +133,7 @@ + (CBLManager*) createEmptyAtPath: (NSString*)path {
return dbm;
}
-+ (CBLManager*) createEmptyAtTemporaryPath: (NSString*)name {
++ (instancetype) createEmptyAtTemporaryPath: (NSString*)name {
return [self createEmptyAtPath: [NSTemporaryDirectory() stringByAppendingPathComponent: name]];
}
#endif
Oops, something went wrong.

0 comments on commit 320f8f7

Please sign in to comment.