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(0)
89 m_refCountingDelegate(refCountingDelegate)
96 return m_refCountingDelegate
97 ? m_refCountingDelegate->
addRef()
98 : atomic::inc(&m_refCount);
107 if (m_refCountingDelegate)
110 const int newRefCounter = atomic::dec(&m_refCount);
111 if (newRefCounter == 0)
113 return newRefCounter;
118 if (m_refCountingDelegate)
119 return m_refCountingDelegate->refCount();
124 mutable atomic::AtomicLong m_refCount;
126 CommonRefManager* m_refCountingDelegate;
129 template <
typename T>
139 virtual int addRef()
const override {
return m_refManager.
addRef(); }
140 virtual int releaseRef()
const override {
return m_refManager.
releaseRef(); }
142 int refCount()
const {
return m_refManager.refCount(); }
155 template<
typename RefCountableInterface>
158 if (
object ==
nullptr)
162 return commonRefCounter->refCount();
165 return object->releaseRef();
168 enum NxGuidFormatOption
173 applyAll = uppercase | hyphens | braces
181 return {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}};
187 memcpy(result.
bytes, data,
sizeof(result.
bytes));
191 static nxpl::NX_GUID fromStdString(
const std::string& guidStr)
193 static const auto kMinGuidStrSize = 32;
194 static const auto kGuidBytesNumber = 16;
196 if (guidStr.size() < kMinGuidStrSize)
197 return NxGuidHelper::nullGuid();
200 int currentByteIndex = 0;
201 std::string currentByteString;
202 for (std::string::size_type i = 0; i < guidStr.size(); ++i)
204 if (guidStr[i] ==
'{' || guidStr[i] ==
'}' || guidStr[i] ==
'-' 205 || guidStr[i] ==
'\t' || guidStr[i] ==
'\n' || guidStr[i] ==
'r' 206 || guidStr[i] ==
' ')
211 if (currentByteIndex >= kGuidBytesNumber)
212 return NxGuidHelper::nullGuid();
214 currentByteString += guidStr[i];
215 if (currentByteString.size() == 2)
217 char* pEnd =
nullptr;
219 const long v = std::strtol(currentByteString.c_str(), &pEnd, 16);
220 const bool hasError = v > std::numeric_limits<unsigned char>::max()
221 || v < std::numeric_limits<unsigned char>::min()
226 return NxGuidHelper::nullGuid();
228 guid.
bytes[currentByteIndex] = (
unsigned char) v;
230 currentByteString.clear();
234 if (currentByteIndex != kGuidBytesNumber)
235 return NxGuidHelper::nullGuid();
241 static std::string toStdString(
243 unsigned int format = NxGuidFormatOption::applyAll)
245 std::stringstream ss;
246 ss << std::hex << std::setfill(
'0');
248 if (format & NxGuidFormatOption::braces)
251 if (format & NxGuidFormatOption::uppercase)
252 ss << std::uppercase;
254 for (
int i = 0; i < 4; ++i)
257 ss << static_cast<unsigned int>(guid.
bytes[i]);
260 if (format & NxGuidFormatOption::hyphens)
263 for (
int i = 0; i < 2; ++i)
266 ss << static_cast<unsigned int>(guid.
bytes[4 + i]);
269 if (format & NxGuidFormatOption::hyphens)
272 for (
int i = 0; i < 2; ++i)
275 ss << static_cast<unsigned int>(guid.
bytes[6 + i]);
278 if (format & NxGuidFormatOption::hyphens)
281 for (
int i = 0; i < 2; ++i)
284 ss << static_cast<unsigned int>(guid.
bytes[8 + i]);
287 if (format & NxGuidFormatOption::hyphens)
290 for (
int i = 0; i < 6; ++i)
293 ss << static_cast<unsigned int>(guid.
bytes[10 + i]);
296 if (format & NxGuidFormatOption::braces)
311 inline std::ostream& operator<<(std::ostream& os,
const nxpl::NX_GUID&
id)
313 return os << nxpt::toStdString(
id);
327 for (
size_t i = 0; i <
sizeof(guid.
bytes); ++i)
328 h = (h + (324723947 + guid.
bytes[i])) ^ 93485734985;
virtual int addRef() const =0
Increment reference counter.
unsigned char bytes[16]
GUID bytes.
Definition: plugin_api.h:29
Definition: plugin_tools.h:318
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:176
Definition: plugin_tools.h:130
CommonRefManager(CommonRefManager *refCountingDelegate)
Definition: plugin_tools.h:88
Definition: plugin_tools.h:67
int addRef() const
Definition: plugin_tools.h:94
Definition: plugin_tools.h:26
int releaseRef() const
Definition: plugin_tools.h:105