mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
pimpl.hpp
Go to the documentation of this file.
1
2
3#ifndef MRS_LIB_EXPERIMENTAL_UTILITY_PIMPL_HPP_
4#define MRS_LIB_EXPERIMENTAL_UTILITY_PIMPL_HPP_
5
6
7#include <cassert>
8#include <cstddef>
9#include <memory>
10#include <stdexcept>
11#include <utility>
12
13
14namespace mrs_lib
15{
16
37 template <typename Interface, typename StorageType = std::unique_ptr<Interface>>
38 class Pimpl
39 {
40 public:
44 explicit Pimpl(std::nullptr_t) = delete;
45
52 explicit Pimpl(StorageType impl) : impl_(std::move(impl))
53 {
54 if (impl_ == nullptr)
55 {
56 throw std::logic_error("Cannot construct Pimpl from nullptr.");
57 }
58 }
59
68 template <typename T, typename... Args>
69 explicit Pimpl(std::in_place_type_t<T>, Args&&... args) : impl_(std::make_unique<T>(std::forward<Args>(args)...))
70 {
71 }
72
74 ~Pimpl() = default;
75
77 Pimpl(const Pimpl&) = delete;
79 Pimpl& operator=(const Pimpl&) = delete;
81 Pimpl(Pimpl&&) = default;
83 Pimpl& operator=(Pimpl&&) = default;
84
90 [[nodiscard]] Interface& operator*() noexcept
91 {
92 assert(impl_ != nullptr);
93 return *impl_;
94 }
95
101 [[nodiscard]] const Interface& operator*() const noexcept
102 {
103 assert(impl_ != nullptr);
104 return *impl_;
105 }
106
112 [[nodiscard]] Interface* operator->() noexcept
113 {
114 assert(impl_ != nullptr);
115 return impl_.get();
116 }
117
123 [[nodiscard]] const Interface* operator->() const noexcept
124 {
125 assert(impl_ != nullptr);
126 return impl_.get();
127 }
128
138 [[nodiscard]] bool valueless_after_move() const noexcept
139 {
140 return impl_ == nullptr;
141 }
142
143 private:
144 StorageType impl_;
145 };
146
147} // namespace mrs_lib
148
149
150#endif // MRS_LIB_EXPERIMENTAL_UTILITY_PIMPL_HPP_
Interface * operator->() noexcept
Arrow operator for non-const access.
Definition pimpl.hpp:112
Pimpl(std::in_place_type_t< T >, Args &&... args)
Construct the wrapped object in place.
Definition pimpl.hpp:69
Pimpl(const Pimpl &)=delete
Pimpl is not copyable.
~Pimpl()=default
Default destructor.
Pimpl & operator=(Pimpl &&)=default
Default move assignment operator.
const Interface & operator*() const noexcept
Dereference operator for const access.
Definition pimpl.hpp:101
Pimpl(std::nullptr_t)=delete
Deleted constructor to prevent construction from nullptr.
const Interface * operator->() const noexcept
Arrow operator for const access.
Definition pimpl.hpp:123
bool valueless_after_move() const noexcept
Checks if the object is in moved from state.
Definition pimpl.hpp:138
Pimpl(StorageType impl)
Construct Pimpl from a pointer to the object.
Definition pimpl.hpp:52
Pimpl & operator=(const Pimpl &)=delete
Pimpl is not copyable.
Interface & operator*() noexcept
Dereference operator for non-const access.
Definition pimpl.hpp:90
Pimpl(Pimpl &&)=default
Default move constructor.
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24