Blame D4Attributes.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) 2013 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit a4aae4
//
Packit a4aae4
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
Packit a4aae4
Packit a4aae4
#ifndef _d4attributes_h
Packit a4aae4
#define _d4attributes_h 1
Packit a4aae4
Packit a4aae4
#include <string>
Packit a4aae4
#include <vector>
Packit a4aae4
Packit a4aae4
#include "DapObj.h"
Packit a4aae4
#include "D4AttributeType.h"
Packit a4aae4
#include "XMLWriter.h"
Packit a4aae4
Packit a4aae4
using namespace std;
Packit a4aae4
Packit a4aae4
namespace libdap
Packit a4aae4
{
Packit a4aae4
Packit a4aae4
class AttrTable;
Packit a4aae4
class D4Attributes;
Packit a4aae4
Packit a4aae4
class D4Attribute : public DapObj {
Packit a4aae4
    string d_name;
Packit a4aae4
    D4AttributeType d_type;    // Attributes are limited to the simple types
Packit a4aae4
Packit a4aae4
    // If d_type is attr_container_c is true, use d_attributes to read
Packit a4aae4
    // the contained attributes, otherwise use d_values to read the vector
Packit a4aae4
    // of values.
Packit a4aae4
    D4Attributes *d_attributes;
Packit a4aae4
Packit a4aae4
    // IF d_type is attr_otherxml_c, the first string in d_values holds the
Packit a4aae4
    // XML, otherwise, the strings hold attributes of type d_type.
Packit a4aae4
    vector<string> d_values;
Packit a4aae4
Packit a4aae4
    // perform a deep copy
Packit a4aae4
    void m_duplicate(const D4Attribute &src;;
Packit a4aae4
Packit a4aae4
public:
Packit a4aae4
    typedef vector<string>::iterator D4AttributeIter;
Packit a4aae4
    typedef vector<string>::const_iterator D4AttributeCIter;
Packit a4aae4
Packit a4aae4
    D4Attribute() : d_name(""), d_type(attr_null_c), d_attributes(0) {}
Packit a4aae4
    D4Attribute(const string &name, D4AttributeType type)
Packit a4aae4
        : d_name(name), d_type(type), d_attributes(0) {}
Packit a4aae4
Packit a4aae4
    D4Attribute(const D4Attribute &src;;
Packit a4aae4
    ~D4Attribute();
Packit a4aae4
    D4Attribute &operator=(const D4Attribute &rhs;;
Packit a4aae4
Packit a4aae4
    string name() const { return d_name; }
Packit a4aae4
    void set_name(const string &name) { d_name = name; }
Packit a4aae4
Packit a4aae4
    D4AttributeType type() const { return d_type; }
Packit a4aae4
    void set_type(D4AttributeType type) { d_type = type; }
Packit a4aae4
Packit a4aae4
    void add_value(const string &value) { d_values.push_back(value); }
Packit a4aae4
    void add_value_vector(const vector<string> &values) { d_values = values; }
Packit a4aae4
Packit a4aae4
    D4AttributeIter value_begin() { return d_values.begin(); }
Packit a4aae4
    D4AttributeIter value_end() { return d_values.end(); }
Packit a4aae4
Packit a4aae4
    unsigned int num_values() const { return d_values.size(); }
Packit a4aae4
    string value(unsigned int i) const { return d_values[i]; }
Packit a4aae4
Packit a4aae4
    D4Attributes *attributes();
Packit a4aae4
Packit a4aae4
    void print_dap4(XMLWriter &xml) const;
Packit a4aae4
Packit a4aae4
    virtual void dump(ostream &strm) const;
Packit a4aae4
};
Packit a4aae4
Packit a4aae4
class D4Attributes : public DapObj {
Packit a4aae4
public:
Packit a4aae4
    typedef vector<D4Attribute*>::iterator D4AttributesIter;
Packit a4aae4
    typedef vector<D4Attribute*>::const_iterator D4AttributesCIter;
Packit a4aae4
Packit a4aae4
private:
Packit a4aae4
    vector<D4Attribute*> d_attrs;
Packit a4aae4
Packit a4aae4
    void m_duplicate(const D4Attributes &src) {
Packit a4aae4
        D4AttributesCIter i = src.d_attrs.begin();
Packit a4aae4
        while (i != src.d_attrs.end()) {
Packit a4aae4
            d_attrs.push_back(new D4Attribute(**i++));    // deep copy
Packit a4aae4
        }
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    D4Attribute *find_depth_first(const string &name, D4AttributesIter i);
Packit a4aae4
Packit a4aae4
public:
Packit a4aae4
Packit a4aae4
    D4Attributes() {}
Packit a4aae4
    D4Attributes(const D4Attributes &rhs) {
Packit a4aae4
        m_duplicate(rhs);
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    virtual ~D4Attributes() {
Packit a4aae4
        D4AttributesIter i = d_attrs.begin();
Packit a4aae4
        while(i != d_attrs.end()) {
Packit a4aae4
            delete *i++;
Packit a4aae4
        }
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    D4Attributes &operator=(const D4Attributes &rhs) {
Packit a4aae4
        if (this == &rhs) return *this;
Packit a4aae4
        m_duplicate(rhs);
Packit a4aae4
        return *this;
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    void transform_to_dap4(AttrTable &at);
Packit a4aae4
Packit a4aae4
    AttrTable *get_AttrTable(const std::string name);
Packit a4aae4
    static void load_AttrTable(AttrTable *d2_attr_table, D4Attributes *d4_attrs);
Packit a4aae4
Packit a4aae4
    bool empty() const { return d_attrs.empty(); }
Packit a4aae4
Packit a4aae4
    void add_attribute(D4Attribute *attr) {
Packit a4aae4
        d_attrs.push_back(new D4Attribute(*attr));
Packit a4aae4
    }
Packit a4aae4
    void add_attribute_nocopy(D4Attribute *attr) {
Packit a4aae4
        d_attrs.push_back(attr);
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    /// Get an iterator to the start of the enumerations
Packit a4aae4
    D4AttributesIter attribute_begin() { return d_attrs.begin(); }
Packit a4aae4
Packit a4aae4
    /// Get an iterator to the end of the enumerations
Packit a4aae4
    D4AttributesIter attribute_end() { return d_attrs.end(); }
Packit a4aae4
Packit a4aae4
    D4Attribute *find(const string &name);
Packit a4aae4
    D4Attribute *get(const string &fqn);
Packit a4aae4
Packit a4aae4
    // D4Attribute *find_container(const string &name);
Packit a4aae4
    // D4Attribute *get_container(const string &fqn);
Packit a4aae4
Packit a4aae4
    // Might add erase()
Packit a4aae4
Packit a4aae4
    void print_dap4(XMLWriter &xml) const;
Packit a4aae4
Packit a4aae4
    virtual void dump(ostream &strm) const;
Packit a4aae4
};
Packit a4aae4
Packit a4aae4
string D4AttributeTypeToString(D4AttributeType at);
Packit a4aae4
D4AttributeType StringToD4AttributeType(string s);
Packit a4aae4
Packit a4aae4
} // namespace libdap
Packit a4aae4
Packit a4aae4
#endif // _d4attributes_h