libdap Updated for version 3.18.1
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
26#include "config.h"
27
28#include <sstream>
29#include <iterator>
30
31#include "D4Opaque.h"
32
33#include "DMR.h"
34#include "D4StreamMarshaller.h"
35#include "D4StreamUnMarshaller.h"
36
37#include "util.h"
38#include "crc.h"
39
40#include "debug.h"
41
42#undef CLEAR_LOCAL_DATA
43
44using namespace std;
45
46namespace libdap {
47
48D4Opaque &
49D4Opaque::operator=(const D4Opaque &rhs)
50{
51 if (this == &rhs)
52 return *this;
53
54 // Call BaseType::operator=
55 dynamic_cast<BaseType &>(*this) = rhs;
56
57 d_buf = rhs.d_buf;
58
59 return *this;
60}
61
62void
64{
65 if (!d_buf.empty()) {
66 d_buf.erase(d_buf.begin(), d_buf.end());
67 d_buf.resize(0);
68 }
69
70 set_read_p(false);
71}
72
73void
75{
76 checksum.AddData(&d_buf[0], d_buf.size());
77}
78
79void
81{
82 if (!read_p())
83 read(); // read() throws Error
84
85 m.put_opaque_dap4( reinterpret_cast<char*>(&d_buf[0]), d_buf.size() ) ;
86
87#ifdef CLEAR_LOCAL_DATA
89#endif
90
91}
92
93void
95{
96 um.get_opaque_dap4( d_buf ) ;
97}
98
99unsigned int
101{
102 assert(val);
103
104 // If *val is null, then the caller has not allocated storage for the
105 // value; we must. If there is storage there, assume it is a vector<uint8_t>
106 // (i.e., dods_opaque) and assign d_buf's value to that storage.
107 if (!*val)
108 *val = new vector<uint8_t>;
109 else
110 *static_cast<vector<uint8_t>*>(*val) = d_buf;
111
112 return sizeof(vector<uint8_t>*);
113}
114
115unsigned int
116D4Opaque::val2buf(void *val, bool)
117{
118 assert(val);
119
120 d_buf = *static_cast<dods_opaque*>(val);
121
122 return sizeof(dods_opaque*);
123}
124
129bool
130D4Opaque::set_value(const dods_opaque &value)
131{
132 d_buf = value;
133 set_read_p(true);
134
135 return true;
136}
137
140D4Opaque::dods_opaque
142{
143 return d_buf;
144}
145
146void
147D4Opaque::print_val(ostream &out, string space, bool print_decl_p)
148{
149 if (print_decl_p) print_decl(out, space, false);
150
151 if (d_buf.size()) {
152 // end() - 1 is only OK if size() is > 0
153 std::ostream_iterator<unsigned int> out_it(out, ",");
154 std::copy(d_buf.begin(), d_buf.end() - 1, out_it);
155 out << (unsigned int) d_buf.back(); // can also use: *(d_buf.end()-1);
156 }
157
158 if (print_decl_p) out << ";" << endl;
159}
160
161void
162D4Opaque::dump(ostream &strm) const
163{
164 strm << DapIndent::LMarg << "D4Opaque::dump - ("
165 << (void *)this << ")" << endl ;
166 DapIndent::Indent() ;
167 BaseType::dump(strm) ;
168 //strm << DapIndent::LMarg << "value: " << d_buf << endl ;
169 ostream_iterator<uint8_t> out_it (strm," ");
170 std::copy ( d_buf.begin(), d_buf.end(), out_it );
171
172 DapIndent::UnIndent() ;
173}
174
175} // namespace libdap
176
Definition: crc.h:77
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:820
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:924
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:425
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:461
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:236
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:125
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:74
virtual unsigned int buf2val(void **val)
Definition: D4Opaque.cc:100
virtual void clear_local_data()
Definition: D4Opaque.cc:63
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:162
virtual dods_opaque value() const
Definition: D4Opaque.cc:141
virtual unsigned int val2buf(void *val, bool reuse=false)
Definition: D4Opaque.cc:116
virtual bool set_value(const dods_opaque &value)
Definition: D4Opaque.cc:130
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)