libdap++  Updated for version 3.14.0
D4EnumDefs.h
Go to the documentation of this file.
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 print_value(XMLWriter &xml, const D4EnumDef::tuple &tuple) const;
57 
58 public:
59  typedef vector<tuple>::iterator D4EnumValueIter;
60 
61  D4EnumDef() : d_name(""), d_type(dods_null_c), d_parent(0) {}
62  D4EnumDef(const string &n, const Type &t, D4EnumDefs *e = 0) : d_name(n), d_type(t), d_parent(e) {}
63 
64  string name() const { return d_name; }
65  void set_name(const string &n) { d_name = n; }
66 
67  Type type() const { return d_type; }
68  void set_type(Type t) { d_type = t; }
69 
70  D4EnumDefs *parent() const { return d_parent; }
71  void set_parent(D4EnumDefs *e) { d_parent = e; }
72 
73  bool empty() const { return d_tuples.empty(); }
74 
75  void add_value(const string &label, long long value) {
76  d_tuples.push_back(tuple(label, value));
77  }
78 
79  D4EnumValueIter value_begin() { return d_tuples.begin(); }
80  D4EnumValueIter value_end() { return d_tuples.end(); }
81  string &label(D4EnumValueIter i) { return (*i).label; }
82  long long value(D4EnumValueIter i) { return (*i).value; }
83 
84  bool is_valid_enum_value(long long value);
85  void print_dap4(XMLWriter &xml) const;
86 };
87 
89 class D4EnumDefs {
90  vector<D4EnumDef*> d_enums;
91 
92  D4Group *d_parent; // the group that holds this set of D4EnumDefs; weak pointer, don't delete
93 
94  void m_print_enum(XMLWriter &xml, D4EnumDef *e) const;
95 
96  void m_duplicate(const D4EnumDefs &rhs) {
97  D4EnumDefCIter i = rhs.d_enums.begin();
98  while (i != rhs.d_enums.end()) {
99  d_enums.push_back(new D4EnumDef(**i++)); // deep copy
100  }
101 
102  d_parent = rhs.d_parent;
103  }
104 
105 public:
106  typedef vector<D4EnumDef*>::iterator D4EnumDefIter;
107  typedef vector<D4EnumDef*>::const_iterator D4EnumDefCIter;
108 
109  D4EnumDefs() : d_parent(0) {}
110  D4EnumDefs(const D4EnumDefs &rhs) {
111  m_duplicate(rhs);
112  }
113 
114  virtual ~D4EnumDefs() {
115  D4EnumDefIter i = d_enums.begin();
116  while(i != d_enums.end()) {
117  delete *i++;
118  }
119  }
120 
122  if (this == &rhs) return *this;
123  m_duplicate(rhs);
124  return *this;
125  }
126 
127  bool empty() const { return d_enums.empty(); }
128 
129  D4Group *parent() const { return d_parent; }
130  void set_parent(D4Group *p) { d_parent = p; }
131 
136  void add_enum(D4EnumDef *enum_def) {
137  add_enum_nocopy(new D4EnumDef(*enum_def));
138  }
139  void add_enum_nocopy(D4EnumDef *enum_def) {
140  enum_def->set_parent(this);
141  d_enums.push_back(enum_def);
142  }
143 
145  D4EnumDefIter enum_begin() { return d_enums.begin(); }
146 
148  D4EnumDefIter enum_end() { return d_enums.end(); }
149 
150  D4EnumDef *find_enum_def(const string &name);
151 
160  void insert_enum(D4EnumDef *enum_def, D4EnumDefIter i) {
161  D4EnumDef *enum_def_copy = new D4EnumDef(*enum_def);
162  enum_def_copy->set_parent(this);
163  d_enums.insert(i, enum_def_copy);
164  }
165 
166  void print_dap4(XMLWriter &xml, bool constrained = false) const;
167 };
168 
169 } /* namespace libdap */
170 #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:160
void add_value(const string &label, long long value)
Definition: D4EnumDefs.h:75
string & label(D4EnumValueIter i)
Definition: D4EnumDefs.h:81
Type type() const
Definition: D4EnumDefs.h:67
D4EnumDefs * parent() const
Definition: D4EnumDefs.h:70
void set_name(const string &n)
Definition: D4EnumDefs.h:65
D4EnumDef(const string &n, const Type &t, D4EnumDefs *e=0)
Definition: D4EnumDefs.h:62
void set_parent(D4EnumDefs *e)
Definition: D4EnumDefs.h:71
void set_type(Type t)
Definition: D4EnumDefs.h:68
D4EnumDefs(const D4EnumDefs &rhs)
Definition: D4EnumDefs.h:110
Type
Identifies the data type.
Definition: Type.h:94
bool empty() const
Definition: D4EnumDefs.h:127
void set_parent(D4Group *p)
Definition: D4EnumDefs.h:130
vector< D4EnumDef * >::const_iterator D4EnumDefCIter
Definition: D4EnumDefs.h:107
bool empty() const
Definition: D4EnumDefs.h:73
D4Group * parent() const
Definition: D4EnumDefs.h:129
void add_enum(D4EnumDef *enum_def)
Definition: D4EnumDefs.h:136
D4EnumDefIter enum_begin()
Get an iterator to the start of the enumerations.
Definition: D4EnumDefs.h:145
D4EnumDefIter enum_end()
Get an iterator to the end of the enumerations.
Definition: D4EnumDefs.h:148
D4EnumValueIter value_end()
Definition: D4EnumDefs.h:80
vector< D4EnumDef * >::iterator D4EnumDefIter
Definition: D4EnumDefs.h:106
D4EnumValueIter value_begin()
Definition: D4EnumDefs.h:79
void add_enum_nocopy(D4EnumDef *enum_def)
Definition: D4EnumDefs.h:139
D4EnumDefs & operator=(const D4EnumDefs &rhs)
Definition: D4EnumDefs.h:121
long long value(D4EnumValueIter i)
Definition: D4EnumDefs.h:82
string name() const
Definition: D4EnumDefs.h:64
virtual ~D4EnumDefs()
Definition: D4EnumDefs.h:114
vector< tuple >::iterator D4EnumValueIter
Definition: D4EnumDefs.h:59