Skip to content

Commit a055300

Browse files
committed
Use std::shared_ptr<> for stream pointer
1 parent f849001 commit a055300

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

ario/ario.hpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ class ario
7070
//! @brief Represents a single member (file) in the archive
7171
class Member
7272
{
73-
friend class ario;
74-
7573
public:
7674
std::string name = {}; ///< Name of the member
7775
int date = {}; ///< Date of the member
@@ -115,18 +113,21 @@ class ario
115113
}
116114

117115
protected:
118-
void set_input_stream( std::istream* pstream )
116+
void set_input_stream( std::shared_ptr<std::istream>& pstream )
119117
{
120118
this->pstream = pstream;
121119
}
122120

123121
void set_new_data( const std::string& data ) { this->new_data = data; }
124122

125123
protected:
126-
std::string short_name = {}; ///< Short name of the member
127-
std::istream* pstream = nullptr; ///< Pointer to the input stream
128-
std::streamoff filepos = {}; ///< File position in the archive
124+
std::string short_name = {}; ///< Short name of the member
125+
std::shared_ptr<std::istream> pstream =
126+
nullptr; ///< Pointer to the input stream
127+
std::streamoff filepos = {}; ///< File position in the archive
129128
std::optional<std::string> new_data = {};
129+
130+
friend class ario;
130131
};
131132

132133
//------------------------------------------------------------------------------
@@ -393,7 +394,7 @@ class ario
393394
// Check if the member with such name already exists
394395
for ( const auto& mem : members_ ) {
395396
if ( mem.name == member.name ) {
396-
return {"Member '" + member.name + "' already exists"};
397+
return { "Member '" + member.name + "' already exists" };
397398
}
398399
}
399400

@@ -468,7 +469,7 @@ class ario
468469
{
469470
while ( true ) {
470471
Member m;
471-
m.set_input_stream( pstream.get() );
472+
m.set_input_stream( pstream );
472473

473474
char header[HEADER_SIZE];
474475
auto filepos = pstream->tellg();
@@ -620,7 +621,7 @@ class ario
620621
std::vector<std::uint32_t> calculate_member_relative_offsets()
621622
{
622623
std::vector<std::uint32_t> member_relative_offset;
623-
size_t position = 0;
624+
size_t position = 0;
624625

625626
member_relative_offset.reserve( members_.size() );
626627
for ( const auto& member : members_ ) {
@@ -678,9 +679,10 @@ class ario
678679
os.write( buf, sizeof( buf ) );
679680

680681
// Write symbol locations (location of the member in the archive)
681-
auto members_start_from = std::string( ARCH_MAGIC ).size() +
682-
static_cast<std::uint32_t>( symbol_table_size ) +
683-
static_cast<std::uint32_t>( long_names_dir_size );
682+
auto members_start_from =
683+
std::string( ARCH_MAGIC ).size() +
684+
static_cast<std::uint32_t>( symbol_table_size ) +
685+
static_cast<std::uint32_t>( long_names_dir_size );
684686
for ( const auto& symbol : symbol_table ) {
685687
auto index = static_cast<std::uint32_t>( symbol.second );
686688
auto location = members_start_from + member_relative_offset[index];
@@ -789,10 +791,11 @@ class ario
789791
return { "Failed to read symbol table" };
790792
}
791793

792-
std::uint32_t num_of_symbols = ( static_cast<std::uint8_t>( buf[0] ) << 24 ) |
793-
( static_cast<std::uint8_t>( buf[1] ) << 16 ) |
794-
( static_cast<std::uint8_t>( buf[2] ) << 8 ) |
795-
( static_cast<std::uint8_t>( buf[3] ) << 0 );
794+
std::uint32_t num_of_symbols =
795+
( static_cast<std::uint8_t>( buf[0] ) << 24 ) |
796+
( static_cast<std::uint8_t>( buf[1] ) << 16 ) |
797+
( static_cast<std::uint8_t>( buf[2] ) << 8 ) |
798+
( static_cast<std::uint8_t>( buf[3] ) << 0 );
796799

797800
std::vector<std::pair<std::uint32_t, std::string>> v( num_of_symbols );
798801

@@ -883,7 +886,7 @@ class ario
883886
//!< Pointer to the input stream
884887
//! It is used to read the archive members
885888
//! data even after the archive is loaded
886-
std::unique_ptr<std::istream> pstream = nullptr;
889+
std::shared_ptr<std::istream> pstream = nullptr;
887890
std::vector<Member> members_; //!< Vector of archive members
888891
//!< Symbol table
889892
//!< This is a map from symbol names to member indexes

0 commit comments

Comments
 (0)