1 // Copyright 2015 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMIUMOS_WIDE_PROFILING_DATA_WRITER_H_
6 #define CHROMIUMOS_WIDE_PROFILING_DATA_WRITER_H_
7 
8 #include <stddef.h>
9 
10 #include "compat/string.h"
11 
12 namespace quipper {
13 
14 // Interface for writing data to a destination. The nature of the destination is
15 // unspecified, and is to be specified by derived classes.
16 class DataWriter {
17  public:
~DataWriter()18   virtual ~DataWriter() {}
19 
20   // Moves the data pointer to |offset| bytes from the beginning of the data.
21   virtual void SeekSet(size_t offset) = 0;
22 
23   // Returns the position of the data pointer, in bytes from the beginning of
24   // the data.
25   virtual size_t Tell() const = 0;
26 
size()27   virtual size_t size() const { return size_; }
28 
29   // Writes raw data. Returns true if it managed to write |size| bytes.
30   virtual bool WriteData(const void* src, const size_t size) = 0;
31 
32   // Like WriteData(), but prints an error if it doesn't write all |size| bytes.
33   virtual bool WriteDataValue(const void* src, const size_t size,
34                               const string& value_name);
35 
36   // Writes a string. If the string length is smaller than |size|, it will fill
37   // in the remainder of of the destination memory with zeroes. If the string is
38   // longer than |size|, it will truncate the string, and will not add a null
39   // terminator. Returns true iff the expected number of bytes were written.
40   virtual bool WriteString(const string& str, const size_t size) = 0;
41 
42   // Writes a string |src| to data, prefixed with a 32-bit size field. The size
43   // is rounded up to the next multiple of uint64.
44   bool WriteStringWithSizeToData(const string& src);
45 
46  protected:
47   // Returns true if |data_size| bytes of data can be written to the current
48   // underlying destination.
49   virtual bool CanWriteSize(size_t data_size) = 0;
50 
51   // Current data size.
52   size_t size_;
53 };
54 
55 }  // namespace quipper
56 
57 #endif  // CHROMIUMOS_WIDE_PROFILING_DATA_WRITER_H_
58