Commit c4c5e643 authored by T.J. Corona's avatar T.J. Corona

Resource: add observer for metadata actions (add/remove)

parent 85ddb3c1
......@@ -24,6 +24,7 @@ set(resourceHeaders
Manager.h
Metadata.h
MetadataContainer.h
MetadataObserver.h
Observer.h
PersistentObject.h
PropertyType.h
......
......@@ -52,6 +52,7 @@ bool Manager::unregisterResource(const std::string& typeName)
if (metadata != m_metadata.get<NameTag>().end())
{
m_metadata.get<NameTag>().erase(metadata);
m_metadataObservers(*metadata, false);
return true;
}
......@@ -65,6 +66,7 @@ bool Manager::unregisterResource(const Resource::Index& index)
if (metadata != m_metadata.get<IndexTag>().end())
{
m_metadata.get<IndexTag>().erase(metadata);
m_metadataObservers(*metadata, false);
return true;
}
......@@ -148,9 +150,12 @@ bool Manager::registerResource(Metadata&& metadata)
auto alreadyRegisteredMetadata = m_metadata.get<IndexTag>().find(metadata.index());
if (alreadyRegisteredMetadata == m_metadata.get<IndexTag>().end())
{
auto size = m_metadata.get<IndexTag>().size();
m_metadata.get<IndexTag>().insert(metadata);
return m_metadata.get<IndexTag>().size() > size;
auto inserted = m_metadata.get<IndexTag>().insert(metadata);
if (inserted.second)
{
m_metadataObservers(*inserted.first, true);
return true;
}
}
return false;
......
......@@ -179,6 +179,10 @@ public:
Observers& observers() { return m_observers; }
const Observers& observers() const { return m_observers; }
/// Return the metadata observers associated with this manager.
Metadata::Observers& metadataObservers() { return m_metadataObservers; }
const Metadata::Observers& metadataObservers() const { return m_metadataObservers; }
private:
Manager();
......@@ -195,6 +199,9 @@ private:
/// A container for all resource observers.
Observers m_observers;
/// A container for all resource metadata observers.
Metadata::Observers m_metadataObservers;
/// A map connecting legacy resource names to legacy readers.
std::map<std::string, std::function<ResourcePtr(const std::string&)> > m_legacyReaders;
};
......
......@@ -14,6 +14,7 @@
#include "smtk/CoreExports.h"
#include "smtk/PublicPointerDefs.h"
#include "smtk/common/UUID.h"
#include "smtk/resource/MetadataObserver.h"
#include "smtk/resource/Resource.h"
#include <functional>
......@@ -38,6 +39,8 @@ class Metadata
public:
typedef std::function<void(const Metadata&)> Visitor;
typedef MetadataObserver Observer;
typedef MetadataObservers Observers;
Metadata(const std::string& typeName, Resource::Index index,
std::set<Resource::Index> parentIndices,
......
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#ifndef __smtk_resource_MetadataObserver_h
#define __smtk_resource_MetadataObserver_h
#include "smtk/CoreExports.h"
#include "smtk/common/Observers.h"
namespace smtk
{
namespace resource
{
class Metadata;
typedef std::function<void(const Metadata&, bool)> MetadataObserver;
typedef smtk::common::Observers<MetadataObserver> MetadataObservers;
}
}
#ifndef smtkCore_EXPORTS
extern
#endif
template class SMTKCORE_EXPORT std::function<void(const smtk::resource::Metadata&)>;
#endif // __smtk_resource_MetadataObserver_h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment