Commit 68f08b64 authored by Andrea Valassi's avatar Andrea Valassi
Browse files

Add CompositeSelection class (AND/OR of other selections)


git-svn-id: file:///git/lcgcool.svndb/cool/trunk@14107 4525493e-7705-40b1-a816-d608a930855b
parent 31bdd30b
// $Id: CompositeSelection.h,v 1.1 2008-07-30 08:18:52 avalassi Exp $
#ifndef COOLKERNEL_COMPOSITESELECTION_H
#define COOLKERNEL_COMPOSITESELECTION_H 1
#ifdef COOL260
// Include files
#include <vector>
#include "CoolKernel/IRecordSelection.h"
namespace cool
{
//--------------------------------------------------------------------------
/** @class CompositeSelection CompositeSelection.h
*
* Composite selection on a data record obtained by the conjuction (AND)
* or disjunction (OR) of several simplere record selections.
*
* @author Andrea Valassi and Martin Wache
* @date 2008-07-30
*/
class CompositeSelection : virtual public IRecordSelection
{
public:
/// Logical connectives (logical operations).
enum Connective{ AND, OR };
/// Describe a logical connective.
static const std::string describe( const Connective conn );
public:
/// Destructor.
virtual ~CompositeSelection();
/// Constructor for connecting two selections.
/// Each input selection is cloned (the clone is owned by this instance).
CompositeSelection( const IRecordSelection* sel1,
const Connective conn,
const IRecordSelection* sel2 );
/// Constructor for connecting any number of selections.
/// The vector must contain at least two selections.
/// Each input selection is cloned (the clone is owned by this instance).
CompositeSelection( const Connective conn,
const std::vector<const IRecordSelection*> selVec );
/// Can the selection be applied to a record with the given specification?
const bool canSelect( const IRecordSpecification& spec ) const;
/// Apply the selection to the given record.
const bool select( const IRecord& record ) const;
/// Clone the record selection (and any objects referenced therein).
IRecordSelection* clone() const;
/// Logical connective between all connected selections.
const Connective connective() const;
/// The number N of connected selections.
const unsigned int size() const;
/// Return one of the connected selections by its index in [0, N-1].
const IRecordSelection* operator[] ( const unsigned int index ) const;
private:
/// Standard constuctor is private
CompositeSelection();
/// Copy constructor - reimplemented for the clone() method
CompositeSelection( const CompositeSelection& rhs );
/// Assignment operator is private
CompositeSelection& operator=( const CompositeSelection& rhs );
private:
/// The logical connective between connected selections.
Connective m_conn;
/// The vector of connected selections.
/// These are clones (owned by this instance) of the user-given selections.
std::vector<IRecordSelection*> m_selVec;
};
}
#endif
#endif // COOLKERNEL_COMPOSITESELECTION_H
// $Id: CompositeSelection.cpp,v 1.1 2008-07-30 08:18:52 avalassi Exp $
#ifdef COOL260
// Include files
#include <sstream>
#include "CoolKernel/Exception.h"
#include "CoolKernel/CompositeSelection.h"
// Namespace
using namespace cool;
//-----------------------------------------------------------------------------
const std::string
CompositeSelection::describe( CompositeSelection::Connective conn )
{
switch( conn )
{
case AND: return "AND";
case OR: return "OR";
default:
throw Exception( "PANIC! Unknown logical connective",
"CompositeSelection::describe()" );
}
}
//-----------------------------------------------------------------------------
CompositeSelection::~CompositeSelection()
{
for ( unsigned int iSel = 0; iSel < m_selVec.size(); iSel++ )
{
delete m_selVec[iSel];
m_selVec[iSel] = 0;
}
}
//-----------------------------------------------------------------------------
CompositeSelection::CompositeSelection( const IRecordSelection* sel1,
const Connective conn,
const IRecordSelection* sel2 )
: m_conn( conn )
, m_selVec()
{
m_selVec.push_back( sel1->clone() );
m_selVec.push_back( sel2->clone() );
}
//-----------------------------------------------------------------------------
CompositeSelection::CompositeSelection
( const Connective conn,
const std::vector<const IRecordSelection*> selVec )
: m_conn( conn )
, m_selVec()
{
for ( unsigned int iSel = 0; iSel < selVec.size(); iSel++ )
{
m_selVec.push_back( selVec[iSel]->clone() );
}
}
//-----------------------------------------------------------------------------
CompositeSelection::CompositeSelection( const CompositeSelection& rhs )
: IRecordSelection( rhs )
, m_conn( rhs.m_conn )
, m_selVec()
{
for ( unsigned int iSel = 0; iSel < rhs.m_selVec.size(); iSel++ )
{
m_selVec.push_back( rhs.m_selVec[iSel]->clone() );
}
}
//-----------------------------------------------------------------------------
const bool
CompositeSelection::canSelect( const IRecordSpecification& rspec ) const
{
for ( unsigned int iSel = 0; iSel < m_selVec.size(); iSel++ )
{
if ( ! m_selVec[iSel]->canSelect( rspec ) ) return false;
}
return true;
}
//-----------------------------------------------------------------------------
const bool CompositeSelection::select( const IRecord& record ) const
{
switch( m_conn )
{
case AND:
for ( unsigned int iSel = 0; iSel < m_selVec.size(); iSel++ )
{
if ( ! m_selVec[iSel]->select( record ) ) return false;
}
return true;
case OR:
for ( unsigned int iSel = 0; iSel < m_selVec.size(); iSel++ )
{
if ( m_selVec[iSel]->select( record ) ) return true;
}
return false;
default:
throw Exception( "PANIC! Unknown logical connective",
"CompositeSelection::select()" );
}
}
//-----------------------------------------------------------------------------
IRecordSelection* CompositeSelection::clone() const
{
return new CompositeSelection( *this );
}
//-----------------------------------------------------------------------------
const CompositeSelection::Connective CompositeSelection::connective() const
{
return m_conn;
}
//-----------------------------------------------------------------------------
const unsigned int CompositeSelection::size() const
{
return m_selVec.size();
}
//-----------------------------------------------------------------------------
const IRecordSelection*
CompositeSelection::operator[] ( const unsigned int index ) const
{
if ( index >= m_selVec.size() )
{
std::ostringstream msg;
msg << "Selection #" << index
<< " not found in the composite selection"
<< " (expected range [0," << m_selVec.size()-1 << "])";
throw Exception( msg.str(), "CompositeSelection::operator[]" );
}
return m_selVec[index];
}
//-----------------------------------------------------------------------------
#endif
Supports Markdown
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