explicit instantiation of common Future types
[folly.git] / folly / Varint.h
index a58866fd10392b8f6dd2394db056977a5db61dc3..e162671815a7fbb357f65a5e2ef368a7a269b364 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
 #ifndef FOLLY_VARINT_H_
 #define FOLLY_VARINT_H_
 
+#include <type_traits>
+#include <folly/Conv.h>
 #include <folly/Range.h>
 
 namespace folly {
@@ -55,7 +57,8 @@ size_t encodeVarint(uint64_t val, uint8_t* buf);
 /**
  * Decode a value from a given buffer, advances data past the returned value.
  */
-uint64_t decodeVarint(ByteRange& data);
+template <class T>
+uint64_t decodeVarint(Range<T*>& data);
 
 /**
  * ZigZag encoding that maps signed integers with a small absolute value
@@ -88,7 +91,13 @@ inline size_t encodeVarint(uint64_t val, uint8_t* buf) {
   return p - buf;
 }
 
-inline uint64_t decodeVarint(ByteRange& data) {
+template <class T>
+inline uint64_t decodeVarint(Range<T*>& data) {
+  static_assert(
+      std::is_same<typename std::remove_cv<T>::type, char>::value ||
+          std::is_same<typename std::remove_cv<T>::type, unsigned char>::value,
+      "Only character ranges are supported");
+
   const int8_t* begin = reinterpret_cast<const int8_t*>(data.begin());
   const int8_t* end = reinterpret_cast<const int8_t*>(data.end());
   const int8_t* p = begin;
@@ -118,7 +127,8 @@ inline uint64_t decodeVarint(ByteRange& data) {
     }
     if (p == end) {
       throw std::invalid_argument("Invalid varint value. Too small: " +
-                                  std::to_string(end - begin) + " bytes");
+                                  folly::to<std::string>(end - begin) +
+                                  " bytes");
     }
     val |= static_cast<uint64_t>(*p++) << shift;
   }