OpenDDS  Snapshot(2023/04/28-20:55)
DynamicSample.cpp
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.opendds.org/license.html
4  */
5 
6 #include <DCPS/DdsDcps_pch.h>
7 
8 #ifndef OPENDDS_SAFETY_PROFILE
9 
10 #include "DynamicSample.h"
11 
12 #include "DynamicDataImpl.h"
14 #include "Utils.h"
15 
16 #include <dds/DCPS/DCPS_Utils.h>
17 #include <dds/DCPS/debug.h>
18 
20 namespace OpenDDS {
21 namespace XTypes {
22 
23 using namespace OpenDDS::DCPS;
24 
26 {}
27 
29  : Sample(d.mutability_, d.extent_)
30  , data_(d.data_)
31 {}
32 
34 {
35  if (this != &rhs) {
37  extent_ = rhs.extent_;
38  data_ = rhs.data_;
39  }
40  return *this;
41 }
42 
43 size_t DynamicSample::serialized_size(const Encoding& enc) const
44 {
45  const DynamicDataImpl* const ddi = dynamic_cast<DynamicDataImpl*>(data_.in());
46  if (!ddi) {
48  ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: DynamicSample::serialized_size: "
49  "DynamicData must be DynamicDataImpl, the type supplied by DynamicDataFactory\n"));
50  }
51  return 0;
52  }
53  return key_only()
55  : DCPS::serialized_size(enc, *ddi);
56 }
57 
59 {
60  const DynamicDataImpl* const ddi = dynamic_cast<DynamicDataImpl*>(data_.in());
61  if (!ddi) {
62  if (log_level >= LogLevel::Notice) {
63  ACE_ERROR((LM_NOTICE, "(%P|%t) NOTICE: DynamicSample::serialize: "
64  "DynamicData must be DynamicDataImpl, the type supplied by DynamicDataFactory\n"));
65  }
66  return false;
67  }
68  return key_only()
69  ? ser << DCPS::KeyOnly<const DynamicDataImpl>(*ddi)
70  : ser << *ddi;
71 }
72 
74 {
75  // DynamicDataXcdrReadImpl uses a message block to read the data on demand,
76  // but it can't be the same message block that 'ser' already has. That one
77  // (or more than one if there's chaining) uses the allocators and locking
78  // strategy from the transport.
79  const ACE_CDR::ULong len = static_cast<ACE_CDR::ULong>(ser.length());
80  ACE_Allocator* const alloc = ACE_Allocator::instance();
81  const Message_Block_Ptr mb(new(alloc->malloc(sizeof(ACE_Message_Block)))
82  ACE_Message_Block(len, ACE_Message_Block::MB_DATA, 0, 0, alloc, 0,
84  ACE_Time_Value::max_time, alloc, alloc));
85  unsigned char* const out = reinterpret_cast<unsigned char*>(mb->wr_ptr());
86  if (!ser.read_octet_array(out, len)) {
87  return false;
88  }
89  mb->wr_ptr(len);
90 
91  const DDS::DynamicType_var type = data_->type();
92  data_ = new DynamicDataXcdrReadImpl(mb.get(), ser.encoding(), type, extent_);
93  return true;
94 }
95 
96 bool DynamicSample::compare(const Sample& other) const
97 {
98  const DynamicSample* const other_same_kind = dynamic_cast<const DynamicSample*>(&other);
99  OPENDDS_ASSERT(other_same_kind);
100  bool is_less_than = false;
101  DDS::ReturnCode_t rc = key_less_than(is_less_than, data_, other_same_kind->data_);
102  if (rc != DDS::RETCODE_OK) {
103  if (log_level >= LogLevel::Warning) {
104  ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: DynamicSample::compare: "
105  "key_less_than returned %C\n", retcode_to_string(rc)));
106  }
107  }
108  return is_less_than;
109 }
110 
111 }
112 }
114 #endif
DDS::ReturnCode_t key_less_than(bool &result, DDS::DynamicData_ptr a, DDS::DynamicData_ptr b)
#define ACE_ERROR(X)
bool serialize(DCPS::Serializer &ser) const
static const ACE_Time_Value max_time
bool deserialize(DCPS::Serializer &ser)
#define OPENDDS_ASSERT(C)
Definition: Definitions.h:72
bool key_only() const
Definition: Sample.h:75
bool read_octet_array(ACE_CDR::Octet *x, ACE_CDR::ULong length)
Definition: Serializer.inl:558
void serialized_size(const Encoding &encoding, size_t &size, const SequenceNumber &)
Class to serialize and deserialize data for DDS.
Definition: Serializer.h:369
size_t length() const
Number of bytes left to read in message block chain.
Definition: Serializer.inl:437
static ACE_Allocator * instance(void)
size_t serialized_size(const DCPS::Encoding &enc) const
LM_NOTICE
bool compare(const DCPS::Sample &other) const
char * wr_ptr(void) const
LM_WARNING
ACE_UINT32 ULong
Mutability mutability_
Definition: Sample.h:104
OpenDDS_Dcps_Export LogLevel log_level
const char * retcode_to_string(DDS::ReturnCode_t value)
Definition: DCPS_Utils.cpp:29
static const ACE_Time_Value zero
const Encoding & encoding() const
Definition: Serializer.inl:199
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
const ReturnCode_t RETCODE_OK
DDS::DynamicData_var data_
Definition: DynamicSample.h:99
#define ACE_DEFAULT_MESSAGE_BLOCK_PRIORITY
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
virtual void * malloc(size_type nbytes)=0
DynamicSample & operator=(const DynamicSample &)