Skip to content

Commit

Permalink
Adding reset(target) and transfer(target) methods to atomic_object, a…
Browse files Browse the repository at this point in the history
…nd by extension to shared_object.

NOTE: It is not clear that these conditional transfer methods are fundamental to atomic_object. Instead, write a class that provides conditional assignment and transfer methods... which seems like a separate project.

NOTE: It will then be necessary to extend the unlock_object functionality to an atomic array of unlocks.
  • Loading branch information
Gabriel Hare authored and codemercenary committed Aug 5, 2014
1 parent 44f9d19 commit 43879a2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 12 deletions.
70 changes: 65 additions & 5 deletions autowiring/atomic_object.h
Expand Up @@ -94,6 +94,31 @@ class atomic_object {
return m_object;
}

///<summary>
///Reset using default constructor yielding initialized() == false.
///</summary>
///<return>True if the object was not assigned default values</return>
bool reset() {
std::lock_guard<lock> lock_this(m_lock);
bool was_initialized = m_initialized;
m_initialized = false;
m_object = object();
return was_initialized;
}

///<summary>
///Atomic copy of target to this object, only if initialized() == false.
///</summary>
///<return>True if the object was not assigned default values</return>
bool reset(const object& target) {
std::lock_guard<lock> lock_this(m_lock);
bool was_initialized = m_initialized;
if (!m_initialized)
m_object = target;
m_initialized = true;
return was_initialized;
}

///<return>True if the object was not assigned default values</return>
bool initialized() const {
std::lock_guard<lock> lock_this(m_lock);
Expand All @@ -104,19 +129,54 @@ class atomic_object {
///Atomic copy of this object to target, only if initialized() == true.
///</summary>
///<return>True if the object was not assigned default values</return>
bool initialized(object& target) {
bool initialized(object& target) const {
std::lock_guard<lock> lock_this(m_lock);
if (m_initialized)
target = m_object;
return m_initialized;
}

///<summary>
///Reset using default constructor yielding initialized() == false.
///If uninitialized uses target for initialization.
///If initialized assigns current value to target.
///</summary>
void reset() {
///<returns> Returns +1 for transfer from target to this, -1 for transfer from this to target</returns>
int transfer(object& target) {
std::lock_guard<lock> lock_this(m_lock);
m_initialized = false;
m_object = object();
int val = 0;
if (m_initialized) {
target = m_object;
val = +1;
} else {
m_object = target;
m_initialized = true;
val = -1;
}
return val;
}

///<summary>
///If neither this nor target are uninitialized, no transfer occurs.
///If this is uninitialized and target is not, then this is initialized by target.
///If target is uninitialized and this is, then target is initialized by this.
///If both this and target are initialized, no transfer occurs.
///</summary>
///<returns> Returns +1 for transfer from target to this, -1 for transfer from this to target, else 0</returns>
int transfer(atomic_object<object, lock>& target) {
std::lock(m_lock, target.m_lock);
int val = 0;
if (m_initialized && !target.m_initialized) {
target.m_object = m_object;
target.m_initialized = true;
val = -1;
}
if (!m_initialized && target.m_initialized) {
m_object = target.m_object;
m_initialized = true;
val = +1;
}
m_lock.unlock();
target.m_lock.unlock();
return val;
}
};
50 changes: 43 additions & 7 deletions autowiring/shared_object.h
Expand Up @@ -88,6 +88,14 @@ class shared_object {
return *m_share;
}

///<summary>
///Reset using default constructor yielding initialized() == false.
///</summary>
///<return>True if the object was not assigned default values</return>
bool reset() {
return m_share->reset();
}

///<return>True if the object was not assigned default values</return>
bool initialized() const {
return m_share->initialized();
Expand All @@ -97,18 +105,46 @@ class shared_object {
///Atomic copy of target to this object, only if initialized() == false.
///</summary>
///<return>True if the object was not assigned default values</return>
bool reset(const object& target) {
return m_share->reset(target);
}

///<summary>
///Atomic copy of this object to target, only if initialized() == true.
///</summary>
///<return>True if the object was not assigned default values</return>
bool initialized(object& target) {
return m_share->initialized(target);
}

///<summary>
///Reset using default constructor yielding initialized() == false.
///If uninitialized uses target for initialization.
///If initialized assigns current value to target.
///</summary>
///<remarks>
///This method is equivalent to:
/// unlock_object<object>(source)->reset();
///</remarks>
void reset() {
m_share->reset();
///<returns> Returns +1 for transfer from target to this, -1 for transfer from this to target</returns>
int transfer(object& target) {
return m_share->transfer(target);
}

///<summary>
///If neither this nor target are uninitialized, no transfer occurs.
///If this is uninitialized and target is not, then this is initialized by target.
///If target is uninitialized and this is, then target is initialized by this.
///If both this and target are initialized, no transforer occurs.
///</summary>
///<returns> Returns +1 for transfer from target to this, -1 for transfer from this to target, else 0</returns>
int transfer(atomic_object<object, lock>& target) {
return m_share->transfer(target);
}

///<summary>
///If neither this nor target are uninitialized, no transfer occurs.
///If this is uninitialized and target is not, then this is initialized by target.
///If target is uninitialized and this is, then target is initialized by this.
///If both this and target are initialized, no transforer occurs.
///</summary>
///<returns> Returns +1 for transfer from target to this, -1 for transfer from this to target, else 0</returns>
int transfer(shared_object<object, lock>& target) {
return m_share->transfer(target.m_share);
}
};

0 comments on commit 43879a2

Please sign in to comment.