Add-ons: add pointer traits
flightgear::addons::shared_ptr_traits allows one to easily switch for instance from an SGSharedPtr<T> to an std::shared_ptr<T> for "all" uses of a particular T, and automatically have std::make_shared<T>(...) used instead of SGSharedPtr<T>(new T(...)) when creating a shared instance of T. This is interesting because std::make_shared<T>() allocates all needed memory as one block, whereas std::shared_ptr<T>(new T(args...)) would perform at least two allocations (one for the object T and one for the control block of the shared pointer). This is done via static methods (one for each kind of smart pointer): shared_ptr_traits<SGSharedPtr<T>>::makeStrongRef() shared_ptr_traits<std::shared_ptr<T>>::makeStrongRef() that forward their arguments in the same way as std::make_shared<T>().
This commit is contained in:
parent
f1e0a4e43f
commit
843383fdc3
2 changed files with 68 additions and 0 deletions
|
@ -11,6 +11,7 @@ set(HEADERS addon_fwd.hxx
|
|||
AddonManager.hxx
|
||||
AddonVersion.hxx
|
||||
exceptions.hxx
|
||||
pointer_traits.hxx
|
||||
)
|
||||
|
||||
flightgear_component(AddonManagement "${SOURCES}" "${HEADERS}")
|
||||
|
|
67
src/Add-ons/pointer_traits.hxx
Normal file
67
src/Add-ons/pointer_traits.hxx
Normal file
|
@ -0,0 +1,67 @@
|
|||
// -*- coding: utf-8 -*-
|
||||
//
|
||||
// pointer_traits.hxx --- Pointer traits classes
|
||||
// Copyright (C) 2018 Florent Rougon
|
||||
//
|
||||
// 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 FG_ADDON_POINTER_TRAITS_HXX
|
||||
#define FG_ADDON_POINTER_TRAITS_HXX
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include <simgear/structure/SGSharedPtr.hxx>
|
||||
|
||||
namespace flightgear
|
||||
{
|
||||
|
||||
namespace addons
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
struct shared_ptr_traits;
|
||||
|
||||
template <typename T>
|
||||
struct shared_ptr_traits<SGSharedPtr<T>>
|
||||
{
|
||||
using element_type = T;
|
||||
using strong_ref = SGSharedPtr<T>;
|
||||
|
||||
template <typename ...Args>
|
||||
static strong_ref makeStrongRef(Args&& ...args)
|
||||
{
|
||||
return strong_ref(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct shared_ptr_traits<std::shared_ptr<T>>
|
||||
{
|
||||
using element_type = T;
|
||||
using strong_ref = std::shared_ptr<T>;
|
||||
|
||||
template <typename ...Args>
|
||||
static strong_ref makeStrongRef(Args&& ...args)
|
||||
{
|
||||
return std::make_shared<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
} // of namespace addons
|
||||
|
||||
} // of namespace flightgear
|
||||
|
||||
#endif // of FG_ADDON_POINTER_TRAITS_HXX
|
Loading…
Reference in a new issue