blob: c392bb1220165728b3561fe8b11c8745b867da6f [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_
#define CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_
#include <stddef.h>
#include "chromiumos-wide-profiling/compat/string.h"
#include "chromiumos-wide-profiling/utils.h"
namespace quipper {
class DataReader {
public:
DataReader() : is_cross_endian_(false) {}
virtual ~DataReader() {}
// Moves the data read pointer to |offset| bytes from the beginning of the
// data.
virtual void SeekSet(size_t offset) = 0;
// Returns the position of the data read pointer, in bytes from the beginning
// of the data.
virtual size_t Tell() const = 0;
virtual size_t size() const {
return size_;
}
// Reads raw data into |dest|. Returns true if it managed to read |size|
// bytes.
virtual bool ReadData(const size_t size, void* dest) = 0;
// Like ReadData(), but prints an error if it doesn't read all |size| bytes.
virtual bool ReadDataValue(const size_t size, const string& value_name,
void* dest);
// Read integers with endian swapping.
bool ReadUint16(uint16_t* value) {
return ReadIntValue(value);
}
bool ReadUint32(uint32_t* value) {
return ReadIntValue(value);
}
bool ReadUint64(uint64_t* value) {
return ReadIntValue(value);
}
// Read a string. Returns true if it managed to read |size| bytes (excluding
// null terminator). The actual string may be shorter than the number of bytes
// requested.
virtual bool ReadString(const size_t size, string* str) = 0;
bool is_cross_endian() const {
return is_cross_endian_;
}
void set_is_cross_endian(bool value) {
is_cross_endian_ = value;
}
protected:
// Size of the data source.
size_t size_;
private:
// Like ReadData(), but used specifically to read integers. Will swap byte
// order if necessary.
// For type-safety this one private and let public member functions call it.
template <typename T>
bool ReadIntValue(T* dest) {
if (!ReadData(sizeof(T), dest))
return false;
*dest = MaybeSwap(*dest, is_cross_endian_);
return true;
}
// For cross-endian data reading.
bool is_cross_endian_;
};
} // namespace quipper
#endif // CHROMIUMOS_WIDE_PROFILING_DATA_READER_H_