From 4c0ac4319f13030b51767ed41c8aaea7ec4afe74 Mon Sep 17 00:00:00 2001
From: Yakov Markovitch <ymarkovitch@gmail.com>
Date: Mon, 15 Aug 2011 21:44:10 +0400
Subject: [PATCH] Made most of the GridFS and GridFile member functions 'const'.

---
 client/gridfs.cpp |   20 ++++++++++----------
 client/gridfs.h   |   46 +++++++++++++++++++++++-----------------------
 2 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/client/gridfs.cpp b/client/gridfs.cpp
index 233724a..449cb40 100644
--- a/client/gridfs.cpp
+++ b/client/gridfs.cpp
@@ -169,29 +169,29 @@ namespace mongo {
         }
     }
 
-    GridFile::GridFile( GridFS * grid , BSONObj obj ) {
+    GridFile::GridFile(const GridFS * grid , BSONObj obj ) {
         _grid = grid;
         _obj = obj;
     }
 
-    GridFile GridFS::findFile( const string& fileName ) {
+    GridFile GridFS::findFile( const string& fileName ) const {
         return findFile( BSON( "filename" << fileName ) );
     };
 
-    GridFile GridFS::findFile( BSONObj query ) {
+    GridFile GridFS::findFile( BSONObj query ) const {
         query = BSON("query" << query << "orderby" << BSON("uploadDate" << -1));
         return GridFile( this , _client.findOne( _filesNS.c_str() , query ) );
     }
 
-    auto_ptr<DBClientCursor> GridFS::list() {
+    auto_ptr<DBClientCursor> GridFS::list() const {
         return _client.query( _filesNS.c_str() , BSONObj() );
     }
 
-    auto_ptr<DBClientCursor> GridFS::list( BSONObj o ) {
+    auto_ptr<DBClientCursor> GridFS::list( BSONObj o ) const {
         return _client.query( _filesNS.c_str() , o );
     }
 
-    BSONObj GridFile::getMetadata() {
+    BSONObj GridFile::getMetadata() const {
         BSONElement meta_element = _obj["metadata"];
         if( meta_element.eoo() ) {
             return BSONObj();
@@ -200,7 +200,7 @@ namespace mongo {
         return meta_element.embeddedObject();
     }
 
-    GridFSChunk GridFile::getChunk( int n ) {
+    GridFSChunk GridFile::getChunk( int n ) const {
         _exists();
         BSONObjBuilder b;
         b.appendAs( _obj["_id"] , "files_id" );
@@ -211,7 +211,7 @@ namespace mongo {
         return GridFSChunk(o);
     }
 
-    gridfs_offset GridFile::write( ostream & out ) {
+    gridfs_offset GridFile::write( ostream & out ) const {
         _exists();
 
         const int num = getNumChunks();
@@ -227,7 +227,7 @@ namespace mongo {
         return getContentLength();
     }
 
-    gridfs_offset GridFile::write( const string& where ) {
+    gridfs_offset GridFile::write( const string& where ) const {
         if (where == "-") {
             return write( cout );
         }
@@ -238,7 +238,7 @@ namespace mongo {
         }
     }
 
-    void GridFile::_exists() {
+    void GridFile::_exists() const {
         uassert( 10015 ,  "doesn't exists" , exists() );
     }
 
diff --git a/client/gridfs.h b/client/gridfs.h
index b52cf75..5a19aa1 100644
--- a/client/gridfs.h
+++ b/client/gridfs.h
@@ -32,13 +32,13 @@ namespace mongo {
         GridFSChunk( BSONObj data );
         GridFSChunk( BSONObj fileId , int chunkNumber , const char * data , int len );
 
-        int len() {
+        int len() const {
             int len;
             _data["data"].binDataClean( len );
             return len;
         }
 
-        const char * data( int & len ) {
+        const char * data( int & len ) const {
             return _data["data"].binDataClean( len );
         }
 
@@ -100,22 +100,22 @@ namespace mongo {
         /**
          * returns a file object matching the query
          */
-        GridFile findFile( BSONObj query );
+        GridFile findFile( BSONObj query ) const;
 
         /**
          * equiv to findFile( { filename : filename } )
          */
-        GridFile findFile( const string& fileName );
+        GridFile findFile( const string& fileName ) const;
 
         /**
          * convenience method to get all the files
          */
-        auto_ptr<DBClientCursor> list();
+        auto_ptr<DBClientCursor> list() const;
 
         /**
          * convenience method to get all the files with a filter
          */
-        auto_ptr<DBClientCursor> list( BSONObj query );
+        auto_ptr<DBClientCursor> list( BSONObj query ) const;
 
     private:
         DBClientBase& _client;
@@ -140,63 +140,63 @@ namespace mongo {
          * @return whether or not this file exists
          * findFile will always return a GriFile, so need to check this
          */
-        bool exists() {
+        bool exists() const {
             return ! _obj.isEmpty();
         }
 
-        string getFilename() {
+        string getFilename() const {
             return _obj["filename"].str();
         }
 
-        int getChunkSize() {
+        int getChunkSize() const {
             return (int)(_obj["chunkSize"].number());
         }
 
-        gridfs_offset getContentLength() {
+        gridfs_offset getContentLength() const {
             return (gridfs_offset)(_obj["length"].number());
         }
 
-        string getContentType() {
+        string getContentType() const {
             return _obj["contentType"].valuestr();
         }
 
-        Date_t getUploadDate() {
+        Date_t getUploadDate() const {
             return _obj["uploadDate"].date();
         }
 
-        string getMD5() {
+        string getMD5() const {
             return _obj["md5"].str();
         }
 
-        BSONElement getFileField( const string& name ) {
+        BSONElement getFileField( const string& name ) const {
             return _obj[name];
         }
 
-        BSONObj getMetadata();
+        BSONObj getMetadata() const;
 
-        int getNumChunks() {
+        int getNumChunks() const {
             return (int) ceil( (double)getContentLength() / (double)getChunkSize() );
         }
 
-        GridFSChunk getChunk( int n );
+        GridFSChunk getChunk( int n ) const;
 
         /**
            write the file to the output stream
          */
-        gridfs_offset write( ostream & out );
+        gridfs_offset write( ostream & out ) const;
 
         /**
            write the file to this filename
          */
-        gridfs_offset write( const string& where );
+        gridfs_offset write( const string& where ) const;
 
     private:
-        GridFile( GridFS * grid , BSONObj obj );
+        GridFile(const GridFS * grid , BSONObj obj );
 
-        void _exists();
+        void _exists() const;
 
-        GridFS * _grid;
-        BSONObj _obj;
+        const GridFS * _grid;
+        BSONObj        _obj;
 
         friend class GridFS;
     };
-- 
1.7.3.4

