Add __builtin___clear_cache to the portability headers
[folly.git] / folly / Range.h
index 0ca0f6150aa6a69c6b48158033e1483dda699a13..7872383ce8b46a1239b66338e034101f16a367e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -361,18 +361,32 @@ public:
     // Unfortunately current gcc versions have a bug causing it to reject
     // this check in a constexpr function:
     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448
-    return e_ - b_;
+    return size_type(e_ - b_);
   }
-  size_type walk_size() const {
+  constexpr size_type walk_size() const {
     return std::distance(b_, e_);
   }
-  bool empty() const { return b_ == e_; }
-  Iter data() const { return b_; }
-  Iter start() const { return b_; }
-  Iter begin() const { return b_; }
-  Iter end() const { return e_; }
-  Iter cbegin() const { return b_; }
-  Iter cend() const { return e_; }
+  constexpr bool empty() const {
+    return b_ == e_;
+  }
+  constexpr Iter data() const {
+    return b_;
+  }
+  constexpr Iter start() const {
+    return b_;
+  }
+  constexpr Iter begin() const {
+    return b_;
+  }
+  constexpr Iter end() const {
+    return e_;
+  }
+  constexpr Iter cbegin() const {
+    return b_;
+  }
+  constexpr Iter cend() const {
+    return e_;
+  }
   value_type& front() {
     assert(b_ < e_);
     return *b_;
@@ -398,7 +412,7 @@ public:
 
   const_range_type castToConst() const {
     return const_range_type(*this);
-  };
+  }
 
   // Works only for Range<const char*> and Range<char*>
   int compare(const const_range_type& o) const {
@@ -442,6 +456,19 @@ public:
   // (The above advice does not apply if you are targeting a 32-bit system.)
   //
   // Works only for Range<const char*> and Range<char*>
+  //
+  //
+  //         ** WANT TO GET RID OF THIS LINT? **
+  //
+  // A) Use a better hash function (*cough*folly::Hash*cough*), but
+  //    only if you don't serialize data in a format that depends on
+  //    this formula (ie the writer and reader assume this exact hash
+  //    function is used).
+  //
+  // B) If you have to use this exact function then make your own hasher
+  //    object and copy the body over (see thrift example: D3972362).
+  //    https://github.com/facebook/fbthrift/commit/f8ed502e24ab4a32a9d5f266580
+  FOLLY_DEPRECATED("Replace with folly::Hash if the hash is not serialized")
   uint32_t hash() const {
     // Taken from fbi/nstring.h:
     //    Quick and dirty bernstein hash...fine for short ascii strings
@@ -851,7 +878,7 @@ void swap(Range<T>& lhs, Range<T>& rhs) {
  * Create a range from two iterators, with type deduction.
  */
 template <class Iter>
-Range<Iter> range(Iter first, Iter last) {
+constexpr Range<Iter> range(Iter first, Iter last) {
   return Range<Iter>(first, last);
 }
 
@@ -859,18 +886,25 @@ Range<Iter> range(Iter first, Iter last) {
  * Creates a range to reference the contents of a contiguous-storage container.
  */
 // Use pointers for types with '.data()' member
-template <class Collection,
-          class T = typename std::remove_pointer<
-              decltype(std::declval<Collection>().data())>::type>
-Range<T*> range(Collection&& v) {
+template <
+    class Collection,
+    class T = typename std::remove_pointer<
+        decltype(std::declval<Collection>().data())>::type>
+constexpr Range<T*> range(Collection&& v) {
   return Range<T*>(v.data(), v.data() + v.size());
 }
 
 template <class T, size_t n>
-Range<T*> range(T (&array)[n]) {
+constexpr Range<T*> range(T (&array)[n]) {
   return Range<T*>(array, array + n);
 }
 
+template <class T, size_t n>
+constexpr Range<const T*> range(const std::array<T, n>& array) {
+  using r = Range<const T*>;
+  return array.empty() ? r{} : r(&array.at(0), &array.at(0) + n);
+}
+
 typedef Range<const char*> StringPiece;
 typedef Range<char*> MutableStringPiece;
 typedef Range<const unsigned char*> ByteRange;
@@ -878,13 +912,13 @@ typedef Range<unsigned char*> MutableByteRange;
 
 inline std::ostream& operator<<(std::ostream& os,
                                 const StringPiece piece) {
-  os.write(piece.start(), piece.size());
+  os.write(piece.start(), std::streamsize(piece.size()));
   return os;
 }
 
 inline std::ostream& operator<<(std::ostream& os,
                                 const MutableStringPiece piece) {
-  os.write(piece.start(), piece.size());
+  os.write(piece.start(), std::streamsize(piece.size()));
   return os;
 }
 
@@ -1024,7 +1058,7 @@ size_t qfind(const Range<T>& haystack,
       // Check if done searching
       if (++j == nsize) {
         // Yay
-        return i - haystack.begin();
+        return size_t(i - haystack.begin());
       }
     }
   }