Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow BamWriter deep copy #224

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/api/BamWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,47 @@ BamWriter::BamWriter()
*/
BamWriter::~BamWriter()
{
delete d;
d = 0;
if (d) delete d;
d = nullptr;
}

/*! \fn BamWriter::BamWriter(const BamWriter& bw)
\brief copy constructor, deep copy
*/
BamWriter::BamWriter(const BamWriter& bw)
: d(nullptr)
{
if (bw.d) d = new BamWriterPrivate(*bw.d);
}

/*! \fn BamWriter::BamWriter()
\brief move constructor, shallow copy
*/
BamWriter::BamWriter(BamWriter&& bw) noexcept
: d(bw.d)
{
bw.d = nullptr;
}

/*! \fn BamWriter::operator=(const BamWriter& bw)
\brief copy assignement operator, deep copy
*/
BamWriter& BamWriter::operator=(const BamWriter& bw)
{
if (d) delete d;
if (bw.d) d = new BamWriterPrivate(*bw.d);
return *this;
}

/*! \fn BamWriter::operator=(const BamWriter& bw)
\brief move assignement operator, shallow copy
*/
BamWriter& BamWriter::operator=(BamWriter&& bw) noexcept
{
if (d) delete d;
d = bw.d;
bw.d = nullptr;
return *this;
}

/*! \fn BamWriter::Close()
Expand Down
4 changes: 4 additions & 0 deletions src/api/BamWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class API_EXPORT BamWriter
public:
BamWriter();
~BamWriter();
BamWriter(const BamWriter& bw);
BamWriter(BamWriter&& bw) noexcept;
BamWriter& operator=(const BamWriter& bw);
BamWriter& operator=(BamWriter&& bw) noexcept;

// public interface
public:
Expand Down
4 changes: 4 additions & 0 deletions src/api/internal/bam/BamWriter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class API_NO_EXPORT BamWriterPrivate
public:
BamWriterPrivate();
~BamWriterPrivate();
BamWriterPrivate(const BamWriterPrivate&) = default;
BamWriterPrivate(BamWriterPrivate&&) = default;
BamWriterPrivate& operator=(const BamWriterPrivate&) = delete;
BamWriterPrivate& operator=(const BamWriterPrivate&&) = delete;

// interface methods
public:
Expand Down
17 changes: 7 additions & 10 deletions src/utils/bamtools_fasta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct Fasta::FastaPrivate
void Chomp(char* sequence);
bool GetNameFromHeader(const std::string& header, std::string& name);
bool GetNextHeader(std::string& header);
bool GetNextSequence(std::string& sequence);
bool GetNextSequence(std::string& sequence, size_t count = -1);
bool LoadIndexData();
bool Rewind();
bool WriteIndexData();
Expand Down Expand Up @@ -387,18 +387,18 @@ bool Fasta::FastaPrivate::GetNextHeader(std::string& header)
return true;
}

bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence)
bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence, size_t count)
{

sequence.clear();
// validate input stream
if (!IsOpen || feof(Stream)) {
return false;
}

// read in sequence
char buffer[1024];
std::ostringstream seqBuffer;
while (true) {
while (sequence.size() < count) {

char ch = fgetc(Stream);
ungetc(ch, Stream);
Expand All @@ -412,12 +412,9 @@ bool Fasta::FastaPrivate::GetNextSequence(std::string& sequence)
}

Chomp(buffer);
seqBuffer << buffer;
sequence.append(buffer);
}

// import buffer contents to sequence string
sequence = seqBuffer.str();

// return success
return true;
}
Expand Down Expand Up @@ -459,13 +456,13 @@ bool Fasta::FastaPrivate::GetSequence(const int& refId, const int& start, const

// retrieve full sequence
std::string fullSequence;
if (!GetNextSequence(fullSequence)) {
const int seqLength = (stop - start) + 1;
if (!GetNextSequence(fullSequence, stop + 1)) {
std::cerr << "FASTA error : could not retrieve sequence from FASTA file" << std::endl;
return false;
}

// set sub-sequence & return success
const int seqLength = (stop - start) + 1;
sequence = fullSequence.substr(start, seqLength);
return true;
}
Expand Down