Main MRPT website > C++ reference for MRPT 1.9.9
pimpl.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/config.h>
13 #include <mrpt/utils/mrpt_macros.h>
14 #include <memory>
15 
16 /** \file Macros to help implementing the PIMPL idiom and make all
17  * declarations easier to read and less error-prone.
18  */
19 namespace mrpt
20 {
21 namespace utils
22 {
23 /** Pointer to IMPLementation auxiliary structure to make raw pointers movable,
24  *copiable and automatically deleted.
25  *\ingroup mrpt_base_grp
26  */
27 template <typename T>
28 struct pimpl
29 {
30  std::unique_ptr<T> ptr;
31 
32  // All these must be defined in a .cpp file with PIMPL_DEFINE(_TYPE), after
33  // including the
34  // real definition of T, which is only forward-declared in the headers:
35  pimpl();
36  ~pimpl();
37  pimpl(pimpl&& op) noexcept; // movable
38  pimpl& operator=(pimpl&& op) noexcept; //
39  pimpl(const pimpl& op); // and copyable
40  pimpl& operator=(const pimpl& op); //
41 };
42 }
43 }
44 
45 // ========== Header file ==============
46 #define PIMPL_FORWARD_DECLARATION(_TYPE) _TYPE
47 
48 #define PIMPL_DECLARE_TYPE(_TYPE, _VAR_NAME) mrpt::utils::pimpl<_TYPE> _VAR_NAME
49 
50 // ========== Implementation file ==============
51 #define PIMPL_IMPLEMENT(_TYPE) \
52  namespace mrpt \
53  { \
54  namespace utils \
55  { \
56  template <> \
57  pimpl<_TYPE>::pimpl() : ptr() \
58  { \
59  } \
60  template <> \
61  pimpl<_TYPE>::~pimpl() \
62  { \
63  } \
64  /* Movable */ \
65  template <> \
66  pimpl<_TYPE>::pimpl(pimpl<_TYPE>&& op) noexcept : ptr(std::move(op.ptr)) \
67  { \
68  } \
69  template <> \
70  pimpl<_TYPE>& pimpl<_TYPE>::operator=(pimpl<_TYPE>&& op) noexcept \
71  { \
72  ptr = std::move(op.ptr); \
73  return *this; \
74  } \
75  /* Copyable: */ \
76  template <> \
77  pimpl<_TYPE>::pimpl(const pimpl<_TYPE>& op) \
78  { \
79  if (op.ptr.get() == ptr.get()) return; \
80  ptr.reset(new _TYPE()); \
81  *ptr = *op.ptr; \
82  } \
83  template <> \
84  pimpl<_TYPE>& pimpl<_TYPE>::operator=(const pimpl<_TYPE>& op) \
85  { \
86  if (op.ptr.get() == ptr.get()) return *this; \
87  ptr.reset(new _TYPE()); \
88  *ptr = *op.ptr; \
89  return *this; \
90  } \
91  } \
92  }
93 
94 // Put in the constructor, initializer, etc.
95 #define PIMPL_CONSTRUCT(_TYPE, _VAR_NAME) _VAR_NAME.ptr.reset(new _TYPE())
96 
97 #define PIMPL_GET_PTR(_TYPE, _VAR_NAME) _VAR_NAME.ptr.get()
98 #define PIMPL_GET_REF(_TYPE, _VAR_NAME) (*_VAR_NAME.ptr.get())
99 #define PIMPL_GET_CONSTREF(_TYPE, _VAR_NAME) (*_VAR_NAME.ptr.get())
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
pimpl & operator=(pimpl &&op) noexcept
Pointer to IMPLementation auxiliary structure to make raw pointers movable, copiable and automaticall...
Definition: pimpl.h:28
std::unique_ptr< T > ptr
Definition: pimpl.h:30



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019