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

Introduce query manager

parent b89993f0
......@@ -3,3 +3,26 @@
`smtk::resource::Set` has been removed from SMTK. It's original purpose was to
hold a collection of resources for export. This functionality has been
superceded by Project.
### Added `smtk::resource::query::Manager`
`smtk::resource::query::Manager` is an interface for registering Query types.
SMTK Plugins can interact with `smtk::resource::query::Manager` by adding
methods similar to the following to their Registrar:
```
registerTo(const smtk::resource::query::Manager::Ptr& manager)
{
manager->registerQuery<ApplicableResourceType, MyQuery>();
}
unregisterFrom(const smtk::resource::query::Manager::Ptr& manager)
{
manager->unregisterQuery<MyQuery>();
}
```
Additionally, the `smtk_add_plugin()` call for the plugin should be extended
to include `smtk::resource::query::Manager` in its list of managers.
Upon registration, all appropriate resources associated with the same
resource manager as the `smtk::resource::query::Manager` will be able to
construct instances of the newly registered Query.
......@@ -14,6 +14,7 @@
#include "smtk/extension/paraview/appcomponents/smtkPQComponentsExtModule.h"
#include "smtk/common/Managers.h"
#include "smtk/resource/query/Manager.h"
#include "smtk/view/Manager.h"
namespace smtk
......
......@@ -140,6 +140,7 @@ smtk_add_plugin(smtkPQComponentsPlugin
smtk::geometry::Manager
smtk::operation::Manager
smtk::resource::Manager
smtk::resource::query::Manager
smtk::view::Manager
PARAVIEW_PLUGIN_ARGS
VERSION "1.0"
......
......@@ -7,6 +7,7 @@ smtk_add_plugin(smtkPVMeshExtPlugin
MANAGERS
smtk::geometry::Manager
smtk::operation::Manager
smtk::resource::query::Manager
smtk::view::Manager
PARAVIEW_PLUGIN_ARGS
VERSION 1.0
......
......@@ -22,6 +22,7 @@ vtk_module_add_module(smtkPVModelExt
vtk_module_link(smtkPVModelExt
PUBLIC
vtkSMTKGeometryExt
nlohmann_json
)
......
......@@ -5,6 +5,7 @@ smtk_add_plugin(smtkPVModelExtPlugin
REGISTRAR
smtk::extension::paraview::model::Registrar
MANAGERS
smtk::resource::query::Manager
smtk::view::Manager
PARAVIEW_PLUGIN_ARGS
VERSION 1.0
......
......@@ -17,6 +17,10 @@
#include "smtk/mesh/core/Resource.h"
#include "smtk/extension/vtk/geometry/BoundingBox.h"
#include "smtk/extension/vtk/geometry/ClosestPoint.h"
#include "smtk/extension/vtk/geometry/DistanceTo.h"
namespace smtk
{
namespace extension
......@@ -25,6 +29,11 @@ namespace vtk
{
namespace geometry
{
namespace
{
typedef std::tuple<BoundingBox, /*ClosestPoint,*/ DistanceTo> QueryList;
}
void Registrar::registerTo(const smtk::geometry::Manager::Ptr& geometryManager)
{
geometryManager->registerBackend<smtk::extension::vtk::geometry::Backend>();
......@@ -34,6 +43,23 @@ void Registrar::unregisterFrom(const smtk::geometry::Manager::Ptr& geometryManag
{
geometryManager->unregisterBackend<smtk::extension::vtk::geometry::Backend>();
}
void Registrar::registerTo(const smtk::resource::query::Manager::Ptr& queryManager)
{
queryManager->registerQueriesIf<QueryList>([](smtk::resource::Resource& resource) -> bool {
if (auto geometryResource = dynamic_cast<smtk::geometry::Resource*>(&resource))
{
smtk::extension::vtk::geometry::Backend vtk;
return !!geometryResource->geometry(vtk);
}
return false;
});
}
void Registrar::unregisterFrom(const smtk::resource::query::Manager::Ptr& queryManager)
{
queryManager->unregisterQueries<QueryList>();
}
}
}
}
......
......@@ -15,6 +15,8 @@
#include "smtk/geometry/Manager.h"
#include "smtk/resource/query/Manager.h"
namespace smtk
{
namespace extension
......@@ -29,6 +31,9 @@ class VTKSMTKGEOMETRYEXT_EXPORT Registrar
public:
static void registerTo(const smtk::geometry::Manager::Ptr&);
static void unregisterFrom(const smtk::geometry::Manager::Ptr&);
static void registerTo(const smtk::resource::query::Manager::Ptr&);
static void unregisterFrom(const smtk::resource::query::Manager::Ptr&);
};
}
}
......
......@@ -17,6 +17,7 @@ set(resourceSrcs
json/jsonResourceLinkBase.cxx
json/jsonSurrogate.cxx
query/Factory.cxx
query/Manager.cxx
query/Query.cxx
)
......@@ -50,6 +51,7 @@ set(resourceHeaders
query/Container.h
query/Factory.h
query/DerivedFrom.h
query/Manager.h
query/Metadata.h
query/Queries.h
query/Query.h
......
......@@ -12,6 +12,7 @@
#include "smtk/resource/Registrar.h"
#include "smtk/resource/Manager.h"
#include "smtk/resource/query/Manager.h"
#include "smtk/plugin/Manager.h"
......@@ -24,6 +25,11 @@ void Registrar::registerTo(const smtk::common::Managers::Ptr& managers)
managers->insert(smtk::resource::Manager::create());
smtk::plugin::Manager::instance()->registerPluginsTo(
managers->get<smtk::resource::Manager::Ptr>());
managers->insert(
smtk::resource::query::Manager::create(managers->get<smtk::resource::Manager::Ptr>()));
smtk::plugin::Manager::instance()->registerPluginsTo(
managers->get<smtk::resource::query::Manager::Ptr>());
}
void Registrar::unregisterFrom(const smtk::common::Managers::Ptr& managers)
......
//=========================================================================
// 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.
//=========================================================================
#include "smtk/resource/query/Manager.h"
namespace smtk
{
namespace resource
{
namespace query
{
Manager::Manager(const smtk::resource::ManagerPtr& manager)
: m_manager(manager)
{
}
Manager::~Manager() = default;
}
}
}
//=========================================================================
// 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_query_Manager_h
#define __smtk_resource_query_Manager_h
#include "smtk/CoreExports.h"
#include "smtk/PublicPointerDefs.h"
#include <smtk/common/TypeName.h>
#include "smtk/resource/Manager.h"
#include "smtk/resource/Observer.h"
#include "smtk/resource/query/Queries.h"
#include <string>
namespace smtk
{
namespace resource
{
namespace query
{
/// A manager for registering Query types.
///
/// smtk::resource::query::Manager is an interface for registering Query types.
/// SMTK Plugins can interact with smtk::resource::query::Manager by adding
/// methods similar to the following to their Registrar:
///
/// registerTo(const smtk::resource::query::Manager::Ptr& manager)
/// {
/// manager->registerQuery<ApplicableResourceType, MyQuery>();
/// }
///
/// unregisterFrom(const smtk::resource::query::Manager::Ptr& manager)
/// {
/// manager->unregisterQuery<MyQuery>();
/// }
///
/// Additionally, the `smtk_add_plugin()` call for the plugin should be extended
/// to include `smtk::resource::query::Manager` in its list of managers.
/// Upon registration, all appropriate resources associated with the same
/// resource manager as the smtk::resource::query::Manager will be able to
/// construct instances of the newly registered Query.
class SMTKCORE_EXPORT Manager : std::enable_shared_from_this<Manager>
{
public:
smtkTypedefs(smtk::resource::query::Manager);
static std::shared_ptr<Manager> create(const smtk::resource::ManagerPtr& resourceManager)
{
return smtk::shared_ptr<Manager>(new Manager(resourceManager));
}
virtual ~Manager();
/// Register <QueryType> to all resources for which the input functor <fn>
/// returns true.
template <typename QueryType>
bool registerQueryIf(std::function<bool(smtk::resource::Resource&)> fn)
{
auto registerQueryType = [fn](
const smtk::resource::Resource& rsrc, smtk::resource::EventType eventType) -> void {
if (eventType == smtk::resource::EventType::ADDED)
{
smtk::resource::Resource& resource = const_cast<smtk::resource::Resource&>(rsrc);
if (fn(resource))
{
resource.queries().registerQuery<QueryType>();
}
}
};
if (auto manager = m_manager.lock())
{
m_observers.insert(std::make_pair(typeid(QueryType).hash_code(),
manager->observers().insert(registerQueryType,
"Register query type <" + smtk::common::typeName<QueryType>() + ">.")));
return true;
}
return false;
}
/// Register <QueryType> for all resources derived from <ResourceType>.
template <typename ResourceType, typename QueryType>
bool registerQuery()
{
return registerQueryIf<QueryType>(
[](smtk::resource::Resource& resource) -> bool { return resource.isOfType<ResourceType>(); });
}
/// Unregister <QueryType> from all appropriate resources.
template <typename QueryType>
bool unregisterQuery()
{
if (auto manager = m_manager.lock())
{
for (auto resource : manager->resources())
{
resource->queries().template unregisterQuery<QueryType>();
}
m_observers.erase(typeid(QueryType).hash_code());
return true;
}
return false;
}
/// Register <QueryTypes> to all resources for which the input functor <fn>
/// returns true.
template <typename QueryTypes>
bool registerQueriesIf(std::function<bool(smtk::resource::Resource&)> fn)
{
auto registerQueryTypes = [fn](
const smtk::resource::Resource& rsrc, smtk::resource::EventType eventType) -> void {
if (eventType == smtk::resource::EventType::ADDED)
{
smtk::resource::Resource& resource = const_cast<smtk::resource::Resource&>(rsrc);
if (fn(resource))
{
resource.queries().registerQueries<QueryTypes>();
}
}
};
if (auto manager = m_manager.lock())
{
m_observers.insert(std::make_pair(typeid(QueryTypes).hash_code(),
manager->observers().insert(registerQueryTypes,
"Register query types <" + smtk::common::typeName<QueryTypes>() + ">.")));
return true;
}
return false;
}
/// Register <QueryTypes> for all resources derived from <ResourceType>.
template <typename ResourceType, typename QueryTypes>
bool registerQueries()
{
return registerQueriesIf<QueryTypes>(
[](smtk::resource::Resource& resource) -> bool { return resource.isOfType<ResourceType>(); });
}
/// Unregister <QueryTypes> from all appropriate resources.
template <typename QueryTypes>
bool unregisterQueries()
{
if (auto manager = m_manager.lock())
{
for (auto resource : manager->resources())
{
resource->queries().template unregisterQueries<QueryTypes>();
}
m_observers.erase(typeid(QueryTypes).hash_code());
return true;
}
return false;
}
protected:
Manager(const std::shared_ptr<smtk::resource::Manager>&);
std::weak_ptr<smtk::resource::Manager> m_manager;
std::unordered_map<std::size_t, smtk::resource::Observers::Key> m_observers;
};
}
}
}
#endif
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