libdap Updated for version 3.18.1
D4Attributes.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2013 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#ifndef _d4attributes_h
27#define _d4attributes_h 1
28
29#include <string>
30#include <vector>
31
32#include "DapObj.h"
33#include "D4AttributeType.h"
34#include "XMLWriter.h"
35
36using namespace std;
37
38namespace libdap
39{
40
41class AttrTable;
42class D4Attributes;
43
44class D4Attribute : public DapObj {
45 string d_name;
46 D4AttributeType d_type; // Attributes are limited to the simple types
47
48 // If d_type is attr_container_c is true, use d_attributes to read
49 // the contained attributes, otherwise use d_values to read the vector
50 // of values.
51 D4Attributes *d_attributes;
52
53 // IF d_type is attr_otherxml_c, the first string in d_values holds the
54 // XML, otherwise, the strings hold attributes of type d_type.
55 vector<string> d_values;
56
57 // perform a deep copy
58 void m_duplicate(const D4Attribute &src);
59
60public:
61 typedef vector<string>::iterator D4AttributeIter;
62 typedef vector<string>::const_iterator D4AttributeCIter;
63
64 D4Attribute() : d_name(""), d_type(attr_null_c), d_attributes(0) {}
65 D4Attribute(const string &name, D4AttributeType type)
66 : d_name(name), d_type(type), d_attributes(0) {}
67
68 D4Attribute(const D4Attribute &src);
70 D4Attribute &operator=(const D4Attribute &rhs);
71
72 string name() const { return d_name; }
73 void set_name(const string &name) { d_name = name; }
74
75 D4AttributeType type() const { return d_type; }
76 void set_type(D4AttributeType type) { d_type = type; }
77
78 void add_value(const string &value) { d_values.push_back(value); }
79 void add_value_vector(const vector<string> &values) { d_values = values; }
80
81 D4AttributeIter value_begin() { return d_values.begin(); }
82 D4AttributeIter value_end() { return d_values.end(); }
83
84 unsigned int num_values() const { return d_values.size(); }
85 string value(unsigned int i) const { return d_values[i]; }
86
87 D4Attributes *attributes();
88
89 void print_dap4(XMLWriter &xml) const;
90
91 virtual void dump(ostream &strm) const;
92};
93
94class D4Attributes : public DapObj {
95public:
96 typedef vector<D4Attribute*>::iterator D4AttributesIter;
97 typedef vector<D4Attribute*>::const_iterator D4AttributesCIter;
98
99private:
100 vector<D4Attribute*> d_attrs;
101
102 void m_duplicate(const D4Attributes &src) {
103 D4AttributesCIter i = src.d_attrs.begin();
104 while (i != src.d_attrs.end()) {
105 d_attrs.push_back(new D4Attribute(**i++)); // deep copy
106 }
107 }
108
109 D4Attribute *find_depth_first(const string &name, D4AttributesIter i);
110
111public:
112
113 D4Attributes() {}
114 D4Attributes(const D4Attributes &rhs) {
115 m_duplicate(rhs);
116 }
117
118 virtual ~D4Attributes() {
119 D4AttributesIter i = d_attrs.begin();
120 while(i != d_attrs.end()) {
121 delete *i++;
122 }
123 }
124
125 D4Attributes &operator=(const D4Attributes &rhs) {
126 if (this == &rhs) return *this;
127 m_duplicate(rhs);
128 return *this;
129 }
130
132
133 bool empty() const { return d_attrs.empty(); }
134
135 void add_attribute(D4Attribute *attr) {
136 d_attrs.push_back(new D4Attribute(*attr));
137 }
138 void add_attribute_nocopy(D4Attribute *attr) {
139 d_attrs.push_back(attr);
140 }
141
143 D4AttributesIter attribute_begin() { return d_attrs.begin(); }
144
146 D4AttributesIter attribute_end() { return d_attrs.end(); }
147
148 D4Attribute *find(const string &name);
149 D4Attribute *get(const string &fqn);
150
151 // D4Attribute *find_container(const string &name);
152 // D4Attribute *get_container(const string &fqn);
153
154 // Might add erase()
155
156 void print_dap4(XMLWriter &xml) const;
157
158 virtual void dump(ostream &strm) const;
159};
160
161string D4AttributeTypeToString(D4AttributeType at);
162D4AttributeType StringToD4AttributeType(string s);
163
164} // namespace libdap
165
166#endif // _d4attributes_h
Contains the attributes for a dataset.
Definition: AttrTable.h:143
virtual void dump(ostream &strm) const
dumps information about this object
void transform_to_dap4(AttrTable &at)
copy attributes from DAP2 to DAP4
D4Attribute * get(const string &fqn)
D4AttributesIter attribute_begin()
Get an iterator to the start of the enumerations.
Definition: D4Attributes.h:143
D4AttributesIter attribute_end()
Get an iterator to the end of the enumerations.
Definition: D4Attributes.h:146
virtual void dump(ostream &strm) const
dumps information about this object
libdap base object for common functionality of libdap objects
Definition: DapObj.h:56
string D4AttributeTypeToString(D4AttributeType at)
Definition: D4Attributes.cc:43