Skip to content

Commit

Permalink
cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverIce committed Mar 14, 2016
1 parent d55d726 commit c14aa07
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 59 deletions.
7 changes: 3 additions & 4 deletions JContainers/src/collections/dyn_form_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ namespace form_watching {
};

class form_ref {

form_entry_ref _watched_form;

public:

explicit form_ref(form_entry_ref&& entry) : _watched_form(std::move(entry)) {}
explicit form_ref(const form_entry_ref& entry) : _watched_form(entry) {}

form_ref& operator = (form_ref&& entry) {
if (this != &entry) {
Expand All @@ -75,8 +75,7 @@ namespace form_watching {
}

form_ref() = default;

explicit form_ref(const form_entry_ref& entry) : _watched_form(entry) {}
form_ref& operator = (const form_ref &) = default;

form_ref(FormId id, form_observer& watcher);
form_ref(const TESForm& form, form_observer& watcher);
Expand Down Expand Up @@ -154,7 +153,7 @@ namespace form_watching {

// "Stupid" form_ref comparison functions:
// the functions don't care whether the @form_refs are really equal or not -
// really equal form_refs have equal @_watched_form field
// really equal form_refs have equal @_watched_form fields
// The comparison is NOT stable
namespace comp {
template<class FormRef1, class FormRef2>
Expand Down
23 changes: 16 additions & 7 deletions JContainers/src/collections/dyn_form_watcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,20 @@ namespace collections {

auto formId = fh::form_handle_to_id(handle);
{
// Since it's impossible that two threads will delete the same form simultaneosly
// we can skip some thread safe stuff
auto itr = _watched_forms.find(formId);
if (itr != _watched_forms.end()) {
// read and write

std::lock_guard<boost::detail::spinlock> guard{ spinlock_for(formId) };
auto watched = itr->second.lock();

if (watched) {
watched->set_deleted();
log("flag form-entry %" PRIX32 " as deleted", formId);
itr->second.reset();

log("flagged form-entry %" PRIX32 " as deleted", formId);
}
itr->second.reset();
}
}
}
Expand Down Expand Up @@ -253,24 +255,31 @@ namespace collections {
return nullptr;
}

log("querying form-entry %" PRIX32, fId);

{
std::lock_guard<boost::detail::spinlock> guard{ spinlock_for(fId) };

auto itr = _watched_forms.find(fId);
if (itr != _watched_forms.end()) {
auto watched = itr->second.lock();
if (!watched) {
// what if two threads trying assign??
// both threads are here or one is here and another performing @on_form_deleted func.
// form-entry and real form has been deleted (recently)
// watch the form again, create entry, assuming that a new form with such ID exists

// this code assumes that @watch_form tries to watch real existing form
// rather than the one from JSON
itr->second = watched = form_entry::make(fId);

log("queried, re-created form-entry %" PRIX32, fId);
}
else {
log("queried form-entry %" PRIX32, fId);
}
return watched;
}
else {
auto watched = form_entry::make(fId);
_watched_forms[fId] = watched;
log("queried, created new form-entry %" PRIX32, fId);
return watched;
}
}
Expand Down
50 changes: 7 additions & 43 deletions JContainers/src/collections/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ namespace collections {
}
}

item() {}
item() = default;
item(const item& other) = default;
item& operator = (const item& other) = default;

item(item&& other) : _var(std::move(other._var)) {}
item(const item& other) : _var(other._var) {}

item& operator = (item&& other) {
if (this != &other) {
Expand All @@ -67,11 +69,6 @@ namespace collections {
return *this;
}

item& operator = (const item& other) {
_var = other._var;
return *this;
}

const variant& var() const {
return _var;
}
Expand All @@ -85,7 +82,7 @@ namespace collections {
}

item_type type() const {
return item_type(_var.which() + 1);
return item_type(_var.which() + item_type::none);
}

template<class T> T* get() {
Expand Down Expand Up @@ -114,7 +111,6 @@ namespace collections {
explicit item(SInt32 val) : _var(val) {}
explicit item(int val) : _var((SInt32)val) {}
explicit item(bool val) : _var((SInt32)val) {}
// explicit item(FormId id) : _var(weak_form_id(id)) {}
explicit item(const form_ref& id) : _var(id) {}
explicit item(form_ref&& id) : _var(std::move(id)) {}

Expand All @@ -124,10 +120,6 @@ namespace collections {
explicit item(std::string&& val) : _var(std::move(val)) {}

// the Item is none if the pointers below are zero:
/*
explicit item(const TESForm *val) {
*this = val;
}*/
explicit item(const char * val) {
*this = val;
}
Expand All @@ -140,10 +132,6 @@ namespace collections {
explicit item(const object_stack_ref &val) {
*this = val.get();
}
/*
explicit Item(const BSFixedString& val) : _var(boost::blank()) {
*this = val.data;
}*/

item& operator = (unsigned int val) { _var = (SInt32)val; return *this; }
item& operator = (int val) { _var = (SInt32)val; return *this; }
Expand All @@ -157,18 +145,6 @@ namespace collections {
item& operator = (boost::none_t) { _var = boost::blank(); return *this; }
item& operator = (object_base& v) { _var = &v; return *this; }

/*
item& operator = (FormId formId) {
// prevent zero FormId from being saved
if (formId != FormId::Zero) {
_var = weak_form_id{ formId };
}
else {
_var = blank();
}
return *this;
}*/

template<class T>
item& _assignPtr(T *ptr) {
if (ptr) {
Expand All @@ -193,17 +169,6 @@ namespace collections {
return _assignPtr(val);
}

/*
item& operator = (const TESForm *val) {
if (val) {
_var = weak_form_id{ *val };
}
else {
_var = blank();
}
return *this;
}*/

object_base *object() const {
if (auto ref = boost::get<internal_object_ref>(&_var)) {
return ref->get();
Expand All @@ -218,7 +183,7 @@ namespace collections {
else if (auto val = boost::get<SInt32>(&_var)) {
return *val;
}
return 0;
return 0.f;
}

SInt32 intValue() const {
Expand All @@ -242,8 +207,7 @@ namespace collections {
}

TESForm * form() const {
auto frmId = formId();
return frmId != FormId::Zero ? skse::lookup_form(frmId) : nullptr;
return skse::lookup_form(formId());
}

FormId formId() const {
Expand Down
2 changes: 1 addition & 1 deletion JContainers/src/skse/skse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace skse {
}

TESForm* lookup_form(FormId handle) {
return g_current_api->lookup_form(handle);
return handle != FormId::Zero ? g_current_api->lookup_form(handle) : nullptr;
}

const char * modname_from_index(uint8_t idx) {
Expand Down
8 changes: 4 additions & 4 deletions JContainers/src/util/stl_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace util {

namespace {
namespace aux {
struct tree_erase_if_eraser {
template<class TreeContainer>
typename TreeContainer::iterator operator()(TreeContainer& cnt, typename TreeContainer::const_iterator itr) const {
Expand All @@ -13,7 +13,7 @@ namespace util {
};
}

template<class TreeContainer, class Predicate, class Eraser = tree_erase_if_eraser>
template<class TreeContainer, class Predicate, class Eraser = aux::tree_erase_if_eraser>
void tree_erase_if(TreeContainer&& container, Predicate&& pred, Eraser&& eraser = Eraser{}) {
for (auto itr = container.begin(); itr != container.end();) {
if (pred(*itr)) {
Expand All @@ -25,7 +25,7 @@ namespace util {
}
}

namespace {
namespace aux {

template<typename S, typename D>
struct copy_const_qual {
Expand All @@ -46,7 +46,7 @@ namespace util {

template<
typename Enum,
typename Number = typename copy_const_qual < Enum, typename std::underlying_type<Enum>::type >::type
typename Number = typename aux::copy_const_qual < Enum, typename std::underlying_type<Enum>::type >::type
>
inline auto to_integral_ref(Enum & e) -> Number & {
return reinterpret_cast<Number &>(e);
Expand Down

0 comments on commit c14aa07

Please sign in to comment.