Implement C++14's make_unique

This commit is contained in:
xboi209 2015-09-20 20:10:28 -07:00
parent b645515eee
commit 2660145b37
4 changed files with 78 additions and 2 deletions

View file

@ -14,6 +14,7 @@ include(CheckSymbolExists)
include(CheckTypeSize)
include(CheckLibraryExists)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckMkdirArgs)
# setup short variable path names
@ -202,6 +203,16 @@ check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
check_symbol_exists(_snprintf "stdio.h" HAVE__SNPRINTF)
check_function_exists(setpgrp HAVE_SETPGRP)
CHECK_CXX_SOURCE_COMPILES(
"
#include <memory>
int main() {
auto foo = std::make_unique<int>(1);
return 0;
}
" HAVE_MAKE_UNIQUE)
# winsock2.h and ws2_32 should provide these
if(HAVE_WINSOCK2_H)
set(HAVE_GETHOSTNAME ON)

View file

@ -107,6 +107,7 @@
#cmakedefine HAVE_SNPRINTF
#cmakedefine HAVE__SNPRINTF
#cmakedefine HAVE_SETPGRP
#cmakedefine HAVE_MAKE_UNIQUE
#cmakedefine BNETD_DEFAULT_CONF_FILE "${BNETD_DEFAULT_CONF_FILE}"
#cmakedefine D2CS_DEFAULT_CONF_FILE "${D2CS_DEFAULT_CONF_FILE}"

View file

@ -11,8 +11,8 @@ set(COMMON_SOURCES
fdwbackend.h field_sizes.h file_protocol.h flags.h
give_up_root_privileges.cpp give_up_root_privileges.h hashtable.cpp
hashtable.h hexdump.cpp hexdump.h init_protocol.h introtate.h
irc_protocol.h list.cpp list.h lstr.h network.cpp network.h packet.cpp
packet.h proginfo.cpp proginfo.h queue.cpp queue.h rcm.cpp rcm.h
irc_protocol.h list.cpp list.h lstr.h make_unique.hpp network.cpp network.h
packet.cpp packet.h proginfo.cpp proginfo.h queue.cpp queue.h rcm.cpp rcm.h
rlimit.cpp rlimit.h scoped_array.h scoped_ptr.h setup_after.h
setup_before.h systemerror.cpp systemerror.h tag.cpp tag.h token.cpp
token.h tracker.h trans.cpp trans.h udp_protocol.h util.cpp util.h

View file

@ -0,0 +1,64 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef MAKE_UNIQUE_H
#define MAKE_UNIQUE_H
#include "common/setup_before.h"
#ifndef HAVE_MAKE_UNIQUE
#include <memory>
#include <type_traits>
#include <utility>
namespace pvpgn
{
//https://isocpp.org/files/papers/N3656.txt
template<class T> struct _Unique_if {
typedef std::unique_ptr<T> _Single_object;
};
template<class T> struct _Unique_if<T[]> {
typedef std::unique_ptr<T[]> _Unknown_bound;
};
template<class T, size_t N> struct _Unique_if<T[N]> {
typedef void _Known_bound;
};
template<class T, class... Args>
typename _Unique_if<T>::_Single_object
make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T>
typename _Unique_if<T>::_Unknown_bound
make_unique(size_t n) {
typedef typename remove_extent<T>::type U;
return std::unique_ptr<T>(new U[n]());
}
template<class T, class... Args>
typename _Unique_if<T>::_Known_bound
make_unique(Args&&...) = delete;
}
#else
#define make_unique std::make_unique
#endif //HAVE_MAKE_UNIQUE
#endif //MAKE_UNIQUE_H