55
66#include < awslabs/enhanced/s3buf.h>
77#include < string>
8+ #include < string_view>
89#include < istream>
910
1011namespace AwsLabs ::Enhanced {
@@ -13,6 +14,16 @@ class is3stream : public std::istream {
1314 std::string _region;
1415 std::string _bucket_name;
1516 std::string _object_name;
17+ protected:
18+ /* *
19+ * Opens the current region/bucket/object and set the iostate
20+ */
21+ void open (std::ios_base::openmode mode = std::ios_base::in) {
22+ if (_s3b->open (_region, _bucket_name, _object_name, mode))
23+ std::ios::setstate (goodbit);
24+ else
25+ std::ios::setstate (failbit);
26+ }
1627public:
1728 /* *
1829 * Default constructor for a is3stream not associated to an S3 object
@@ -41,7 +52,8 @@ class is3stream : public std::istream {
4152 * @return
4253 */
4354 is3stream &operator =(is3stream &&is3s) {
44- _s3b->close ();
55+ if (is_open ())
56+ _s3b->close ();
4557 _region = is3s._region ;
4658 _bucket_name = is3s._bucket_name ;
4759 _object_name = is3s._object_name ;
@@ -63,7 +75,7 @@ class is3stream : public std::istream {
6375 _region = region;
6476 _bucket_name = bucket_name;
6577 _object_name = object_name;
66- _s3b-> open (_region, _bucket_name, _object_name, std::ios_base::in );
78+ open ();
6779 }
6880
6981 /* *
@@ -87,7 +99,7 @@ class is3stream : public std::istream {
8799 */
88100 void open (const std::string &object_name) {
89101 _object_name = object_name;
90- _s3b-> open (_region, _bucket_name, _object_name, std::ios_base::in );
102+ open ();
91103 }
92104 /* *
93105 * Opens object_name in previously set region and bucket to read content from it.
@@ -99,7 +111,7 @@ class is3stream : public std::istream {
99111 _region = region;
100112 _bucket_name = bucket_name;
101113 _object_name = object_name;
102- _s3b-> open (_region, _bucket_name, _object_name, std::ios_base::in );
114+ open ();
103115 }
104116 /* *
105117 * Returns whether the is3stream is currently associated to an S3 object.
@@ -137,7 +149,12 @@ class is3stream : public std::istream {
137149 }
138150
139151 virtual ~is3stream () {
140- delete _s3b;
152+ try {
153+ delete _s3b;
154+ } catch (...) {
155+ // Don't throw from destructors
156+ // https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Re-never-fail
157+ }
141158 }
142159};
143160
0 commit comments