PDF SDK Documentation

Comprehensive Guide for Developers: Features, Integration, and API Reference

Loading...
Searching...
No Matches
wrapper_base.h
1// Copyright (c) 2009-2025 Avanquest Software. All rights reserved.
2
3#ifndef PDFSDK_CXX_PDF_WRAPPER_BASE_H_INCLUDED_
4#define PDFSDK_CXX_PDF_WRAPPER_BASE_H_INCLUDED_
5
6#include <cassert>
7#include <utility>
8
9#include <pdfsdk/core/init.h>
10#include <pdfsdk/cxx/exception.h>
11
12namespace PDF {
13
14#define PDF_CXX_CORE_WRAPPER_DEFINE_MEMBERS_(Class, HandleType) \
15public: \
16 Class() noexcept { \
17 } \
18 Class(HandleType handle, bool adopt = false) noexcept \
19 : detail::RefCountedHandle<HandleType>(handle, adopt) { \
20 } \
21 Class(const Class& rhs) noexcept \
22 : detail::RefCountedHandle<HandleType>(rhs) { \
23 } \
24 Class& operator=(const Class& rhs) noexcept { \
25 detail::RefCountedHandle<HandleType>::operator=(rhs); \
26 return *this; \
27 } \
28 Class(Class&& rhs) noexcept \
29 : detail::RefCountedHandle<HandleType>(std::move(rhs)) { \
30 } \
31 Class& operator=(Class&& rhs) noexcept { \
32 detail::RefCountedHandle<HandleType>::operator=(std::move(rhs)); \
33 return *this; \
34 }
35
36#ifdef SWIG
37#undef PDF_CXX_CORE_WRAPPER_DEFINE_MEMBERS_
38#define PDF_CXX_CORE_WRAPPER_DEFINE_MEMBERS_(Class, HandleType) \
39public: \
40 Class() noexcept; \
41 Class(const Class& rhs) noexcept;
42#endif
43
44namespace detail {
45
46template<typename HandleType>
47class RefCountedHandle {
48protected:
49 ~RefCountedHandle() {
50 Release();
51 }
52
53 RefCountedHandle() noexcept
54 : m_handle(nullptr) {
55 }
56
57 RefCountedHandle(HandleType handle, bool adopt = false) noexcept
58 : m_handle(handle) {
59 if (!adopt)
60 Acquire();
61 }
62
63 RefCountedHandle(const RefCountedHandle& that) noexcept
64 : m_handle(that.m_handle) {
65 Acquire();
66 }
67
68 RefCountedHandle& operator=(const RefCountedHandle& rhs) noexcept {
69 if (this != &rhs)
70 reset(rhs.m_handle);
71 return *this;
72 }
73
74 RefCountedHandle(RefCountedHandle&& that) noexcept
75 : m_handle(that.m_handle) {
76 that.m_handle = nullptr;
77 }
78
79 RefCountedHandle& operator=(RefCountedHandle&& rhs) noexcept {
80 reset(rhs.m_handle, true);
81 rhs.m_handle = nullptr;
82 return *this;
83 }
84
85public:
86 void reset(HandleType handle, bool adopt = false) noexcept {
87 if (m_handle != handle) {
88 Release();
89 m_handle = handle;
90 if (!adopt)
91 Acquire();
92 }
93 }
94
95 void reset() noexcept {
96 reset(nullptr);
97 }
98
99 HandleType* operator&() noexcept {
100 assert(!m_handle);
101 return &m_handle;
102 }
103
104 HandleType get() const noexcept { return m_handle; }
105
106 HandleType detach() noexcept {
107 auto detached = m_handle;
108 m_handle = nullptr;
109 return detached;
110 }
111
112 bool operator==(const RefCountedHandle& rhs) const noexcept = default;
113
114 bool operator==(const HandleType& handle) const noexcept { return (m_handle == handle); }
115 friend bool operator==(const HandleType& handle, const RefCountedHandle& wrapper) noexcept { return (wrapper == handle); }
116
117 bool operator!=(const HandleType& handle) const noexcept { return !(*this == handle); }
118 friend bool operator!=(const HandleType& handle, const RefCountedHandle& wrapper) noexcept { return (wrapper != handle); }
119
120 explicit operator bool() const noexcept { return !!m_handle; }
121
122 bool operator<(const RefCountedHandle& rhs) const noexcept { return m_handle < rhs.m_handle; }
123
124private:
125 void Acquire() noexcept {
126 if (m_handle)
127 PDHandleAcquire(m_handle);
128 }
129 void Release() noexcept {
130 if (m_handle)
131 PDHandleRelease(m_handle);
132 }
133
134protected:
135 HandleType m_handle;
136};
137
138} // namespace detail
139} // namespace PDF
140
141#endif // PDFSDK_CXX_PDF_WRAPPER_BASE_H_INCLUDED_
Initialization API.