TableWriterArchiveImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

Inheritance diagram for TableWriterArchiveImpl< Holder >:
Collaboration diagram for TableWriterArchiveImpl< Holder >:

Public Types

typedef Holder::T T
 
- Public Types inherited from TableWriterImplBase< Holder >
typedef Holder::T T
 

Public Member Functions

virtual bool Open (const std::string &wspecifier)
 
virtual bool IsOpen () const
 
virtual bool Write (const std::string &key, const T &value)
 
virtual void Flush ()
 
virtual bool Close ()
 
 TableWriterArchiveImpl ()
 
virtual ~TableWriterArchiveImpl ()
 
- Public Member Functions inherited from TableWriterImplBase< Holder >
virtual ~TableWriterImplBase ()
 
 TableWriterImplBase ()
 

Private Types

enum  { kUninitialized, kOpen, kWriteError }
 

Private Attributes

Output output_
 
WspecifierOptions opts_
 
std::string wspecifier_
 
std::string archive_wxfilename_
 
enum kaldi::TableWriterArchiveImpl:: { ... }  state_
 

Detailed Description

template<class Holder>
class kaldi::TableWriterArchiveImpl< Holder >

Definition at line 996 of file kaldi-table-inl.h.

Member Typedef Documentation

◆ T

typedef Holder::T T

Definition at line 998 of file kaldi-table-inl.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
kUninitialized 
kOpen 
kWriteError 

Definition at line 1118 of file kaldi-table-inl.h.

Constructor & Destructor Documentation

◆ TableWriterArchiveImpl()

Definition at line 1103 of file kaldi-table-inl.h.

◆ ~TableWriterArchiveImpl()

virtual ~TableWriterArchiveImpl ( )
inlinevirtual

Definition at line 1106 of file kaldi-table-inl.h.

References SequentialTableReader< Holder >::Close(), SequentialTableReader< Holder >::IsOpen(), and KALDI_ERR.

1106  {
1107  if (!IsOpen()) return;
1108  else if (!Close())
1109  KALDI_ERR << "At TableWriter destructor: Write failed or stream close "
1110  << "failed: wspecifier is "<< wspecifier_;
1111  }
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1083 of file kaldi-table-inl.h.

References SequentialTableReader< Holder >::IsOpen(), KALDI_ERR, and KALDI_WARN.

1083  {
1084  if (!this->IsOpen() || !output_.IsOpen())
1085  KALDI_ERR << "Close called on a stream that was not open."
1086  << this->IsOpen() << ", " << output_.IsOpen();
1087  bool close_success = output_.Close();
1088  if (!close_success) {
1089  KALDI_WARN << "Error closing stream: wspecifier is " << wspecifier_;
1091  return false;
1092  }
1093  if (state_ == kWriteError) {
1094  KALDI_WARN << "Closing writer in error state: wspecifier is "
1095  << wspecifier_;
1097  return false;
1098  }
1100  return true;
1101  }
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
enum kaldi::TableWriterArchiveImpl::@1 state_
bool Close()
Definition: kaldi-io.cc:677

◆ Flush()

virtual void Flush ( )
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1073 of file kaldi-table-inl.h.

References KALDI_WARN.

1073  {
1074  switch (state_) {
1075  case kWriteError: case kOpen:
1076  output_.Stream().flush(); // Don't check error status.
1077  return;
1078  default:
1079  KALDI_WARN << "Flush called on not-open writer.";
1080  }
1081  }
std::ostream & Stream()
Definition: kaldi-io.cc:701
#define KALDI_WARN
Definition: kaldi-error.h:150
enum kaldi::TableWriterArchiveImpl::@1 state_

◆ IsOpen()

virtual bool IsOpen ( ) const
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1030 of file kaldi-table-inl.h.

References KALDI_ERR.

1030  {
1031  switch (state_) {
1032  case kUninitialized: return false;
1033  case kOpen: case kWriteError: return true;
1034  default: KALDI_ERR << "IsOpen() called on TableWriter in invalid state.";
1035  }
1036  return false;
1037  }
#define KALDI_ERR
Definition: kaldi-error.h:147
enum kaldi::TableWriterArchiveImpl::@1 state_

