Skip to content

Commit

Permalink
Finished implementation of AutoSelfUpdate, which is used to provide p…
Browse files Browse the repository at this point in the history
…rior data.
  • Loading branch information
Gabriel Hare authored and codemercenary committed Aug 5, 2014
1 parent 43879a2 commit eeb315f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
37 changes: 30 additions & 7 deletions autowiring/AutoSelfUpdate.h
@@ -1,28 +1,51 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "Autowiring/NewAutoFilter.h"
#include "Autowiring/atomic_object.h"
#include "Autowiring/shared_object.h"

///<summary>
///Enables an automatic self-update when a packet is decorated with the object type.
///Provides the prior object (the last decorated instance) to all subsequent packets.
///</summary>
///<remarks>
///In order to ensure that this method will be consistent with any other AutoFilter calls,
///the object inherits from atomic_object, which implements basic locking functionality.
///the object inherits from shared_object, which implements basic locking functionality.
///</remarks>
template<class object, class lock = std::mutex>
class AutoSelfUpdate:
public atomic_object<object, lock> {
public shared_object<object, lock> {
protected:
using shared_object<object, lock>::get_lock;
using shared_object<object, lock>::get_object;

public:
AutoSelfUpdate() {}
AutoSelfUpdate(const shared_object<object, lock>& source) : shared_object<object, lock>(source) {}
AutoSelfUpdate(const object& source) : shared_object<object, lock>(source) {}
using shared_object<object, lock>::operator =;
using shared_object<object, lock>::operator object;

//The distinct type assigned to the prior value of the object
class prior_object: public object {
public:
prior_object(const object& source): object(source) {}
};

//Avoid intermediate copy by defining an explicit cast
operator prior_object() const {
std::lock_guard<lock> lock_this(get_lock());
return prior_object(get_object());
}

//Decorates all packets with instances of prior_object
void AutoFilter(AutoPacket& packet) {
//TODO: Decorate with prior<object>
packet.Decorate(this->operator prior_object());
}

void SelfUpdate(object& update) {
atomic_object<object, lock>::operator = (update);
//Updates this object
void AutoGather(const object& update) {
shared_object<object, lock>::operator = (update);
}

NewAutoFilter<decltype(&AutoSelfUpdate<object>::SelfUpdate), &AutoSelfUpdate<object>::SelfUpdate> AutoFilter_SelfUpdate;
NewAutoFilter<decltype(&AutoSelfUpdate<object>::AutoGather), &AutoSelfUpdate<object>::AutoGather> SelfUpdate;
};
6 changes: 6 additions & 0 deletions autowiring/shared_object.h
Expand Up @@ -29,9 +29,15 @@ class shared_object {
object& get_object() {
return m_share->m_object;
}
const object& get_object() const {
return m_share->m_object;
}
bool& get_initialized() {
return m_share->m_initialized;
}
const bool& get_initialized() const {
return m_share->m_initialized;
}

public:
typedef unlock_object<object, lock> unlock;
Expand Down

0 comments on commit eeb315f

Please sign in to comment.