Main MRPT website > C++ reference for MRPT 1.5.6
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  namespace utils {
21  /** Pointer to IMPLementation auxiliary structure to make raw pointers movable, copiable and automatically deleted.
22  *\ingroup mrpt_base_grp
23  */
24  template <typename T>
25  struct pimpl
26  {
27  // This should be only parsed by MRPT sources (which since 1.5.0 require C++11).
28  // It does not matter if user code, using C++98, does not see this declaration, since PIMPL
29  // are always "private" or "protected".
30 #if MRPT_HAS_UNIQUE_PTR
31  std::unique_ptr<T> ptr;
32 
33  // All these must be defined in a .cpp file with PIMPL_DEFINE(_TYPE), after 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 #endif
42  };
43  }
44 }
45 
46 // ========== Header file ==============
47 #define PIMPL_FORWARD_DECLARATION(_TYPE) _TYPE
48 
49 #define PIMPL_DECLARE_TYPE(_TYPE, _VAR_NAME) mrpt::utils::pimpl<_TYPE> _VAR_NAME
50 
51 // ========== Implementation file ==============
52 #define PIMPL_IMPLEMENT(_TYPE) \
53  namespace mrpt { namespace utils { \
54  template <> pimpl<_TYPE>::pimpl() : ptr() {} \
55  template <> pimpl<_TYPE>::~pimpl() {} \
56  /* Movable */ \
57  template <> pimpl<_TYPE>::pimpl(pimpl<_TYPE> && op) noexcept : ptr(std::move(op.ptr)) {} \
58  template <> pimpl<_TYPE>& pimpl<_TYPE>::operator=(pimpl<_TYPE>&& op) noexcept { ptr = std::move(op.ptr); return *this; } \
59  /* Copyable: */ \
60  template <> pimpl<_TYPE>::pimpl(const pimpl<_TYPE> & op) { \
61  if (op.ptr.get() == ptr.get()) return; \
62  ptr.reset(new _TYPE(*op.ptr)); \
63  } \
64  template <> pimpl<_TYPE>& pimpl<_TYPE>::operator=(const pimpl<_TYPE>& op) { \
65  if (op.ptr.get() == ptr.get()) return *this; \
66  ptr.reset(new _TYPE(*op.ptr)); \
67  return *this; \
68  } \
69 } }
70 
71 // Put in the constructor, initializer, etc.
72 #define PIMPL_CONSTRUCT(_TYPE,_VAR_NAME) _VAR_NAME.ptr.reset( new _TYPE())
73 
74 #define PIMPL_GET_PTR(_TYPE, _VAR_NAME) _VAR_NAME.ptr.get()
75 #define PIMPL_GET_REF(_TYPE, _VAR_NAME) (*_VAR_NAME.ptr.get())
76 #define PIMPL_GET_CONSTREF(_TYPE, _VAR_NAME) (*_VAR_NAME.ptr.get())
77 
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Pointer to IMPLementation auxiliary structure to make raw pointers movable, copiable and automaticall...
Definition: pimpl.h:25



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019