From 64acb16354b3812fdcdbb47c1c9c65d81694971d Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Tue, 3 May 2016 18:11:55 -0400 Subject: [PATCH] common: Add make_unique There are parts of C++14 that are both useful and easy to implement. This is one of them. Signed-off-by: Adam C. Emerson --- src/common/backport14.h | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/common/backport14.h diff --git a/src/common/backport14.h b/src/common/backport14.h new file mode 100644 index 00000000000..a7afd49e4d6 --- /dev/null +++ b/src/common/backport14.h @@ -0,0 +1,60 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include +#include + +#ifndef CEPH_COMMON_BACKPORT14_H +#define CEPH_COMMON_BACKPORT14_H + +// Library code from C++14 that can be implemented in C++11. + +namespace ceph { +template +using remove_extent_t = typename std::remove_extent::type; + +namespace _backport14 { +template +struct uniquity { + using datum = std::unique_ptr; +}; + +template +struct uniquity { + using array = std::unique_ptr; +}; + +template +struct uniquity { + using verboten = void; +}; + +template +inline typename uniquity::datum make_unique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +inline typename uniquity::array make_unique(std::size_t n) { + return std::unique_ptr(new remove_extent_t[n]()); +} + +template +typename uniquity::verboten +make_unique(Args&&...) = delete; +} // namespace _backport14 +using _backport14::make_unique; +} // namespace ceph + +#endif // CEPH_COMMON_BACKPORT14_H