DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (ce8742e6c77b)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// Copyright (c) 2009-2017 The OTS 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 OTS_GRAPHITE_H_
#define OTS_GRAPHITE_H_

#include <vector>
#include <type_traits>

namespace ots {

template<typename ParentType>
class TablePart {
 public:
  TablePart(ParentType* parent) : parent(parent) { }
  virtual ~TablePart() { }
  virtual bool ParsePart(Buffer& table) = 0;
  virtual bool SerializePart(OTSStream* out) const = 0;
 protected:
  ParentType* parent;
};

template<typename T>
bool SerializeParts(const std::vector<T>& vec, OTSStream* out) {
  for (const T& part : vec) {
    if (!part.SerializePart(out)) {
      return false;
    }
  }
  return true;
}

template<typename T>
bool SerializeParts(const std::vector<std::vector<T>>& vec, OTSStream* out) {
  for (const std::vector<T>& part : vec) {
    if (!SerializeParts(part, out)) {
      return false;
    }
  }
  return true;
}

inline bool SerializeParts(const std::vector<uint8_t>& vec, OTSStream* out) {
  for (uint8_t part : vec) {
    if (!out->WriteU8(part)) {
      return false;
    }
  }
  return true;
}

inline bool SerializeParts(const std::vector<uint16_t>& vec, OTSStream* out) {
  for (uint16_t part : vec) {
    if (!out->WriteU16(part)) {
      return false;
    }
  }
  return true;
}

inline bool SerializeParts(const std::vector<int16_t>& vec, OTSStream* out) {
  for (int16_t part : vec) {
    if (!out->WriteS16(part)) {
      return false;
    }
  }
  return true;
}

inline bool SerializeParts(const std::vector<uint32_t>& vec, OTSStream* out) {
  for (uint32_t part : vec) {
    if (!out->WriteU32(part)) {
      return false;
    }
  }
  return true;
}

inline bool SerializeParts(const std::vector<int32_t>& vec, OTSStream* out) {
  for (int32_t part : vec) {
    if (!out->WriteS32(part)) {
      return false;
    }
  }
  return true;
}

template<typename T>
size_t datasize(std::vector<T> vec) {
  return sizeof(T) * vec.size();
}

}  // namespace ots

#endif  // OTS_GRAPHITE_H_