libdap Updated for version 3.20.5
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4Opaque.cc
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 D4Opaqueeet, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25//#define DODS_DEBUG
26
27#include "config.h"
28
29#include <sstream>
30#include <iterator>
31
32#include "D4Opaque.h"
33
34#include "DMR.h"
35#include "D4StreamMarshaller.h"
36#include "D4StreamUnMarshaller.h"
37
38#include "util.h"
39#include "crc.h"
40
41#include "debug.h"
42#include "DapIndent.h"
43
44#undef CLEAR_LOCAL_DATA
45
46using namespace std;
47
48namespace libdap {
49
50D4Opaque &
51D4Opaque::operator=(const D4Opaque &rhs)
52{
53 if (this == &rhs)
54 return *this;
55
56 // Call BaseType::operator=
57 dynamic_cast<BaseType &>(*this) = rhs;
58
59 d_buf = rhs.d_buf;
60
61 return *this;
62}
63
64void
66{
67 if (!d_buf.empty()) {
68 d_buf.erase(d_buf.begin(), d_buf.end());
69 d_buf.resize(0);
70 }
71
72 set_read_p(false);
73}
74
75void
77{
78 checksum.AddData(&d_buf[0], d_buf.size());
79}
80
81void
83{
84 if (!read_p())
85 read(); // read() throws Error
86
87 m.put_opaque_dap4( reinterpret_cast<char*>(&d_buf[0]), d_buf.size() ) ;
88
89#ifdef CLEAR_LOCAL_DATA
91#endif
92
93}
94
95void
97{
98 um.get_opaque_dap4( d_buf ) ;
99}
100
101unsigned int
103{
104 assert(val);
105
106 // If *val is null, then the caller has not allocated storage for the
107 // value; we must. If there is storage there, assume it is a vector<uint8_t>
108 // (i.e., dods_opaque) and assign d_buf's value to that storage.
109 if (!*val)
110 *val = new vector<uint8_t>;
111 else
112 *static_cast<vector<uint8_t>*>(*val) = d_buf;
113
114 return sizeof(vector<uint8_t>*);
115}
116
117unsigned int
118D4Opaque::val2buf(void *val, bool)
119{
120 assert(val);
121
122 d_buf = *static_cast<dods_opaque*>(val);
123
124 return sizeof(dods_opaque*);
125}
126
131bool
132D4Opaque::set_value(const dods_opaque &value)
133{
134 d_buf = value;
135 set_read_p(true);
136
137 return true;
138}
139
142D4Opaque::dods_opaque
144{
145 return d_buf;
146}
147
148std::vector<BaseType *> *
150 DBG(cerr << __func__ << "() - Transform not implemented DAP4 Opaque type." << endl;);
151 return NULL;
152}
153
154
155void
156D4Opaque::print_val(ostream &out, string space, bool print_decl_p)
157{
158 if (print_decl_p) print_decl(out, space, false);
159
160 if (d_buf.size()) {
161 // end() - 1 is only OK if size() is > 0
162 std::ostream_iterator<unsigned int> out_it(out, ",");
163 std::copy(d_buf.begin(), d_buf.end() - 1, out_it);
164 out << (unsigned int) d_buf.back(); // can also use: *(d_buf.end()-1);
165 }
166
167 if (print_decl_p) out << ";" << endl;
168}
169
170void
171D4Opaque::dump(ostream &strm) const
172{
173 strm << DapIndent::LMarg << "D4Opaque::dump - ("
174 << (void *)this << ")" << endl ;
175 DapIndent::Indent() ;
176 BaseType::dump(strm) ;
177 //strm << DapIndent::LMarg << "value: " << d_buf << endl ;
178 ostream_iterator<uint8_t> out_it (strm," ");
179 std::copy ( d_buf.begin(), d_buf.end(), out_it );
180
181 DapIndent::UnIndent() ;
182}
183
184} // namespace libdap
185
Definition: crc.h:77
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98
Contains the attributes for a dataset.
Definition: AttrTable.h:143
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:899
virtual void print_decl(FILE *out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
Definition: BaseType.cc:1003
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:480
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:516
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:291
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: D4Opaque.cc:149
virtual bool deserialize(UnMarshaller &, DDS *, bool=false)
Receive data from the net.
Definition: D4Opaque.h:72
virtual void compute_checksum(Crc32 &checksum)
include the data for this variable in the checksum DAP4 includes a checksum with every data response....
Definition: D4Opaque.cc:76
virtual unsigned int buf2val(void **val)
Reads the class data.
Definition: D4Opaque.cc:102
virtual void clear_local_data()
Definition: D4Opaque.cc:65
virtual bool serialize(ConstraintEvaluator &, DDS &, Marshaller &, bool=true)
Move data to the net, then remove them from the object.
Definition: D4Opaque.h:69
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: D4Opaque.cc:171
virtual dods_opaque value() const
Definition: D4Opaque.cc:143
virtual unsigned int val2buf(void *val, bool reuse=false)
Loads class data.
Definition: D4Opaque.cc:118
virtual bool set_value(const dods_opaque &value)
Definition: D4Opaque.cc:132
virtual void print_val(FILE *, std::string="", bool=true)
Prints the value of the variable.
Definition: D4Opaque.h:90
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
Read data from the stream made by D4StreamMarshaller.
virtual void get_opaque_dap4(char **val, int64_t &len)
top level DAP object to house generic methods
Definition: AlarmHandler.h:36