12 #define NOMINMAX //< Needed to prevent windows.h from defining macros min() and max(). 22 #include <type_traits> 24 #include "plugin_api.h" 31 typedef volatile LONG AtomicLong;
33 typedef volatile long AtomicLong;
35 #error "Unsupported compiler is used." 39 static AtomicLong
inc(AtomicLong* val)
42 return InterlockedIncrement(val);
44 return __sync_add_and_fetch(val, 1);
49 static AtomicLong
dec(AtomicLong* val)
52 return InterlockedDecrement(val);
54 return __sync_sub_and_fetch(val, 1);
79 m_objToWatch(objToWatch),
80 m_refCountingDelegate(nullptr)
90 m_objToWatch(nullptr),
91 m_refCountingDelegate(refCountingDelegate)
98 return m_refCountingDelegate
99 ? m_refCountingDelegate->
addRef()
100 : atomic::inc(&m_refCount);
109 if (m_refCountingDelegate)
112 const int newRefCounter = atomic::dec(&m_refCount);
113 if (newRefCounter == 0)
115 return newRefCounter;
120 if (m_refCountingDelegate)
121 return m_refCountingDelegate->refCount();
126 mutable atomic::AtomicLong m_refCount;
128 CommonRefManager* m_refCountingDelegate;
131 template <
typename T>
141 virtual int addRef()
const override {
return m_refManager.
addRef(); }
142 virtual int releaseRef()
const override {
return m_refManager.
releaseRef(); }
144 int refCount()
const {
return m_refManager.refCount(); }
157 template<
typename RefCountableInterface>
160 if (
object ==
nullptr)
164 return commonRefCounter->refCount();
167 return object->releaseRef();
170 enum NxGuidFormatOption
175 applyAll = uppercase | hyphens | braces
183 return {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}};
189 memcpy(result.
bytes, data,
sizeof(result.
bytes));
193 static nxpl::NX_GUID fromStdString(
const std::string& guidStr)
195 static const auto kMinGuidStrSize = 32;
196 static const auto kGuidBytesNumber = 16;
198 if (guidStr.size() < kMinGuidStrSize)
199 return NxGuidHelper::nullGuid();
202 int currentByteIndex = 0;
203 std::string currentByteString;
204 for (std::string::size_type i = 0; i < guidStr.size(); ++i)
206 if (guidStr[i] ==
'{' || guidStr[i] ==
'}' || guidStr[i] ==
'-' 207 || guidStr[i] ==
'\t' || guidStr[i] ==
'\n' || guidStr[i] ==
'r' 208 || guidStr[i] ==
' ')
213 if (currentByteIndex >= kGuidBytesNumber)
214 return NxGuidHelper::nullGuid();
216 currentByteString += guidStr[i];
217 if (currentByteString.size() == 2)
219 char* pEnd =
nullptr;
221 const long v = std::strtol(currentByteString.c_str(), &pEnd, 16);
222 const bool hasError = v > std::numeric_limits<unsigned char>::max()
223 || v < std::numeric_limits<unsigned char>::min()
228 return NxGuidHelper::nullGuid();
230 guid.
bytes[currentByteIndex] = (
unsigned char) v;
232 currentByteString.clear();
236 if (currentByteIndex != kGuidBytesNumber)
237 return NxGuidHelper::nullGuid();
243 static std::string toStdString(
245 unsigned int format = NxGuidFormatOption::applyAll)
247 std::stringstream ss;
248 ss << std::hex << std::setfill(
'0');
250 if (format & NxGuidFormatOption::braces)
253 if (format & NxGuidFormatOption::uppercase)
254 ss << std::uppercase;
256 for (
int i = 0; i < 4; ++i)
259 ss << static_cast<unsigned int>(guid.
bytes[i]);
262 if (format & NxGuidFormatOption::hyphens)
265 for (
int i = 0; i < 2; ++i)
268 ss << static_cast<unsigned int>(guid.
bytes[4 + i]);
271 if (format & NxGuidFormatOption::hyphens)
274 for (
int i = 0; i < 2; ++i)
277 ss << static_cast<unsigned int>(guid.
bytes[6 + i]);
280 if (format & NxGuidFormatOption::hyphens)
283 for (
int i = 0; i < 2; ++i)
286 ss << static_cast<unsigned int>(guid.
bytes[8 + i]);
289 if (format & NxGuidFormatOption::hyphens)
292 for (
int i = 0; i < 6; ++i)
295 ss << static_cast<unsigned int>(guid.
bytes[10 + i]);
298 if (format & NxGuidFormatOption::braces)
313 inline std::ostream& operator<<(std::ostream& os,
const nxpl::NX_GUID&
id)
315 return os << nxpt::toStdString(
id);
329 for (
size_t i = 0; i <
sizeof(guid.
bytes); ++i)
330 h = (h + (324
'723'947ull + guid.
bytes[i])) ^ 93
'485'734
'985ull; virtual int addRef() const =0
Increment reference counter.
unsigned char bytes[16]
GUID bytes.
Definition: plugin_api.h:29
Definition: to_string.h:49
Base class for every interface, provided by plugin.
Definition: plugin_api.h:44
CommonRefManager(nxpl::PluginInterface *objToWatch)
Definition: plugin_tools.h:77
GUID of plugin interface.
Definition: plugin_api.h:26
VMS dynamic plugin API (c++)
Definition: plugin_api.h:23
Definition: plugin_tools.h:178
Definition: plugin_tools.h:132
CommonRefManager(CommonRefManager *refCountingDelegate)
Definition: plugin_tools.h:88
Definition: plugin_tools.h:67
int addRef() const
Definition: plugin_tools.h:96
Definition: plugin_tools.h:26
int releaseRef() const
Definition: plugin_tools.h:107