BasicHolder< BasicType > Class Template Reference

BasicHolder is valid for float, double, bool, and integer types. More...

#include <kaldi-holder-inl.h>

Collaboration diagram for BasicHolder< BasicType >:

Public Types

typedef BasicType T
 

Public Member Functions

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

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 (BasicHolder)
 

Private Attributes

T t_
 

Detailed Description

template<class BasicType>
class kaldi::BasicHolder< BasicType >

BasicHolder is valid for float, double, bool, and integer types.

There will be a compile time error otherwise, because we make sure that the {Write, Read}BasicType functions do not get instantiated for other types.

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

Member Typedef Documentation

◆ T

typedef BasicType T

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

Constructor & Destructor Documentation

◆ BasicHolder()

BasicHolder ( )
inline

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

131 : t_(static_cast<T>(-1)) { }

◆ ~BasicHolder()

~BasicHolder ( )
inline

Member Function Documentation

◆ Clear()

void Clear ( )
inline

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

146 { }

◆ ExtractRange()

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

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

References KALDI_ERR.

206  {
207  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
208  return false;
209  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsReadInBinary()

static bool IsReadInBinary ( )
inlinestatic

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

196 { return true; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( BasicHolder< BasicType >  )
private

◆ Read()

bool Read ( std::istream &  is)
inline

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

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

149  {
150  bool is_binary;
151  if (!InitKaldiInputStream(is, &is_binary)) {
152  KALDI_WARN << "Reading Table object [integer type], failed reading binary"
153  " header\n";
154  return false;
155  }
156  try {
157  int c;
158  if (!is_binary) { // This is to catch errors, the class would work
159  // without it..
160  // Eat up any whitespace and make sure it's not newline.
161  while (isspace((c = is.peek())) && c != static_cast<int>('\n')) {
162  is.get();
163  }
164  if (is.peek() == '\n') {
165  KALDI_WARN << "Found newline but expected basic type.";
166  return false; // This is just to catch a more-
167  // likely-than average type of error (empty line before the token),
168  // since ReadBasicType will eat it up.
169  }
170  }
171 
172  ReadBasicType(is, is_binary, &t_);
173 
174  if (!is_binary) { // This is to catch errors, the class would work
175  // without it..
176  // make sure there is a newline.
177  while (isspace((c = is.peek())) && c != static_cast<int>('\n')) {
178  is.get();
179  }
180  if (is.peek() != '\n') {
181  KALDI_WARN << "BasicHolder::Read, expected newline, got "
182  << CharToString(is.peek()) << ", position " << is.tellg();
183  return false;
184  }
185  is.get(); // Consume the newline.
186  }
187  return true;
188  } catch(const std::exception &e) {
189  KALDI_WARN << "Exception caught reading Table object. " << e.what();
190  return false;
191  }
192  }
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
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string CharToString(const char &c)
Definition: kaldi-utils.cc:36

◆ Swap()

void Swap ( BasicHolder< T > *  other)
inline

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

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

202  {
203  std::swap(t_, other->t_);
204  }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)

◆ Value()

T& Value ( )
inline

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

References KaldiObjectHolder< KaldiType >::t_.

198  {
199  return t_;
200  }

◆ Write()

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

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

References kaldi::InitKaldiOutputStream(), KALDI_WARN, and kaldi::WriteBasicType().

133  {
134  InitKaldiOutputStream(os, binary); // Puts binary header if binary mode.
135  try {
136  WriteBasicType(os, binary, t);
137  if (!binary) os << '\n'; // Makes output format more readable and
138  // easier to manipulate.
139  return os.good();
140  } catch(const std::exception &e) {
141  KALDI_WARN << "Exception caught writing Table object. " << e.what();
142  return false; // Write failure.
143  }
144  }
#define KALDI_WARN
Definition: kaldi-error.h:150
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
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_

T t_
private

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

Referenced by BasicHolder< BasicType >::Swap().


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