mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-29 00:27:27 -04:00
Merge pull request #1696 from G10h4ck/modern_cpp
Take advantage of modern C++ features
This commit is contained in:
commit
f5bf093409
4 changed files with 189 additions and 11 deletions
35
libretroshare/src/util/cxx17retrocompat.h
Normal file
35
libretroshare/src/util/cxx17retrocompat.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*******************************************************************************
|
||||
* libretroshare/src/util: cxx17retrocompat.h *
|
||||
* *
|
||||
* libretroshare: retroshare core library *
|
||||
* *
|
||||
* Copyright (C) 2019 Gioacchino Mazzurco <gio@eigenlab.org> *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as *
|
||||
* published by the Free Software Foundation, either version 3 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 Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus < 201703L
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace std
|
||||
{
|
||||
using namespace std;
|
||||
template <class T> constexpr typename add_const<T>::type& as_const(T& t) noexcept
|
||||
{ return t; }
|
||||
template <class T> void as_const(const T&&) = delete;
|
||||
}
|
||||
#endif // __cplusplus < 201703L
|
|
@ -62,6 +62,35 @@ bool myFunnyFunction(
|
|||
*/
|
||||
#define RS_DEFAULT_STORAGE_PARAM(Type,...) *std::unique_ptr<Type>(new Type(__VA_ARGS__))
|
||||
|
||||
|
||||
/** @brief Safely dynamic cast between std::unique_ptr of different types
|
||||
* std::unique_ptr semantic rely on the invariant that only one instance own
|
||||
* the object, when casting between differents types one would be tempted to do
|
||||
* it in a one liner that easly end up breaking that condition ending up in a
|
||||
* double delete and crash or in a silent memleak.
|
||||
* With this function one can do that with same comfort of a plain dynamic_cast,
|
||||
* plus the std::unique_ptr safety.
|
||||
* @param[inout] src reference to source pointer. If the cast is successfull it
|
||||
* is released, otherwise it is left untouched.
|
||||
* @param[out] dst reference to destination pointer. If the cast is successful
|
||||
* it get reseated to the object address, otherwise it is left untouched.
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
template <class T_SRC, class T_DST>
|
||||
bool rs_unique_cast(
|
||||
std::unique_ptr<T_SRC>& src, std::unique_ptr<T_DST>& dst )
|
||||
{
|
||||
T_DST* dstPtr = dynamic_cast<T_DST*>(src.get());
|
||||
if(dstPtr)
|
||||
{
|
||||
src.release();
|
||||
dst.reset(dstPtr);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void *rs_malloc(size_t size) ;
|
||||
|
||||
// This is a scope guard to release the memory block when going of of the current scope.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue