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 Release();
81 m_handle = rhs.m_handle;
82 rhs.m_handle = nullptr;
83 return *this;
84 }
85
86public:
87 void reset(HandleType handle, bool adopt = false) noexcept {
88 if (m_handle != handle) {
89 Release();
90 m_handle = handle;
91 if (!adopt)
92 Acquire();
93 } else {
94 assert(!adopt);
95 }
96 }
97
98 void reset() noexcept {
99 reset(nullptr);
100 }
101
102 HandleType* operator&() noexcept {
103 assert(!m_handle);
104 return &m_handle;
105 }
106
107 HandleType get() const noexcept { return m_handle; }
108
109 HandleType detach() noexcept { return std::exchange(m_handle, nullptr); }
110
111 bool operator==(const RefCountedHandle& rhs) const noexcept = default;
112
113 bool operator==(const HandleType& handle) const noexcept { return (m_handle == handle); }
114 friend bool operator==(const HandleType& handle, const RefCountedHandle& wrapper) noexcept { return (wrapper == handle); }
115
116 bool operator!=(const HandleType& handle) const noexcept { return !(*this == handle); }
117 friend bool operator!=(const HandleType& handle, const RefCountedHandle& wrapper) noexcept { return (wrapper != handle); }
118
119 explicit operator bool() const noexcept { return !!m_handle; }
120
121 bool operator<(const RefCountedHandle& rhs) const noexcept { return m_handle < rhs.m_handle; }
122
123private:
124 void Acquire() noexcept {
125 if (m_handle)
126 PDHandleAcquire(m_handle);
127 }
128 void Release() noexcept {
129 if (m_handle)
130 PDHandleRelease(m_handle);
131 }
132
133protected:
134 HandleType m_handle;
135};
136
137} // namespace detail
138} // namespace PDF
139
140#endif // PDFSDK_CXX_PDF_WRAPPER_BASE_H_INCLUDED_
Initialization API.