Home | History | Annotate | Download | only in rpc
      1 #ifndef ANDROID_PDX_RPC_DEFAULT_INITIALIZATION_ALLOCATOR_H_
      2 #define ANDROID_PDX_RPC_DEFAULT_INITIALIZATION_ALLOCATOR_H_
      3 
      4 #include <memory>
      5 
      6 namespace android {
      7 namespace pdx {
      8 namespace rpc {
      9 
     10 // Allocator adaptor that interposes construct() calls to convert value
     11 // initialization into default initialization. All standard containers
     12 // value-initialize their elements when constructed with a single size_type
     13 // argument or when grown by a call to resize. This allocator avoids potentially
     14 // costly value-initialization in these situations for value types that are
     15 // default constructible. As a consequence, elements of non-class types are left
     16 // uninitialized; this is desirable when using std::vector as a resizable
     17 // buffer, for example.
     18 template <typename T, typename Allocator = std::allocator<T>>
     19 class DefaultInitializationAllocator : public Allocator {
     20   typedef std::allocator_traits<Allocator> AllocatorTraits;
     21 
     22  public:
     23   template <typename U>
     24   struct rebind {
     25     using other = DefaultInitializationAllocator<
     26         U, typename AllocatorTraits::template rebind_alloc<U>>;
     27   };
     28 
     29   using Allocator::Allocator;
     30 
     31   template <typename U>
     32   void construct(U* pointer) noexcept(
     33       std::is_nothrow_default_constructible<U>::value) {
     34     ::new (static_cast<void*>(pointer)) U;
     35   }
     36   template <typename U, typename... Args>
     37   void construct(U* pointer, Args&&... args) {
     38     AllocatorTraits::construct(static_cast<Allocator&>(*this), pointer,
     39                                std::forward<Args>(args)...);
     40   }
     41 };
     42 
     43 }  // namespace rpc
     44 }  // namespace pdx
     45 }  // namespace android
     46 
     47 #endif  //  ANDROID_PDX_RPC_DEFAULT_INITIALIZATION_ALLOCATOR_H_
     48