libdap++  Updated for version 3.14.0
D4Attributes.h
Go to the documentation of this file.
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 "D4AttributeType.h"
33 #include "XMLWriter.h"
34 
35 using namespace std;
36 
37 namespace libdap
38 {
39 
40 class AttrTable;
41 class D4Attributes;
42 
43 class D4Attribute {
44  string d_name;
45  D4AttributeType d_type; // Attributes are limited to the simple types
46 
47  // If d_type is attr_container_c is true, use d_attributes to read
48  // the contained attributes, otherwise use d_values to read the vector
49  // of values.
50  D4Attributes *d_attributes;
51 
52  // IF d_type is attr_otherxml_c, the first string in d_values holds the
53  // XML, otherwise, the strings hold attributes of type d_type.
54  vector<string> d_values;
55 
56  // perform a deep copy
57  void m_duplicate(const D4Attribute &src);
58 
59 public:
60  typedef vector<string>::iterator D4AttributeIter;
61  typedef vector<string>::const_iterator D4AttributeCIter;
62 
63  D4Attribute() : d_name(""), d_type(attr_null_c), d_attributes(0) {}
64  D4Attribute(const string &name, D4AttributeType type)
65  : d_name(name), d_type(type), d_attributes(0) {}
66 
67  D4Attribute(const D4Attribute &src);
68  ~D4Attribute();
69  D4Attribute &operator=(const D4Attribute &rhs);
70 
71  string name() const { return d_name; }
72  void set_name(const string &name) { d_name = name; }
73 
74  D4AttributeType type() const { return d_type; }
75  void set_type(D4AttributeType type) { d_type = type; }
76 
77  void add_value(const string &value) { d_values.push_back(value); }
78  void add_value_vector(const vector<string> &values) { d_values = values; }
79 
80  D4AttributeIter value_begin() { return d_values.begin(); }
81  D4AttributeIter value_end() { return d_values.end(); }
82 
83  unsigned int num_values() const { return d_values.size(); }
84  string value(unsigned int i) const { return d_values[i]; }
85 
86  D4Attributes *attributes();
87 
88  void print_dap4(XMLWriter &xml) const;
89 };
90 
91 class D4Attributes {
92 public:
93  typedef vector<D4Attribute*>::iterator D4AttributesIter;
94  typedef vector<D4Attribute*>::const_iterator D4AttributesCIter;
95 
96 private:
97  vector<D4Attribute*> d_attrs;
98 
99  void m_duplicate(const D4Attributes &src) {
100  D4AttributesCIter i = src.d_attrs.begin();
101  while (i != src.d_attrs.end()) {
102  d_attrs.push_back(new D4Attribute(**i++)); // deep copy
103  }
104  }
105 
106  D4Attribute *find_depth_first(const string &name, D4AttributesIter i);
107 
108 public:
109 
112  m_duplicate(rhs);
113  }
114 
115  virtual ~D4Attributes() {
116  D4AttributesIter i = d_attrs.begin();
117  while(i != d_attrs.end()) {
118  delete *i++;
119  }
120  }
121 
123  if (this == &rhs) return *this;
124  m_duplicate(rhs);
125  return *this;
126  }
127 
128  void transform_to_dap4(AttrTable &at);
129 
130  bool empty() const { return d_attrs.empty(); }
131 
133  d_attrs.push_back(new D4Attribute(*attr));
134  }
136  d_attrs.push_back(attr);
137  }
138 
140  D4AttributesIter attribute_begin() { return d_attrs.begin(); }
141 
143  D4AttributesIter attribute_end() { return d_attrs.end(); }
144 
145  D4Attribute *find(const string &name);
146  D4Attribute *get(const string &fqn);
147 
148  // D4Attribute *find_container(const string &name);
149  // D4Attribute *get_container(const string &fqn);
150 
151  // Might add erase()
152 
153  void print_dap4(XMLWriter &xml) const;
154 };
155 
158 
159 } // namespace libdap
160 
161 #endif // _d4attributes_h
D4AttributeType StringToD4AttributeType(string s)
D4Attribute(const string &name, D4AttributeType type)
Definition: D4Attributes.h:64
D4Attributes & operator=(const D4Attributes &rhs)
Definition: D4Attributes.h:122
string value(unsigned int i) const
Definition: D4Attributes.h:84
D4AttributesIter attribute_begin()
Get an iterator to the start of the enumerations.
Definition: D4Attributes.h:140
Contains the attributes for a dataset.
Definition: AttrTable.h:142
D4AttributeType
D4AttributeType type() const
Definition: D4Attributes.h:74
void set_type(D4AttributeType type)
Definition: D4Attributes.h:75
vector< D4Attribute * >::iterator D4AttributesIter
Definition: D4Attributes.h:93
vector< D4Attribute * >::const_iterator D4AttributesCIter
Definition: D4Attributes.h:94
D4AttributeIter value_end()
Definition: D4Attributes.h:81
void set_name(const string &name)
Definition: D4Attributes.h:72
vector< string >::const_iterator D4AttributeCIter
Definition: D4Attributes.h:61
D4Attributes(const D4Attributes &rhs)
Definition: D4Attributes.h:111
unsigned int num_values() const
Definition: D4Attributes.h:83
void add_attribute_nocopy(D4Attribute *attr)
Definition: D4Attributes.h:135
void add_value_vector(const vector< string > &values)
Definition: D4Attributes.h:78
D4AttributesIter attribute_end()
Get an iterator to the end of the enumerations.
Definition: D4Attributes.h:143
bool empty() const
Definition: D4Attributes.h:130
vector< string >::iterator D4AttributeIter
Definition: D4Attributes.h:60
string D4AttributeTypeToString(D4AttributeType at)
Definition: D4Attributes.cc:43
void add_value(const string &value)
Definition: D4Attributes.h:77
D4AttributeIter value_begin()
Definition: D4Attributes.h:80
string name() const
Definition: D4Attributes.h:71
void add_attribute(D4Attribute *attr)
Definition: D4Attributes.h:132