Google APIs Client Library for C++
data_writer.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 
00021 #ifndef APISERVING_CLIENTS_CPP_DATA_DATA_WRITER_H_
00022 #define APISERVING_CLIENTS_CPP_DATA_DATA_WRITER_H_
00023 
00024 #include <string>
00025 using std::string;
00026 #include "googleapis/base/callback.h"
00027 #include "googleapis/base/integral_types.h"
00028 #include "googleapis/strings/stringpiece.h"
00029 #include "googleapis/util/status.h"
00030 namespace googleapis {
00031 
00032 class FileOpenOptions;
00033 
00034 namespace client {
00035 class DataReader;
00036 
00037 /*
00038  * Interface for writing into synchronous binary data stream.
00039  * @ingroup DataLayerRaw
00040  *
00041  * The DataWriter is the base class for writing non-trivial data using
00042  * a streaming-like interface. Writers can act as reader factories if you
00043  * need to read-back what had been written from within the same process.
00044  * For example streaming HTTP responses into files then reading them back
00045  * to handle them.
00046  *
00047  * @see NewStringDataWriter
00048  * @see NewFileDataWriter
00049  */
00050 // TODO(ewiseblatt): 20130418
00051 // Consider adding << operator
00052 class DataWriter {
00053  public:
00054   /*
00055    * Standard destructor.
00056    */
00057   virtual ~DataWriter();
00058 
00059   /*
00060    * Returns the number of bytes written into the stream.
00061    */
00062   int64 size() const { return size_; }
00063 
00064   /*
00065    * Determine if we've encountered a hard error or not.
00066    *
00067    * @return true if no hard error has been set, false if an error.
00068    * @see error
00069    */
00070   bool ok() const                      { return status_.ok(); }
00071 
00072   /*
00073    * Returns details for the error on the stream, if any.
00074    *
00075    * @return a successful status if the stream is ok, otherwise the
00076    * error encounteredd.
00077    */
00078   const util::Status& status() const { return status_; }
00079 
00080   /*
00081    * Clears any prior data writen into the stream so that it is empty.
00082    *
00083    * TODO(ewiseblatt): 20130306
00084    * I'm not sure what clear means yet.
00085    * Should it delete files or any other side effects?
00086    * This is needed to reset a response, such as when retrying.
00087    */
00088   void Clear();
00089 
00090   /*
00091    * Notifies the writer that it is startimg to write a stream.
00092    *
00093    * @see DoBegin
00094    */
00095   void Begin();
00096 
00097   /*
00098    * Synchronously write a fixed number of bytes into the stream.
00099    *
00100    * If Begin had not been called already then this will call Begin first.
00101    *
00102    * @param[in] size The number of bytes to write.
00103    * @param[in] data The data to write.
00104    *
00105    * @see DoWrite
00106    */
00107   util::Status Write(int64 size, const char* data);
00108 
00109   /*
00110    * Synchronously write a fixed number of bytes into the stream.
00111    *
00112    * @param[in] data The string piece encapsulates the size and data to write.
00113    *
00114    * @see DoWrite
00115    */
00116   util::Status Write(const StringPiece& data) {
00117     return Write(data.size(), data.data());
00118   }
00119 
00120   /*
00121    * Notifies the writer that it has finished writing a stream.
00122    *
00123    * @see DoEnd
00124    */
00125   void End();
00126 
00127   /*
00128    * Returns an unmanaged data reader that will read the content written to
00129    * this writer's byte stream.
00130    *
00131    * Depending on the writer, this may only be valid over the lifetime of
00132    * the writer. If you arent sure, use instead:
00133    * <pre>
00134    *    writer->NewManagedDataReader(DeletePointerClosure(writer))
00135    * </pre>
00136    * That effectively transfers ownership of the writer to the reader
00137    * so can only be requested once.
00138    */
00139   DataReader* NewUnmanagedDataReader() {
00140     return NewManagedDataReader(NULL);
00141   }
00142 
00143   /*
00144    * Returns a managed data reader that will read this content.
00145    */
00146   DataReader* NewManagedDataReader(Closure* deleter);
00147 
00148  protected:
00149   /*
00150    * Standard constructor.
00151    */
00152   DataWriter();
00153 
00154   /*
00155    * Sets the status as a means to pass error details back to the caller.
00156    *
00157    * Setting an error implies setting done as well. However clearing an
00158    * error by setting an ok status will not clear done. To clear the done
00159    * state you must explicitly call set_done.
00160    *
00161    * @param[in] status An error implies a hard failure and will mark the ader
00162    *            done.
00163    */
00164   void set_status(util::Status status)  { status_ = status; }
00165 
00166   /*
00167    * Hook for specialized writers to respond to Begin.
00168    *
00169    * The base class just returns ok.
00170    */
00171   virtual util::Status DoBegin();
00172 
00173   /*
00174    * Hook for specialized writes to respond to End.
00175    *
00176    * The base class just returns ok.
00177    */
00178   virtual util::Status DoEnd();
00179 
00180   /*
00181    * Hook for specialized writers to clear the byte stream.
00182    *
00183    * The base class just returns ok.
00184    */
00185   virtual util::Status DoClear();
00186 
00187   /*
00188    * Hook for the specialied writers to write into their byte stream.
00189    *
00190    * @param[in] bytes The number of bytes in data to write.
00191    * @param[in] data The data to write into the byte stream.
00192    *
00193    * @return success if all the bytes could be written, or an error.
00194    */
00195   virtual util::Status DoWrite(int64 bytes, const char* data) = 0;
00196 
00197   /*
00198    * Factory method to create new reader specialized for the writer's
00199    * byte stream implementation.
00200    *
00201    * @param[in] closure If non-NULL then create a managed reader with the
00202    *            closure.
00203    */
00204   virtual DataReader* DoNewDataReader(Closure* closure) = 0;
00205 
00206  private:
00207   int64 size_;
00208   bool began_;
00209   util::Status status_;
00210 
00211   DISALLOW_COPY_AND_ASSIGN(DataWriter);
00212 };
00213 
00214 
00215 /*
00216  * Creates a data writer that rewrites the file at the given path.
00217  * @ingroup DataLayerRaw
00218  *
00219  * @param[in] path The caller will own the file at the given path.
00220  */
00221 DataWriter* NewFileDataWriter(const StringPiece& path);
00222 
00223 /*
00224  * Creates a data writer that rewrites the file at the given path with control
00225  * over how the file is created.
00226  * @ingroup DataLayerRaw
00227  *
00228  * @param[in] path The caller will own the file at the given path.
00229  * @param[in] options The options can be used to override the permissions
00230  *            to given the created file.
00231  */
00232 DataWriter* NewFileDataWriter(
00233     const StringPiece& path, const FileOpenOptions& options);
00234 
00235 /*
00236  * Creates a data writer that rewrites the given string.
00237  * @ingroup DataLayerRaw
00238  *
00239  * @param[in] s A pointer to a string owned by the caller. This string will
00240  *              remain valid after the writer is destroyed.
00241  *              Use <code>s->data()</code> rather
00242  *              than <code>s->c_str()</code> if you need to access the raw
00243  *              bytes in the string since the writer may be given binary data.
00244  */
00245 DataWriter* NewStringDataWriter(string* s);
00246 
00247 /*
00248  * Creates an in-memory data writer that encapsulates the memory it uses.
00249  * @ingroup DataLayerRaw
00250  */
00251 DataWriter* NewStringDataWriter();
00252 
00253 }  // namespace client
00254 
00255 } // namespace googleapis
00256 #endif  // APISERVING_CLIENTS_CPP_DATA_DATA_WRITER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines