23 #include <type_traits> 25 #include "plugin_api.h" 32 typedef volatile LONG AtomicLong;
34 typedef volatile long AtomicLong;
36 #error "Unsupported compiler is used." 40 static AtomicLong
inc(AtomicLong* val)
43 return InterlockedIncrement(val);
45 return __sync_add_and_fetch(val, 1);
50 static AtomicLong
dec(AtomicLong* val)
53 return InterlockedDecrement(val);
55 return __sync_sub_and_fetch(val, 1);
80 m_objToWatch(objToWatch),
81 m_refCountingDelegate(0)
90 m_refCountingDelegate(refCountingDelegate)
97 return m_refCountingDelegate
98 ? m_refCountingDelegate->
addRef()
99 : atomic::inc(&m_refCount);
108 if (m_refCountingDelegate)
111 const int newRefCounter = atomic::dec(&m_refCount);
112 if (newRefCounter == 0)
114 return newRefCounter;
119 if (m_refCountingDelegate)
120 return m_refCountingDelegate->refCount();
125 mutable atomic::AtomicLong m_refCount;
127 CommonRefManager* m_refCountingDelegate;
130 template <
typename T>
140 virtual int addRef()
const override {
return m_refManager.
addRef(); }
141 virtual int releaseRef()
const override {
return m_refManager.
releaseRef(); }
143 int refCount()
const {
return m_refManager.refCount(); }
156 template<
typename RefCountableInterface>
159 if (
object ==
nullptr)
163 return commonRefCounter->refCount();
166 return object->releaseRef();
169 enum NxGuidFormatOption
174 applyAll = uppercase | hyphens | braces
182 return {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}};
188 memcpy(result.
bytes, data,
sizeof(result.
bytes));
192 static nxpl::NX_GUID fromStdString(
const std::string& guidStr)
194 static const auto kMinGuidStrSize = 32;
195 static const auto kGuidBytesNumber = 16;
197 if (guidStr.size() < kMinGuidStrSize)
198 return NxGuidHelper::nullGuid();
201 int currentByteIndex = 0;
202 std::string currentByteString;
203 for (std::string::size_type i = 0; i < guidStr.size(); ++i)
205 if (guidStr[i] ==
'{' || guidStr[i] ==
'}' || guidStr[i] ==
'-' 206 || guidStr[i] ==
'\t' || guidStr[i] ==
'\n' || guidStr[i] ==
'r' 207 || guidStr[i] ==
' ')
212 if (currentByteIndex >= kGuidBytesNumber)
213 return NxGuidHelper::nullGuid();
215 currentByteString += guidStr[i];
216 if (currentByteString.size() == 2)
218 char* pEnd =
nullptr;
220 const long v = std::strtol(currentByteString.c_str(), &pEnd, 16);
221 const bool hasError = v > std::numeric_limits<unsigned char>::max()
222 || v < std::numeric_limits<unsigned char>::min()
227 return NxGuidHelper::nullGuid();
229 guid.
bytes[currentByteIndex] = (
unsigned char) v;
231 currentByteString.clear();
235 if (currentByteIndex != kGuidBytesNumber)
236 return NxGuidHelper::nullGuid();
242 static std::string toStdString(
244 unsigned int format = NxGuidFormatOption::applyAll)
246 std::stringstream ss;
247 ss << std::hex << std::setfill(
'0');
249 if (format & NxGuidFormatOption::braces)
252 if (format & NxGuidFormatOption::uppercase)
253 ss << std::uppercase;
255 for (
int i = 0; i < 4; ++i)
258 ss << static_cast<unsigned int>(guid.
bytes[i]);
261 if (format & NxGuidFormatOption::hyphens)
264 for (
int i = 0; i < 2; ++i)
267 ss << static_cast<unsigned int>(guid.
bytes[4 + i]);
270 if (format & NxGuidFormatOption::hyphens)
273 for (
int i = 0; i < 2; ++i)
276 ss << static_cast<unsigned int>(guid.
bytes[6 + i]);
279 if (format & NxGuidFormatOption::hyphens)
282 for (
int i = 0; i < 2; ++i)
285 ss << static_cast<unsigned int>(guid.
bytes[8 + i]);
288 if (format & NxGuidFormatOption::hyphens)
291 for (
int i = 0; i < 6; ++i)
294 ss << static_cast<unsigned int>(guid.
bytes[10 + i]);
297 if (format & NxGuidFormatOption::braces)
312 inline std::ostream& operator<<(std::ostream& os,
const nxpl::NX_GUID&
id)
314 return os << nxpt::toStdString(
id);
328 for (
size_t i = 0; i <
sizeof(guid.
bytes); ++i)
329 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:319
Base class for every interface, provided by plugin.
Definition: plugin_api.h:44
CommonRefManager(nxpl::PluginInterface *objToWatch)
Definition: plugin_tools.h:78
GUID of plugin interface.
Definition: plugin_api.h:26
VMS dynamic plugin API (c++)
Definition: plugin_api.h:23
Definition: plugin_tools.h:177
Definition: plugin_tools.h:131
CommonRefManager(CommonRefManager *refCountingDelegate)
Definition: plugin_tools.h:89
Definition: plugin_tools.h:68
int addRef() const
Definition: plugin_tools.h:95
Definition: plugin_tools.h:27
int releaseRef() const
Definition: plugin_tools.h:106