Blame D4FilterClause.h

Packit a4aae4
Packit a4aae4
// -*- mode: c++; c-basic-offset:4 -*-
Packit a4aae4
Packit a4aae4
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
Packit a4aae4
// Access Protocol.
Packit a4aae4
Packit a4aae4
// Copyright (c) 2015 OPeNDAP, Inc.
Packit a4aae4
// Author: James Gallagher <jgallagher@opendap.org>
Packit a4aae4
//
Packit a4aae4
// This library is free software; you can redistribute it and/or
Packit a4aae4
// modify it under the terms of the GNU Lesser General Public
Packit a4aae4
// License as published by the Free Software Foundation; either
Packit a4aae4
// version 2.1 of the License, or (at your option) any later version.
Packit a4aae4
//
Packit a4aae4
// This library is distributed in the hope that it will be useful,
Packit a4aae4
// but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4aae4
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4aae4
// Lesser General Public License for more details.
Packit a4aae4
//
Packit a4aae4
// You should have received a copy of the GNU Lesser General Public
Packit a4aae4
// License along with this library; if not, write to the Free Software
Packit a4aae4
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit a4aae4
//
Packit a4aae4
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
Packit a4aae4
Packit a4aae4
#ifndef _d4_filter_clause_h
Packit a4aae4
#define _d4_filter_clause_h
Packit a4aae4
Packit a4aae4
#include <cassert>
Packit a4aae4
#include <vector>
Packit a4aae4
Packit a4aae4
#include "ce_expr.tab.hh"   // Use the same codes for D4 as we use in DAP2
Packit a4aae4
Packit a4aae4
namespace libdap
Packit a4aae4
{
Packit a4aae4
Packit a4aae4
class D4Rvalue;
Packit a4aae4
class D4FilterClause;
Packit a4aae4
Packit a4aae4
/**
Packit a4aae4
 * @brief List of DAP4 Filter Clauses
Packit a4aae4
 *
Packit a4aae4
 */
Packit a4aae4
class D4FilterClauseList
Packit a4aae4
{
Packit a4aae4
private:
Packit a4aae4
    std::vector<D4FilterClause *> d_clauses;
Packit a4aae4
Packit a4aae4
    void m_duplicate(const D4FilterClauseList &src;;
Packit a4aae4
Packit a4aae4
public:
Packit a4aae4
    typedef std::vector<D4FilterClause *>::iterator iter;
Packit a4aae4
    typedef std::vector<D4FilterClause *>::const_iterator citer;
Packit a4aae4
Packit a4aae4
    D4FilterClauseList() { }
Packit a4aae4
    D4FilterClauseList(const D4FilterClauseList &src) { m_duplicate(src); }
Packit a4aae4
Packit a4aae4
    D4FilterClauseList(D4FilterClause *c) { add_clause(c); }
Packit a4aae4
Packit a4aae4
    virtual ~D4FilterClauseList();
Packit a4aae4
Packit a4aae4
    D4FilterClauseList &operator=(const D4FilterClauseList &rhs) {
Packit a4aae4
        if (this == &rhs)
Packit a4aae4
            return *this;
Packit a4aae4
Packit a4aae4
        m_duplicate(rhs);
Packit a4aae4
Packit a4aae4
        return *this;
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    void add_clause(D4FilterClause *c) {
Packit a4aae4
        d_clauses.push_back(c);
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    D4FilterClause *get_clause(unsigned int i) {
Packit a4aae4
        return d_clauses.at(i);
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    citer cbegin() const { return d_clauses.begin(); }
Packit a4aae4
    citer cend() const { return d_clauses.end(); }
Packit a4aae4
Packit a4aae4
    unsigned int size() const { return d_clauses.size(); }
Packit a4aae4
Packit a4aae4
    // get the clause value; this version supports functional clauses
Packit a4aae4
    bool value(DMR &dmr);
Packit a4aae4
Packit a4aae4
    bool value();
Packit a4aae4
};
Packit a4aae4
Packit a4aae4
/**
Packit a4aae4
 * @brief DAP4 filter clauses
Packit a4aae4
 *
Packit a4aae4
 * The DAP4 constraint expression provides a way to filter the values of
Packit a4aae4
 * Sequences (and possibly arrays and coverages, although those are still
Packit a4aae4
 * more ideas than anything at this point). This class holds the operator
Packit a4aae4
 * and operands of one DAP4 constraint's filter clause. The object is built
Packit a4aae4
 * during the parse phase of the constraint evaluation but is not evaluated
Packit a4aae4
 * until the data are sent or interned (read into the DAP4 variable object
Packit a4aae4
 * so that they can be used as input to some process other than directly
Packit a4aae4
 * being sent to a remote client).
Packit a4aae4
 *
Packit a4aae4
 * For filter clauses that are to be applied to a Sequence, each D4RValue
Packit a4aae4
 * will either be a constant or a BaseType* that will reference one of the
Packit a4aae4
 * Sequences fields. The method 'value()' is effectively the evaluator for
Packit a4aae4
 * the clause and nominally reads values from the rvalue objects.
Packit a4aae4
 *
Packit a4aae4
 * @note Potential optimization: Because Sequences might have an optimized
Packit a4aae4
 * representation as a STL vector of some built in types, there could be a
Packit a4aae4
 * value() method that takes a value and compares it to the clause's constant
Packit a4aae4
 * value using the supplied op.
Packit a4aae4
 *
Packit a4aae4
 * @note The 'ND' and 'map' ops are 'still just an idea' parts.
Packit a4aae4
 */
Packit a4aae4
class D4FilterClause
Packit a4aae4
{
Packit a4aae4
public:
Packit a4aae4
	enum ops {
Packit a4aae4
		// Stock relops
Packit a4aae4
		null = 0,
Packit a4aae4
		less = SCAN_LESS,
Packit a4aae4
		greater = SCAN_GREATER,
Packit a4aae4
		less_equal = SCAN_LESS_EQL,
Packit a4aae4
		greater_equal = SCAN_GREATER_EQL,
Packit a4aae4
		equal = SCAN_EQUAL,
Packit a4aae4
		not_equal = SCAN_NOT_EQUAL,
Packit a4aae4
		// Regex match for strings
Packit a4aae4
		match = SCAN_REGEXP,
Packit a4aae4
		// The mapping operator; not sure if this will be implemented
Packit a4aae4
		map,
Packit a4aae4
		// No Data 'operator' for array filtering; may not be impl'd
Packit a4aae4
		ND
Packit a4aae4
	};
Packit a4aae4
Packit a4aae4
private:
Packit a4aae4
    /** The operator */
Packit a4aae4
    ops d_op;
Packit a4aae4
Packit a4aae4
    D4RValue *d_arg1, *d_arg2;
Packit a4aae4
Packit a4aae4
    D4FilterClause() : d_op(null), d_arg1(0), d_arg2(0) { }
Packit a4aae4
Packit a4aae4
    void m_duplicate(const D4FilterClause &rhs;;
Packit a4aae4
Packit a4aae4
    // These methods factor out first the first argument and then the
Packit a4aae4
    // second. I could write one really large cmp() for all of this...
Packit a4aae4
    //template<typename T> bool cmp(ops op, BaseType *arg1, T arg2);
Packit a4aae4
    bool cmp(ops op, BaseType *arg1, BaseType *arg2);
Packit a4aae4
Packit a4aae4
    friend class D4FilterClauseList;
Packit a4aae4
Packit a4aae4
public:
Packit a4aae4
    /**
Packit a4aae4
     * Build a D4FilterClause. The clause will take ownership of
Packit a4aae4
     * the two pointer arguments and delete them.
Packit a4aae4
     *
Packit a4aae4
     * @note When comparing an unsigned variable (UInt16) with a constant,
Packit a4aae4
     * at parse time (i.e., when the D4FilterClause is made) check that the
Packit a4aae4
     * constant is >= 0 and store it in an unsigned value at that time. This
Packit a4aae4
     * will avoid having to make the test repeatedly during filter evaluation.
Packit a4aae4
     *
Packit a4aae4
     * @note When parsing a constant, extract the value from the BaseType and
Packit a4aae4
     * store it in a local field, to avoid the overhead of extracting the
Packit a4aae4
     * value and looking up its type over an over.
Packit a4aae4
     *
Packit a4aae4
     * @param op The operator
Packit a4aae4
     * @param arg1 The left-hand operand
Packit a4aae4
     * @param arg2 The right-hand operand
Packit a4aae4
     */
Packit a4aae4
    D4FilterClause(const ops op, D4RValue *arg1, D4RValue *arg2) :
Packit a4aae4
    	d_op(op), d_arg1(arg1), d_arg2(arg2) {
Packit a4aae4
    	assert(op != null && "null operator");
Packit a4aae4
    	assert(arg1 && "null arg1");
Packit a4aae4
    	assert(arg2 && "null arg2");
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    D4FilterClause(const D4FilterClause &src) {
Packit a4aae4
        m_duplicate(src);
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    D4FilterClause &operator=(const D4FilterClause &rhs) {
Packit a4aae4
        if (this == &rhs)
Packit a4aae4
            return *this;
Packit a4aae4
Packit a4aae4
        m_duplicate(rhs);
Packit a4aae4
Packit a4aae4
        return *this;
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    virtual ~D4FilterClause() {
Packit a4aae4
    	delete d_arg1;
Packit a4aae4
    	delete d_arg2;
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    // get the clause value; this version supports functional clauses
Packit a4aae4
    bool value(DMR &dmr);
Packit a4aae4
Packit a4aae4
    bool value();
Packit a4aae4
};
Packit a4aae4
Packit a4aae4
} // namespace libdap
Packit a4aae4
Packit a4aae4
#endif // _clause_h