libdap  Updated for version 3.18.3
D4EnumDefs.h
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2013 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #ifndef D4ENUMDEF_H_
26 #define D4ENUMDEF_H_
27 
28 #include <string>
29 #include <vector>
30 #include <algorithm>
31 #include <functional>
32 
33 #include "BaseType.h"
34 
35 using namespace std;
36 
37 namespace libdap {
38 
39 class D4EnumDefs;
40 class D4Group;
41 
42 class D4EnumDef {
43  string d_name;
44  Type d_type;
45  D4EnumDefs *d_parent;
46 
47  struct tuple {
48  string label;
49  long long value;
50 
51  tuple(const string &l, long long v) : label(l), value(v) {}
52  };
53 
54  vector<tuple> d_tuples;
55 
56  void m_duplicate(const D4EnumDef &rhs)
57  {
58  d_name = rhs.d_name;
59  d_type = rhs.d_type;
60  d_parent = rhs.d_parent;
61  d_tuples = rhs.d_tuples;
62  }
63 
64  void print_value(XMLWriter &xml, const D4EnumDef::tuple &tuple) const;
65 
66 public:
67  typedef vector<tuple>::iterator D4EnumValueIter;
68 
69  D4EnumDef() : d_name(""), d_type(dods_null_c), d_parent(0) {}
70  D4EnumDef(const string &n, const Type &t, D4EnumDefs *e = 0) : d_name(n), d_type(t), d_parent(e) {}
71  D4EnumDef(const D4EnumDef &rhs) {
72  m_duplicate(rhs);
73  }
74 
75  string name() const { return d_name; }
76  void set_name(const string &n) { d_name = n; }
77 
78  Type type() const { return d_type; }
79  void set_type(Type t) { d_type = t; }
80 
81  D4EnumDefs *parent() const { return d_parent; }
82  void set_parent(D4EnumDefs *e) { d_parent = e; }
83 
84  bool empty() const { return d_tuples.empty(); }
85 
86  void add_value(const string &label, long long value) {
87  d_tuples.push_back(tuple(label, value));
88  }
89 
90  D4EnumValueIter value_begin() { return d_tuples.begin(); }
91  D4EnumValueIter value_end() { return d_tuples.end(); }
92  string &label(D4EnumValueIter i) { return (*i).label; }
93  long long value(D4EnumValueIter i) { return (*i).value; }
94 
95  bool is_valid_enum_value(long long value);
96  void print_dap4(XMLWriter &xml) const;
97 };
98 
100 class D4EnumDefs {
101  vector<D4EnumDef*> d_enums;
102 
103  D4Group *d_parent; // the group that holds this set of D4EnumDefs; weak pointer, don't delete
104 
105  void m_print_enum(XMLWriter &xml, D4EnumDef *e) const;
106 
107  void m_duplicate(const D4EnumDefs &rhs) {
108  D4EnumDefCIter i = rhs.d_enums.begin();
109  while (i != rhs.d_enums.end()) {
110  d_enums.push_back(new D4EnumDef(**i++)); // deep copy
111  }
112 
113  d_parent = rhs.d_parent;
114  }
115 
116 public:
117  typedef vector<D4EnumDef*>::iterator D4EnumDefIter;
118  typedef vector<D4EnumDef*>::const_iterator D4EnumDefCIter;
119 
120  D4EnumDefs() : d_parent(0) {}
121  D4EnumDefs(const D4EnumDefs &rhs) {
122  m_duplicate(rhs);
123  }
124 
125  virtual ~D4EnumDefs() {
126  D4EnumDefIter i = d_enums.begin();
127  while(i != d_enums.end()) {
128  delete *i++;
129  }
130  }
131 
132  D4EnumDefs &operator=(const D4EnumDefs &rhs) {
133  if (this == &rhs) return *this;
134  m_duplicate(rhs);
135  return *this;
136  }
137 
138  bool empty() const { return d_enums.empty(); }
139 
140  D4Group *parent() const { return d_parent; }
141  void set_parent(D4Group *p) { d_parent = p; }
142 
147  void add_enum(D4EnumDef *enum_def) {
148  add_enum_nocopy(new D4EnumDef(*enum_def));
149  }
150  void add_enum_nocopy(D4EnumDef *enum_def) {
151  enum_def->set_parent(this);
152  d_enums.push_back(enum_def);
153  }
154 
156  D4EnumDefIter enum_begin() { return d_enums.begin(); }
157 
159  D4EnumDefIter enum_end() { return d_enums.end(); }
160 
161  D4EnumDef *find_enum_def(const string &name);
162 
171  void insert_enum(D4EnumDef *enum_def, D4EnumDefIter i) {
172  D4EnumDef *enum_def_copy = new D4EnumDef(*enum_def);
173  enum_def_copy->set_parent(this);
174  d_enums.insert(i, enum_def_copy);
175  }
176 
177  void print_dap4(XMLWriter &xml, bool constrained = false) const;
178 };
179 
180 } /* namespace libdap */
181 #endif /* D4ENUMDEF_H_ */
void insert_enum(D4EnumDef *enum_def, D4EnumDefIter i)
Insert a D4EnumDef. Insert a D4EnumDef before the position specified by the iterator.
Definition: D4EnumDefs.h:171
STL namespace.
Type
Identifies the data type.
Definition: Type.h:94
void add_enum(D4EnumDef *enum_def)
Definition: D4EnumDefs.h:147
D4EnumDefIter enum_begin()
Get an iterator to the start of the enumerations.
Definition: D4EnumDefs.h:156
D4EnumDefIter enum_end()
Get an iterator to the end of the enumerations.
Definition: D4EnumDefs.h:159