libdap  Updated for version 3.18.3
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 
44 using namespace std;
45 
46 namespace libdap {
47 
48 D4Opaque &
49 D4Opaque::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 
62 void
63 D4Opaque::clear_local_data()
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 
73 void
74 D4Opaque::compute_checksum(Crc32 &checksum)
75 {
76  checksum.AddData(&d_buf[0], d_buf.size());
77 }
78 
79 void
80 D4Opaque::serialize(D4StreamMarshaller &m, DMR &, bool)
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
88  clear_local_data();
89 #endif
90 
91 }
92 
93 void
94 D4Opaque::deserialize(D4StreamUnMarshaller &um, DMR &)
95 {
96  um.get_opaque_dap4( d_buf ) ;
97 }
98 
99 unsigned int
100 D4Opaque::buf2val(void **val)
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 
115 unsigned int
116 D4Opaque::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 
129 bool
130 D4Opaque::set_value(const dods_opaque &value)
131 {
132  d_buf = value;
133  set_read_p(true);
134 
135  return true;
136 }
137 
140 D4Opaque::dods_opaque
141 D4Opaque::value() const
142 {
143  return d_buf;
144 }
145 
146 void
147 D4Opaque::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 
161 void
162 D4Opaque::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 
Read data from the stream made by D4StreamMarshaller.
Definition: crc.h:76
STL namespace.
virtual void get_opaque_dap4(char **val, int64_t &len)
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4&#39;s receiv...
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98