KaldiObjectHolder< KaldiType > Class Template Reference

KaldiObjectHolder works for Kaldi objects that have the "standard" Read and Write functions, and a copy constructor. More...

#include <kaldi-holder-inl.h>

Collaboration diagram for KaldiObjectHolder< KaldiType >:

Public Types

typedef KaldiType T
 

Public Member Functions

 KaldiObjectHolder ()
 
void Clear ()
 
bool Read (std::istream &is)
 
TValue ()
 
void Swap (KaldiObjectHolder< T > *other)
 
bool ExtractRange (const KaldiObjectHolder< T > &other, const std::string &range)
 
 ~KaldiObjectHolder ()
 

Static Public Member Functions

static bool Write (std::ostream &os, bool binary, const T &t)
 
static bool IsReadInBinary ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (KaldiObjectHolder)
 

Private Attributes

Tt_
 

Detailed Description

template<class KaldiType>
class kaldi::KaldiObjectHolder< KaldiType >

KaldiObjectHolder works for Kaldi objects that have the "standard" Read and Write functions, and a copy constructor.

Definition at line 45 of file kaldi-holder-inl.h.

Member Typedef Documentation

◆ T

typedef KaldiType T

Definition at line 47 of file kaldi-holder-inl.h.

Constructor & Destructor Documentation

◆ KaldiObjectHolder()

KaldiObjectHolder ( )
inline

Definition at line 49 of file kaldi-holder-inl.h.

49 : t_(NULL) { }

◆ ~KaldiObjectHolder()

Member Function Documentation

◆ Clear()

void Clear ( )
inline

Definition at line 62 of file kaldi-holder-inl.h.

References KaldiObjectHolder< KaldiType >::t_.

62  {
63  if (t_) {
64  delete t_;
65  t_ = NULL;
66  }
67  }

◆ ExtractRange()

bool ExtractRange ( const KaldiObjectHolder< T > &  other,
const std::string &  range 
)
inline

Definition at line 106 of file kaldi-holder-inl.h.

References kaldi::ExtractObjectRange(), KALDI_ASSERT, and KaldiObjectHolder< KaldiType >::t_.

107  {
108  KALDI_ASSERT(other.t_ != NULL);
109  delete t_;
110  t_ = new T;
111  // this call will fail for most object types.
112  return ExtractObjectRange(*(other.t_), range, t_);
113  }
bool ExtractObjectRange(const GeneralMatrix &input, const std::string &range, GeneralMatrix *output)
GeneralMatrix is always of type BaseFloat.
Definition: kaldi-holder.cc:88
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ IsReadInBinary()

static bool IsReadInBinary ( )
inlinestatic

Definition at line 93 of file kaldi-holder-inl.h.

93 { return true; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

◆ Read()

bool Read ( std::istream &  is)
inline

Definition at line 70 of file kaldi-holder-inl.h.

References kaldi::InitKaldiInputStream(), KALDI_WARN, and KaldiObjectHolder< KaldiType >::t_.

70  {
71  delete t_;
72  t_ = new T;
73  // Don't want any existing state to complicate the read function: get new
74  // object.
75  bool is_binary;
76  if (!InitKaldiInputStream(is, &is_binary)) {
77  KALDI_WARN << "Reading Table object, failed reading binary header\n";
78  return false;
79  }
80  try {
81  t_->Read(is, is_binary);
82  return true;
83  } catch(const std::exception &e) {
84  KALDI_WARN << "Exception caught reading Table object. " << e.what();
85  delete t_;
86  t_ = NULL;
87  return false;
88  }
89  }
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
Definition: io-funcs-inl.h:306
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ Swap()

void Swap ( KaldiObjectHolder< T > *  other)
inline

Definition at line 101 of file kaldi-holder-inl.h.

References kaldi::swap(), and KaldiObjectHolder< KaldiType >::t_.

101  {
102  // the t_ values are pointers so this is a shallow swap.
103  std::swap(t_, other->t_);
104  }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)

◆ Value()

T& Value ( )
inline

Definition at line 95 of file kaldi-holder-inl.h.

References KALDI_ERR, and KaldiObjectHolder< KaldiType >::t_.

95  {
96  // code error if !t_.
97  if (!t_) KALDI_ERR << "KaldiObjectHolder::Value() called wrongly.";
98  return *t_;
99  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ Write()

static bool Write ( std::ostream &  os,
bool  binary,
const T t 
)
inlinestatic

Definition at line 51 of file kaldi-holder-inl.h.

References kaldi::InitKaldiOutputStream(), and KALDI_WARN.

51  {
52  InitKaldiOutputStream(os, binary); // Puts binary header if binary mode.
53  try {
54  t.Write(os, binary);
55  return os.good();
56  } catch(const std::exception &e) {
57  KALDI_WARN << "Exception caught writing Table object. " << e.what();
58  return false; // Write failure.
59  }
60  }
#define KALDI_WARN
Definition: kaldi-error.h:150
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
Definition: io-funcs-inl.h:291

Member Data Documentation

◆ t_


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