libdap++  Updated for version 3.14.0
D4Opaque.cc
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 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 using namespace std;
43 
44 namespace libdap {
45 
46 D4Opaque &
47 D4Opaque::operator=(const D4Opaque &rhs)
48 {
49  if (this == &rhs)
50  return *this;
51 
52  // Call BaseType::operator=
53  dynamic_cast<BaseType &>(*this) = rhs;
54 
55  d_buf = rhs.d_buf;
56 
57  return *this;
58 }
59 
60 void
61 D4Opaque::compute_checksum(Crc32 &checksum)
62 {
63  checksum.AddData(&d_buf[0], d_buf.size());
64 }
65 
66 void
67 D4Opaque::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
68 {
69  if (!read_p())
70  read(); // read() throws Error
71 
72  m.put_opaque_dap4( reinterpret_cast<char*>(&d_buf[0]), d_buf.size() ) ;
73 }
74 
75 void
76 D4Opaque::deserialize(D4StreamUnMarshaller &um, DMR &)
77 {
78  um.get_opaque_dap4( d_buf ) ;
79 }
80 
81 unsigned int
82 D4Opaque::buf2val(void **val)
83 {
84  assert(val);
85 
86  // If *val is null, then the caller has not allocated storage for the
87  // value; we must. If there is storage there, assume it is a vector<uint8_t>
88  // (i.e., dods_opaque) and assign d_buf's value to that storage.
89  if (!*val)
90  *val = new vector<uint8_t>;
91  else
92  *static_cast<vector<uint8_t>*>(*val) = d_buf;
93 
94  return sizeof(vector<uint8_t>*);
95 }
96 
97 unsigned int
98 D4Opaque::val2buf(void *val, bool)
99 {
100  assert(val);
101 
102  d_buf = *static_cast<dods_opaque*>(val);
103 
104  return sizeof(dods_opaque*);
105 }
106 
111 bool
112 D4Opaque::set_value(const dods_opaque &value)
113 {
114  d_buf = value;
115  set_read_p(true);
116 
117  return true;
118 }
119 
123 D4Opaque::value() const
124 {
125  return d_buf;
126 }
127 
128 void
129 D4Opaque::print_val(ostream &out, string space, bool print_decl_p)
130 {
131  if (print_decl_p) print_decl(out, space, false);
132 
133  if (d_buf.size()) {
134  // end() - 1 is only OK if size() is > 0
135  std::ostream_iterator<unsigned int> out_it(out, ",");
136  std::copy(d_buf.begin(), d_buf.end() - 1, out_it);
137  out << (unsigned int) d_buf.back(); // can also use: *(d_buf.end()-1);
138  }
139 
140  if (print_decl_p) out << ";" << endl;
141 }
142 
143 void
144 D4Opaque::dump(ostream &strm) const
145 {
146  strm << DapIndent::LMarg << "D4Opaque::dump - ("
147  << (void *)this << ")" << endl ;
148  DapIndent::Indent() ;
149  BaseType::dump(strm) ;
150  //strm << DapIndent::LMarg << "value: " << d_buf << endl ;
151  ostream_iterator<uint8_t> out_it (strm," ");
152  std::copy ( d_buf.begin(), d_buf.end(), out_it );
153 
154  DapIndent::UnIndent() ;
155 }
156 
157 } // namespace libdap
158 
virtual void put_opaque_dap4(const char *val, int64_t len)
dods_opaque d_buf
Definition: D4Opaque.h:44
Read data from the stream made by D4StreamMarshaller.
Definition: crc.h:76
virtual void get_opaque_dap4(char **val, int64_t &len)
std::vector< uint8_t > dods_opaque
Definition: D4Opaque.h:41
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:84
The basic data type for the DODS DAP types.
Definition: BaseType.h:117