IODA Bundle
ThreadSharedDataHandle.cc
Go to the documentation of this file.
1 
2 /*
3  * (C) Copyright 1996-2018 ECMWF.
4  *
5  * This software is licensed under the terms of the Apache Licence Version 2.0
6  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7  * In applying this licence, ECMWF does not waive the privileges and immunities
8  * granted to it by virtue of its status as an intergovernmental organisation nor
9  * does it submit to any jurisdiction.
10  */
11 
13 
14 #include "eckit/exception/Exceptions.h"
15 
16 namespace odc {
17 namespace core {
18 
19 //----------------------------------------------------------------------------------------------------------------------
20 
21 
22 ThreadSharedDataHandle::Internal::Internal(eckit::DataHandle* dh, bool owned) :
23  dh_(dh),
24  owned_(owned) {
25 
26  if (owned_) {
27  dh_->openForRead();
28  }
29 }
30 
32  if (owned_) {
33  dh_->close();
34  delete dh_;
35  }
36 }
37 
38 
40  internal_(std::make_shared<ThreadSharedDataHandle::Internal>(&dh, false)),
41  position_(internal_->dh_->position()) {}
42 
43 
45  internal_(std::make_shared<ThreadSharedDataHandle::Internal>(dh, true)),
46  position_(internal_->dh_->position()) {}
47 
48 
50 
52  internal_(other.internal_),
53  position_(other.position_) {}
54 
56  internal_ = rhs.internal_;
57  position_ = rhs.position_;
58  return *this;
59 }
60 
62  internal_(std::move(other.internal_)),
63  position_(other.position_) {}
64 
66  std::swap(internal_, rhs.internal_);
67  position_ = rhs.position_;
68  return *this;
69 }
70 
72  return !(*this == other);
73 }
74 
76  return (internal_ == other.internal_ && position_ == other.position_);
77 }
78 
79 
80 void ThreadSharedDataHandle::print(std::ostream& s) const {
81  s << "ThreadSharedDataHandle(" << *internal_->dh_ << ")";
82 }
83 
85  ASSERT(internal_);
86  std::lock_guard<std::mutex> lock(internal_->m_);
87  return internal_->dh_->openForRead();
88 }
89 
90 void ThreadSharedDataHandle::openForWrite(const eckit::Length&) { NOTIMP; }
91 
92 void ThreadSharedDataHandle::openForAppend(const eckit::Length&) { NOTIMP; }
93 
94 long ThreadSharedDataHandle::read(void* buffer, long length) {
95 
96  ASSERT(internal_);
97  std::lock_guard<std::mutex> lock(internal_->m_);
98 
99  if (position_ != internal_->dh_->position()) {
100  internal_->dh_->seek(position_);
101  }
102 
103  long delta = internal_->dh_->read(buffer, length);
104  position_ += delta;
105  return delta;
106 }
107 
108 long ThreadSharedDataHandle::write(const void*, long) { NOTIMP; }
109 
111  ASSERT(internal_);
112  std::lock_guard<std::mutex> lock(internal_->m_);
113  internal_->dh_->close();
114 }
115 
117  ASSERT(internal_);
118  return internal_->dh_->estimate();
119 }
120 
122  return position_;
123 }
124 
125 eckit::Offset ThreadSharedDataHandle::seek(const eckit::Offset& position) {
127  return position_;
128 }
129 
130 std::string ThreadSharedDataHandle::title() const {
131  ASSERT(internal_);
132  return internal_->dh_->name();
133 }
134 
135 //----------------------------------------------------------------------------------------------------------------------
136 
137 }
138 }
std::string title() const override
ThreadSharedDataHandle & operator=(const ThreadSharedDataHandle &)
void openForAppend(const eckit::Length &) override
std::shared_ptr< Internal > internal_
eckit::Offset seek(const eckit::Offset &) override
bool operator==(const ThreadSharedDataHandle &other)
void openForWrite(const eckit::Length &) override
long write(const void *, long) override
void print(std::ostream &s) const override
ThreadSharedDataHandle(eckit::DataHandle &dh)
bool operator!=(const ThreadSharedDataHandle &other)
Definition: ColumnInfo.h:23
Definition: encode.cc:30
Internal(eckit::DataHandle *dh, bool owned)