X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FPadded.h;h=f30e22265bb9dd9dd91edc72beaaebf2b2aae0b4;hb=b22935cd23251a9958f73242cb1dc44dfc75c5f5;hp=50fb8d78a5c4583acb78ff504c9249a233a21eb3;hpb=7a78293e749feefdcd44040b27e84e4047af4eec;p=folly.git diff --git a/folly/Padded.h b/folly/Padded.h index 50fb8d78..f30e2226 100644 --- a/folly/Padded.h +++ b/folly/Padded.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 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. @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef FOLLY_PADDED_H_ -#define FOLLY_PADDED_H_ +#pragma once #include #include @@ -29,7 +28,7 @@ #include #include -#include +#include /** * Code that aids in storing data aligned on block (possibly cache-line) @@ -68,7 +67,7 @@ struct NodeValid::type> { typedef void type; }; -} // namespace detail +} // namespace detail template class Node::type> { @@ -146,6 +145,33 @@ template class Iterator; namespace detail { +template +struct padded_emplace_back_or_push_back_ { + static decltype(auto) go(Container& container, Args&&... args) { + using Value = typename Container::value_type; + return container.push_back(Value(std::forward(args)...)); + } +}; + +template +struct padded_emplace_back_or_push_back_< + void_t().emplace_back(std::declval()...))>, + Container, + Args...> { + static decltype(auto) go(Container& container, Args&&... args) { + return container.emplace_back(std::forward(args)...); + } +}; + +template +decltype(auto) padded_emplace_back_or_push_back( + Container& container, + Args&&... args) { + using impl = padded_emplace_back_or_push_back_; + return impl::go(container, std::forward(args)...); +} + // Helper class to transfer the constness from From (a lvalue reference) // and create a lvalue reference to To. // @@ -191,7 +217,7 @@ struct IteratorBase { > type; }; -} // namespace detail +} // namespace detail /** * Wrapper around iterators to Node to return iterators to the underlying @@ -346,8 +372,9 @@ class Adaptor { lastCount_(lastCount) { } explicit Adaptor(size_t n, const value_type& value = value_type()) - : c_(Node::nodeCount(n), fullNode(value)), - lastCount_(n % Node::kElementCount ?: Node::kElementCount) { + : c_(Node::nodeCount(n), fullNode(value)) { + const auto count = n % Node::kElementCount; + lastCount_ = count != 0 ? count : Node::kElementCount; } Adaptor(const Adaptor&) = default; @@ -385,7 +412,7 @@ class Adaptor { iterator end() { auto it = iterator(c_.end()); if (lastCount_ != Node::kElementCount) { - it -= (Node::kElementCount - lastCount_); + it -= difference_type(Node::kElementCount - lastCount_); } return it; } @@ -495,7 +522,7 @@ class Adaptor { private: value_type* allocate_back() { if (lastCount_ == Node::kElementCount) { - container_emplace_back_or_push_back(c_); + detail::padded_emplace_back_or_push_back(c_); lastCount_ = 0; } return &c_.back().data()[lastCount_++]; @@ -510,7 +537,5 @@ class Adaptor { size_t lastCount_; // number of elements in last Node }; -} // namespace padded -} // namespace folly - -#endif /* FOLLY_PADDED_H_ */ +} // namespace padded +} // namespace folly