◆ Open()

virtual bool Open ( const std::string &  wspecifier)
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1000 of file kaldi-table-inl.h.

References kaldi::ClassifyWspecifier(), SequentialTableReader< Holder >::Close(), KALDI_ASSERT, KALDI_ERR, and kaldi::kArchiveWspecifier.

1000  {
1001  switch (state_) {
1002  case kUninitialized:
1003  break;
1004  case kWriteError:
1005  KALDI_ERR << "Opening stream, already open with write error.";
1006  case kOpen: default:
1007  if (!Close()) // throw because this error may not have been previously
1008  // detected by the user.
1009  KALDI_ERR << "Opening stream, error closing previously open stream.";
1010  }
1011  wspecifier_ = wspecifier;
1012  WspecifierType ws = ClassifyWspecifier(wspecifier,
1014  NULL,
1015  &opts_);
1016  KALDI_ASSERT(ws == kArchiveWspecifier); // or wrongly called.
1017 
1018  if (output_.Open(archive_wxfilename_, opts_.binary, false)) { // false
1019  // means no binary header.
1020  state_ = kOpen;
1021  return true;
1022  } else {
1023  // stream will not be open. User will report this error
1024  // (we return bool), so don't bother printing anything.
1026  return false;
1027  }
1028  }
#define KALDI_ERR
Definition: kaldi-error.h:147
WspecifierType
Definition: kaldi-table.h:106
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
bool Open(const std::string &wxfilename, bool binary, bool write_header)
This opens the stream, with the given mode (binary or text).
Definition: kaldi-io.cc:707
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
enum kaldi::TableWriterArchiveImpl::@1 state_

◆ Write()

virtual bool Write ( const std::string &  key,
const T value 
)
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1041 of file kaldi-table-inl.h.

References kaldi::IsToken(), KALDI_ERR, KALDI_WARN, and kaldi::PrintableWxfilename().

1041  {
1042  switch (state_) {
1043  case kOpen: break;
1044  case kWriteError:
1045  // user should have known from the last
1046  // call to Write that there was a problem.
1047  KALDI_WARN << "Attempting to write to invalid stream.";
1048  return false;
1049  case kUninitialized: default:
1050  KALDI_ERR << "Write called on invalid stream";
1051  }
1052  // state is now kOpen or kWriteError.
1053  if (!IsToken(key)) // e.g. empty string or has spaces...
1054  KALDI_ERR << "Using invalid key " << key;
1055  output_.Stream() << key << ' ';
1056  if (!Holder::Write(output_.Stream(), opts_.binary, value)) {
1057  KALDI_WARN << "Write failure to "
1059  state_ = kWriteError;
1060  return false;
1061  }
1062  if (state_ == kWriteError) return false; // Even if this Write seems to
1063  // have succeeded, we fail because a previous Write failed and the archive
1064  // may be corrupted and unreadable.
1065 
1066  if (opts_.flush)
1067  Flush();
1068  return true;
1069  }
bool IsToken(const std::string &token)
Returns true if "token" is nonempty, and all characters are printable and whitespace-free.
Definition: text-utils.cc:105
std::ostream & Stream()
Definition: kaldi-io.cc:701
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
enum kaldi::TableWriterArchiveImpl::@1 state_

Member Data Documentation

◆ archive_wxfilename_

std::string archive_wxfilename_
private

Definition at line 1117 of file kaldi-table-inl.h.

◆ opts_

WspecifierOptions opts_
private

Definition at line 1115 of file kaldi-table-inl.h.

◆ output_

Output output_
private

Definition at line 1114 of file kaldi-table-inl.h.

◆ state_

enum { ... } state_

◆ wspecifier_

std::string wspecifier_
private

Definition at line 1116 of file kaldi-table-inl.h.


The documentation for this class was generated from the following file: