#include <type_traits>
#include <utility>
+#include <folly/FormatTraits.h>
#include <folly/Likely.h>
#include <folly/Malloc.h>
#include <folly/Traits.h>
//-----------------------------------------------------------------------------
// other
+namespace detail {
+
+// Format support.
+template <class T, class A>
+struct IndexableTraits<fbvector<T, A>>
+ : public IndexableTraitsSeq<fbvector<T, A>> {
+};
+
+} // namespace detail
+
template <class T, class A>
void compactResize(fbvector<T, A>* v, size_t sz) {
v->resize(sz);
#error This file may only be included from Format.h.
#endif
+#include <array>
+#include <deque>
+#include <map>
+#include <unordered_map>
+#include <vector>
+
#include <folly/Exception.h>
+#include <folly/FormatTraits.h>
#include <folly/Traits.h>
// Ignore -Wformat-nonliteral warnings within this file
namespace detail {
-// Shortcut, so we don't have to use enable_if everywhere
-struct FormatTraitsBase {
- typedef void enabled;
-};
-
-// Traits that define enabled, value_type, and at() for anything
-// indexable with integral keys: pointers, arrays, vectors, and maps
-// with integral keys
-template <class T, class Enable=void> struct IndexableTraits;
-
-// Base class for sequences (vectors, deques)
-template <class C>
-struct IndexableTraitsSeq : public FormatTraitsBase {
- typedef C container_type;
- typedef typename C::value_type value_type;
- static const value_type& at(const C& c, int idx) {
- return c.at(idx);
- }
-
- static const value_type& at(const C& c, int idx,
- const value_type& dflt) {
- return (idx >= 0 && size_t(idx) < c.size()) ? c.at(idx) : dflt;
- }
-};
-
-// Base class for associative types (maps)
-template <class C>
-struct IndexableTraitsAssoc : public FormatTraitsBase {
- typedef typename C::value_type::second_type value_type;
- static const value_type& at(const C& c, int idx) {
- return c.at(static_cast<typename C::key_type>(idx));
- }
- static const value_type& at(const C& c, int idx,
- const value_type& dflt) {
- auto pos = c.find(static_cast<typename C::key_type>(idx));
- return pos != c.end() ? pos->second : dflt;
- }
-};
-
// std::array
template <class T, size_t N>
struct IndexableTraits<std::array<T, N>>
: public IndexableTraitsSeq<std::deque<T, A>> {
};
-// fbvector
-template <class T, class A>
-struct IndexableTraits<fbvector<T, A>>
- : public IndexableTraitsSeq<fbvector<T, A>> {
-};
-
-// small_vector
-template <class T, size_t M, class A, class B, class C>
-struct IndexableTraits<small_vector<T, M, A, B, C>>
- : public IndexableTraitsSeq<small_vector<T, M, A, B, C>> {
-};
-
// std::map with integral keys
template <class K, class T, class C, class A>
struct IndexableTraits<
#ifndef FOLLY_FORMAT_H_
#define FOLLY_FORMAT_H_
-#include <array>
#include <cstdio>
#include <tuple>
#include <type_traits>
-#include <vector>
-#include <deque>
-#include <map>
-#include <unordered_map>
-#include <folly/FBVector.h>
#include <folly/Conv.h>
#include <folly/Range.h>
#include <folly/Traits.h>
-#include <folly/Likely.h>
#include <folly/String.h>
-#include <folly/small_vector.h>
#include <folly/FormatArg.h>
// Ignore shadowing warnings within this file, so includers can use -Wshadow.
--- /dev/null
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FOLLY_FORMAT_TRAITS_H_
+#define FOLLY_FORMAT_TRAITS_H_
+
+#include <type_traits>
+
+namespace folly { namespace detail {
+
+// Shortcut, so we don't have to use enable_if everywhere
+struct FormatTraitsBase {
+ typedef void enabled;
+};
+
+// Traits that define enabled, value_type, and at() for anything
+// indexable with integral keys: pointers, arrays, vectors, and maps
+// with integral keys
+template <class T, class Enable = void> struct IndexableTraits;
+
+// Base class for sequences (vectors, deques)
+template <class C>
+struct IndexableTraitsSeq : public FormatTraitsBase {
+ typedef C container_type;
+ typedef typename C::value_type value_type;
+
+ static const value_type& at(const C& c, int idx) {
+ return c.at(idx);
+ }
+
+ static const value_type& at(const C& c, int idx, const value_type& dflt) {
+ return (idx >= 0 && size_t(idx) < c.size()) ? c.at(idx) : dflt;
+ }
+};
+
+// Base class for associative types (maps)
+template <class C>
+struct IndexableTraitsAssoc : public FormatTraitsBase {
+ typedef typename C::value_type::second_type value_type;
+
+ static const value_type& at(const C& c, int idx) {
+ return c.at(static_cast<typename C::key_type>(idx));
+ }
+
+ static const value_type& at(const C& c, int idx, const value_type& dflt) {
+ auto pos = c.find(static_cast<typename C::key_type>(idx));
+ return pos != c.end() ? pos->second : dflt;
+ }
+};
+
+}} // namespaces
+
+#endif /* FOLLY_FORMAT_TRAITS_H_ */
folly-config.h \
Foreach.h \
FormatArg.h \
+ FormatTraits.h \
Format.h \
Format-inl.h \
futures/Deprecated.h \
#ifndef FOLLY_SMALL_VECTOR_H_
#define FOLLY_SMALL_VECTOR_H_
-#include <folly/Portability.h>
-
#include <stdexcept>
#include <cstdlib>
#include <type_traits>
#include <boost/mpl/count.hpp>
#include <boost/mpl/max.hpp>
+#include <folly/FormatTraits.h>
#include <folly/Malloc.h>
+#include <folly/Portability.h>
#if defined(__GNUC__) && FOLLY_X64
# include <folly/SmallLocks.h>
//////////////////////////////////////////////////////////////////////
-}
+namespace detail {
+
+// Format support.
+template <class T, size_t M, class A, class B, class C>
+struct IndexableTraits<small_vector<T, M, A, B, C>>
+ : public IndexableTraitsSeq<small_vector<T, M, A, B, C>> {
+};
+
+} // namespace detail
+
+} // namespace folly
#pragma GCC diagnostic pop
#include <folly/Format.h>
+#include <folly/FBVector.h>
#include <folly/FileUtil.h>
-#include <folly/json.h>
#include <folly/dynamic.h>
+#include <folly/json.h>
+#include <folly/small_vector.h>
#include <glog/logging.h>
#include <gflags/gflags.h>
EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr)));
}
+namespace {
+
+template <class T>
+void testFormatSeq() {
+ T v{10, 20, 30};
+ EXPECT_EQ("30 10", sformat("{0[2]} {0[0]}", v));
+ EXPECT_EQ("0020", sformat("{0[1]:04}", v));
+ EXPECT_EQ("0020", svformat("{1:04}", v));
+ EXPECT_EQ("10 20", svformat("{} {}", v));
+ EXPECT_EQ("10 20 0030", svformat("{} {} {:04}", v));
+}
+
+} // namespace
+
+TEST(FormatOther, fbvector) {
+ testFormatSeq<fbvector<int>>();
+}
+
+TEST(FormatOther, small_vector) {
+ testFormatSeq<small_vector<int, 2>>();
+}
+
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);