3 #ifndef INCLUDE_NN_NLIB_HANDLEMAKER_H_
4 #define INCLUDE_NN_NLIB_HANDLEMAKER_H_
10 namespace handle_maker {
33 return body->IsHandleBodyEnabled();
38 static const size_t size = HBODY::N;
46 int32_t transaction_count;
63 #define NLIB_HANDLETABLE_INITIALIZER {0, 0, 0, {{0, 0, NULL}}}
78 HandleAccess<HBODY> tmp(ptr_, handle_table_);
85 HandleAccess(detail::HandleData<HBODY>* ptr, HandleTable<HBODY>* handle_table) NLIB_NOEXCEPT
86 : ptr_(ptr), handle_table_(handle_table) {}
88 HandleTable<HBODY>* handle_table) NLIB_NOEXCEPT {
91 handle_table_ = handle_table;
92 handle_maker::LockHandleBody<HBODY>(ptr->item);
95 detail::HandleData<HBODY>* ptr_;
96 HandleTable<HBODY>* handle_table_;
98 friend class HandleMaker<HBODY>;
102 template<
class HBODY>
104 int32_t old_handle = ptr_->handle;
105 if (old_handle != 0) {
114 template<
class HBODY>
116 #if (defined(_MSC_VER) && _MSC_VER >= 1700) || \
117 (defined(__GLIBCXX__) && __GLIBCXX__ >= 20140911) || \
118 defined(_LIBCPP_VERSION)
135 handle = (handle ^ ptr_->mask) >> 2;
136 return static_cast<size_t>(handle) & (N - 1);
139 if (++ptr_->base_count == 1024 * 1024) ptr_->base_count = 0;
140 int salt = ptr_->base_count;
141 salt |= ((salt + 1) << 10) | ((salt + 2) << 20);
142 int handle =
static_cast<int>(idx + (salt * N));
143 return (handle << 2) ^ ptr_->mask;
146 HBODY* x = data->item;
147 handle_maker::UnlockHandleBody<HBODY>(x);
152 handle_maker::DestroyHandleBody<HBODY>(x);
157 HandleTable<HBODY>* ptr_;
158 friend class HandleAccess<HBODY>;
161 template<
class HBODY>
165 template<
class HBODY>
170 int32_t expected = 0;
171 int32_t mask = detail::GenerateHandleMakerMask();
177 detail::HandleData<HBODY>* data;
180 idx = (ptr_->cur) & (N - 1);
182 data = &ptr_->table[idx];
183 int32_t expected = 0;
187 size_t from = idx + 1;
188 for (; idx < N; ++idx) {
189 data = &ptr_->table[idx];
193 goto CREATE_HANDLE_SUCCESS;
196 for (idx = 0; idx < from; ++idx) {
197 data = &ptr_->table[idx];
201 goto CREATE_HANDLE_SUCCESS;
204 for (; idx < N; ++idx) {
205 data = &ptr_->table[idx];
209 goto CREATE_HANDLE_SUCCESS;
212 for (idx = 0; idx < from; ++idx) {
213 data = &ptr_->table[idx];
217 goto CREATE_HANDLE_SUCCESS;
224 CREATE_HANDLE_SUCCESS:
226 data->handle = this->GetHandleFromIdx(idx);
228 *handle = data->handle;
232 template<
class HBODY>
237 detail::HandleData<HBODY>* data;
243 idx = this->GetIdxFromHandle(handle);
248 data = &ptr_->table[idx];
250 if (old_cnt <= 0)
return EBADF;
255 if (old_cnt <= 0)
return EBADF;
258 access->Init(data, ptr_);
267 template<
class HBODY>
277 #endif // INCLUDE_NN_NLIB_HANDLEMAKER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
A total number of handles that access the HBODY type can be obtained through the data member size of ...
void LockHandleBody(HBODY *body) noexcept
Called when the HandleAccess object is given by HandleMaker::GetHandleAccess().
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
void UnlockHandleBody(HBODY *body) noexcept
Executed when the HandleAccess object is destructed.
A class supporting the implementation of handles with a 32-bit integer value.
void DestroyHandleBody(HBODY *body) noexcept
Called from the HandleMaker object to destroy a handle instance.
bool IsHandleBodyEnabled(HBODY *body) noexcept
Returns if the handle instance is valid or not.
A table referencing handle instances used by the HandlerMaker class.
A file that contains the configuration information for each development environment.
TimeSpan operator*(int i, const TimeSpan &rhs) noexcept
Increases rhs by a factor of i.
An accessor class that can access the handle instance as if it is a pointer. It becomes available aft...
HandleAccess() noexcept
Instantiates the object with default parameters (default constructor).
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.