-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add simple interface for shared and unique pointers
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* \file ptr_interface.hpp | ||
* \mainpage Interface class for pointers | ||
* \author Tobit Flatscher (github.com/2b-t) | ||
*/ | ||
|
||
#ifndef LBT__COMMON__PTR_INTERFACE | ||
#define LBT__COMMON__PTR_INTERFACE | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
|
||
namespace lbt { | ||
|
||
/**\class PtrInterface | ||
* \brief Interface class that gives easy access to shared pointers | ||
* by using CRTP | ||
* | ||
* \tparam T The class that inherits from this interface | ||
*/ | ||
template <typename T> | ||
class PtrInterface { | ||
public: | ||
// Convenient aliases | ||
using SharedPtr = std::shared_ptr<T>; | ||
using UniquePtr = std::unique_ptr<T>; | ||
|
||
/**\fn make_shared | ||
* \brief Create a shared pointer | ||
* | ||
* \tparam Args The variadic arguments | ||
* \param[in] args The arguments to be passed to the constructor of T | ||
* \return A shared pointer of type T | ||
*/ | ||
template <typename... Args> | ||
[[nodiscard]] | ||
static SharedPtr make_shared(Args&&... args) { | ||
return std::make_shared<T>(std::forward<Args>(args)...); | ||
} | ||
|
||
/**\fn make_unique | ||
* \brief Create a unique pointer | ||
* | ||
* \tparam Args The variadic arguments | ||
* \param[in] args The arguments to be passed to the constructor of T | ||
* \return A unique pointer of type T | ||
*/ | ||
template <typename... Args> | ||
[[nodiscard]] | ||
static UniquePtr make_unique(Args&&... args) { | ||
return std::make_unique<T>(std::forward<Args>(args)...); | ||
} | ||
|
||
protected: | ||
PtrInterface() = default; | ||
PtrInterface(PtrInterface const&) = default; | ||
PtrInterface(PtrInterface&&) = default; | ||
PtrInterface& operator=(PtrInterface const&) = default; | ||
PtrInterface& operator=(PtrInterface&&) = default; | ||
}; | ||
|
||
} | ||
|
||
#endif // LBT__COMMON__PTR_INTERFACE |