From 1034506c069b9f9c24266adfaab33c26ca174aef Mon Sep 17 00:00:00 2001 From: Tudor Bosman Date: Mon, 11 Jun 2012 16:24:30 -0700 Subject: [PATCH] Minor changes to folly/experimental/io Reviewed By: philipp@fb.com FB internal diff: D491952 --- folly/Bits.h | 14 ++++++++++++++ folly/experimental/io/IOBufQueue.h | 10 ++++++++++ folly/experimental/io/TypedIOBuf.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/folly/Bits.h b/folly/Bits.h index 9795f8f5..6ee18055 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -329,6 +329,20 @@ struct EndianInt : public detail::EndianIntBase { 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 static T swap(T x) { return detail::EndianInt::swap(x); } diff --git a/folly/experimental/io/IOBufQueue.h b/folly/experimental/io/IOBufQueue.h index 283cd3ac..3cb60b49 100644 --- a/folly/experimental/io/IOBufQueue.h +++ b/folly/experimental/io/IOBufQueue.h @@ -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()); /** diff --git a/folly/experimental/io/TypedIOBuf.h b/folly/experimental/io/TypedIOBuf.h index b3327dc7..5658010f 100644 --- a/folly/experimental/io/TypedIOBuf.h +++ b/folly/experimental/io/TypedIOBuf.h @@ -17,6 +17,8 @@ #ifndef FOLLY_IO_TYPEDIOBUF_H_ #define FOLLY_IO_TYPEDIOBUF_H_ +#include +#include #include #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 + 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; -- 2.34.1