factor the specific query into separate header

This commit is contained in:
Fischlurch 2010-06-19 05:37:39 +02:00
parent c80b1894e6
commit c1cb5320e0
3 changed files with 116 additions and 64 deletions

View file

@ -46,7 +46,6 @@
#include "proc/mobject/session/query-resolver.hpp"
#include <tr1/functional>
#include "lib/meta/function-closure.hpp" //////////////////////TODO
namespace mobject {
@ -216,65 +215,5 @@ namespace session {
};
template<class MO>
class SpecificContentsQuery
: public ContentsQuery<MO>
{
typedef typename ContentsQuery<MO>::ContentFilter ContentFilter;
typedef Placement<MO> const& TypedPlacement;
typedef function<bool(TypedPlacement)> SpecialPredicate;
/**
* Filter functor, built on top of a predicate,
* which is provided by the client on creation of this
* SpecivicContentsQuery instance. This allows for filtering
* based on operations of the specific type MO, as opposed to
* just using the bare MObject interface.
*/
class Filter
{
SpecialPredicate predicate_;
public:
Filter (SpecialPredicate const& pred)
: predicate_(pred)
{ }
bool
operator() (PlacementMO const& anyMO)
{
if (!anyMO.isCompatible<MO>())
return false;
TypedPlacement interestingObject = static_cast<TypedPlacement> (anyMO);
return predicate_(interestingObject);
}
};
Filter specialTest_;
/** using a specialised version of the filtering,
* which doesn't only check the concrete type,
* but also applies a custom filter predicate
* @return function object, embedding a copy
* of the Filter functor.
*/
ContentFilter
buildContentFilter() const
{
return specialTest_;
}
public:
SpecificContentsQuery (PlacementMO const& scope
,SpecialPredicate const& specialPred)
: ContentsQuery<MO> (scope)
, specialTest_(specialPred)
{ }
};
}} // namespace mobject::session
#endif

View file

@ -0,0 +1,111 @@
/*
SPECIFIC-CONTENTS-QUERY.hpp - pick specific contents from the model, using a filter
Copyright (C) Lumiera.org
2010, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef MOBJECT_SESSION_SPECIFIC_CONTENTS_QUERY_H
#define MOBJECT_SESSION_SPECIFIC_CONTENTS_QUERY_H
#include "proc/mobject/session/scope-query.hpp"
#include "proc/mobject/placement.hpp"
#include <tr1/functional>
namespace mobject {
namespace session {
using std::tr1::function;
/**
* Specialised version of the ContentsQuery to pick some objects
* from the session, based on a filter predicate. As the parent type,
* ContentsQuery, the resolution of this query requires to explore the
* given scope depth first; but in addition to filter based on type,
* a client-provided predicate is applied to each result.
* @note this may degenerate on large sessions.
* @todo develop a system of sub-indices and specialised queries
*/
template<class MO>
class SpecificContentsQuery
: public ContentsQuery<MO>
{
typedef typename ContentsQuery<MO>::ContentFilter ContentFilter;
typedef Placement<MO> const& TypedPlacement;
typedef function<bool(TypedPlacement)> SpecialPredicate;
/**
* Filter functor, built on top of a predicate,
* which is provided by the client on creation of this
* SpecivicContentsQuery instance. This allows for filtering
* based on operations of the specific type MO, as opposed to
* just using the bare MObject interface.
*/
class Filter
{
SpecialPredicate predicate_;
public:
Filter (SpecialPredicate const& pred)
: predicate_(pred)
{ }
bool
operator() (PlacementMO const& anyMO)
{
if (!anyMO.isCompatible<MO>())
return false;
TypedPlacement interestingObject = static_cast<TypedPlacement> (anyMO);
return predicate_(interestingObject);
}
};
Filter specialTest_;
/** using a specialised version of the filtering,
* which doesn't only check the concrete type,
* but also applies a custom filter predicate
* @return function object, embedding a copy
* of the Filter functor. */
ContentFilter
buildContentFilter() const
{
return specialTest_;
}
public:
SpecificContentsQuery (PlacementMO const& scope
,SpecialPredicate const& specialPred)
: ContentsQuery<MO> (scope)
, specialTest_(specialPred)
{ }
};
}} // namespace mobject::session
#endif

View file

@ -22,8 +22,9 @@
#include "lib/test/run.hpp"
#include "proc/mobject/session/session-service-explore-scope.hpp"
#include "proc/mobject/session/scope-query.hpp"
#include "proc/mobject/session/specific-contents-query.hpp"
#include "proc/mobject/session/session-service-explore-scope.hpp"
#include "proc/mobject/session/test-scopes.hpp"
#include "proc/mobject/session/clip.hpp"
#include "lib/symbol.hpp"
@ -76,6 +77,7 @@ namespace test {
}
/**********************************************************************************************
* @test how to discover contents or location of a container-like part of the high-level model.
* As this container-like object is just a concept and actually implemented by the
@ -91,7 +93,6 @@ namespace test {
*/
class ScopeQuery_test : public Test
{
virtual void
run (Arg)
{
@ -107,7 +108,7 @@ namespace test {
discover (ScopeQuery<TestSubMO2> (scope, CONTENTS) , "contents depth-first, filtered to TestSubMO2");
discover (SpecificContentsQuery<DummyMO> (scope, filter) , "contents depth-first, custom filtered DummyMO");
// note the filter is typed to accept DummyMO
ScopeQuery<TestSubMO21> allM021(scope, CONTENTS);
ScopeQuery<TestSubMO21>::iterator specialEl (issue(allM021));
++specialEl; // step in to second solution found...
@ -122,6 +123,7 @@ namespace test {
}
template<class MO>
static void
discover (ScopeQuery<MO> const& query, Literal description)