Minor changes to folly/experimental/io
authorTudor Bosman <tudorb@fb.com>
Mon, 11 Jun 2012 23:24:30 +0000 (16:24 -0700)
committerTudor Bosman <tudorb@fb.com>
Wed, 27 Jun 2012 20:51:16 +0000 (13:51 -0700)
Reviewed By: philipp@fb.com

FB internal diff: D491952

folly/Bits.h
folly/experimental/io/IOBufQueue.h
folly/experimental/io/TypedIOBuf.h

index 9795f8f5650f1337d0509c48769dceb1b37fe8c0..6ee1805503cfc3bf2d83b8b491b0d3f74b21b78e 100644 (file)
@@ -329,6 +329,20 @@ struct EndianInt : public detail::EndianIntBase<T> {
 
 class Endian {
  public:
+  enum class Order : uint8_t {
+    LITTLE,
+    BIG
+  };
+
+  static constexpr Order order =
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+    Order::LITTLE;
+#elif __BYTE_ORDER == __BIG_ENDIAN
+    Order::BIG;
+#else
+# error Your machine uses a weird endianness!
+#endif  /* __BYTE_ORDER */
+
   template <class T> static T swap(T x) {
     return detail::EndianInt<T>::swap(x);
   }
index 283cd3ac5d7ea360b4f704d73d98e7942ae3e99c..3cb60b4998b4f89b02aa5ad0948e1064c66ab7f1 100644 (file)
@@ -36,6 +36,16 @@ class IOBufQueue {
     bool cacheChainLength;
   };
 
+  /**
+   * Commonly used Options, currently the only possible value other than
+   * the default.
+   */
+  static Options cacheChainLength() {
+    Options options;
+    options.cacheChainLength = true;
+    return options;
+  }
+
   explicit IOBufQueue(const Options& options = Options());
 
   /**
index b3327dc7be22006a61cf6e7a1c81405d3249a981..5658010ffda2cf86f986406c5310abb095c4067e 100644 (file)
@@ -17,6 +17,8 @@
 #ifndef FOLLY_IO_TYPEDIOBUF_H_
 #define FOLLY_IO_TYPEDIOBUF_H_
 
+#include <algorithm>
+#include <iterator>
 #include <type_traits>
 #include "folly/experimental/io/IOBuf.h"
 
@@ -106,6 +108,33 @@ class TypedIOBuf {
     buf_->reserve(smul(minHeadroom), smul(minTailroom));
   }
 
+  /**
+   * Simple wrapper to make it easier to treat this TypedIOBuf as an array of
+   * T.
+   */
+  const T& operator[](ssize_t idx) const {
+    assert(idx >= 0 && idx < length());
+    return data()[idx];
+  }
+
+  /**
+   * Append one element.
+   */
+  void push(const T& data) {
+    push(&data, &data + 1);
+  }
+
+  /**
+   * Append multiple elements in a sequence; will call distance().
+   */
+  template <class IT>
+  void push(IT begin, IT end) {
+    auto n = std::distance(begin, end);
+    reserve(headroom(), n);
+    std::copy(begin, end, writableTail());
+    append(n);
+  }
+
   // Movable
   TypedIOBuf(TypedIOBuf&&) = default;
   TypedIOBuf& operator=(TypedIOBuf&&) = default;