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 ~Class() noexcept = default;
36
37#ifdef SWIG
38#undef PDF_CXX_CORE_WRAPPER_DEFINE_MEMBERS_
39#define PDF_CXX_CORE_WRAPPER_DEFINE_MEMBERS_(Class, HandleType) \
40public: \
41 Class() noexcept; \
42 Class(const Class& rhs) noexcept;
43#endif
44
45namespace detail {
46
47template<typename HandleType>
48class RefCountedHandle {
49protected:
50 ~RefCountedHandle() noexcept {
51 Release();
52 }
53
54 RefCountedHandle() noexcept
55 : m_handle(nullptr) {
56 }
57
58 RefCountedHandle(HandleType handle, bool adopt = false) noexcept
59 : m_handle(handle) {
60 if (!adopt)
61 Acquire();
62 }
63
64 RefCountedHandle(const RefCountedHandle& that) noexcept
65 : m_handle(that.m_handle) {
66 Acquire();
67 }
68
69 RefCountedHandle& operator=(const RefCountedHandle& rhs) noexcept {
70 if (this != &rhs)
71 reset(rhs.m_handle);
72 return *this;
73 }
74
75 RefCountedHandle(RefCountedHandle&& that) noexcept
76 : m_handle(that.m_handle) {
77 that.m_handle = nullptr;
78 }
79
80 RefCountedHandle& operator=(RefCountedHandle&& rhs) noexcept {
81 Release();
82 m_handle = rhs.m_handle;
83 rhs.m_handle = nullptr;
84 return *this;
85 }
86
87public:
88 void reset(HandleType handle, bool adopt = false) noexcept {
89 if (m_handle != handle) {
90 Release();
91 m_handle = handle;
92 if (!adopt)
93 Acquire();
94 } else {
95 assert(!adopt);
96 }
97 }
98
99 void reset() noexcept {
100 reset(nullptr);
101 }
102
103 HandleType* operator&() noexcept {
104 assert(!m_handle);
105 return &m_handle;
106 }
107
108 HandleType get() const noexcept { return m_handle; }
109
110 HandleType detach() noexcept { return std::exchange(m_handle, nullptr); }
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.