OpenASIP  2.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Private Member Functions | Static Private Attributes | List of all members
TPEF::BinaryReader Class Referenceabstract

#include <BinaryReader.hh>

Inheritance diagram for TPEF::BinaryReader:
Inheritance graph
Collaboration diagram for TPEF::BinaryReader:
Collaboration graph

Public Types

typedef unsigned long Length
 

Public Member Functions

virtual ~BinaryReader ()
 

Static Public Member Functions

static BinaryreadBinary (BinaryStream &stream)
 

Protected Member Functions

 BinaryReader ()
 
virtual BinaryreadData (BinaryStream &stream) const =0
 Does actual reading and constructing part. More...
 
virtual bool isMyStreamType (BinaryStream &stream) const =0
 Checks if given stream can be read with this BinaryReader class. More...
 

Static Protected Member Functions

static void registerBinaryReader (BinaryReader *reader)
 

Private Member Functions

 BinaryReader (const BinaryReader &)
 

Static Private Attributes

static std::set< BinaryReader * > * prototypes_ = NULL
 Contains instances of concrete BinaryReaders. More...
 

Detailed Description

Abstract base class for each actual BinaryReader class.

Handles registrations and storing of every BinaryReader instance. BinaryReaders implement Singleton design pattern.

Definition at line 51 of file BinaryReader.hh.

Member Typedef Documentation

◆ Length

typedef unsigned long TPEF::BinaryReader::Length

Definition at line 53 of file BinaryReader.hh.

Constructor & Destructor Documentation

◆ ~BinaryReader()

TPEF::BinaryReader::~BinaryReader ( )
virtual

Destructor.

Definition at line 64 of file BinaryReader.cc.

64  {
65 }

◆ BinaryReader() [1/2]

TPEF::BinaryReader::BinaryReader ( )
protected

Constructor.

Definition at line 58 of file BinaryReader.cc.

58  {
59 }

◆ BinaryReader() [2/2]

TPEF::BinaryReader::BinaryReader ( const BinaryReader )
private

Member Function Documentation

◆ isMyStreamType()

virtual bool TPEF::BinaryReader::isMyStreamType ( BinaryStream stream) const
protectedpure virtual

Checks if given stream can be read with this BinaryReader class.

Implemented in TPEF::AOutReader, and TPEF::TPEFReader.

◆ readBinary()

Binary * TPEF::BinaryReader::readBinary ( BinaryStream stream)
static

Constructs a Binary object by reading data from the given binary stream.

Looks for the concrete binary reader class that can read the given binary stream. If found, that reader is used to read and construct a Binary object. This method cleans keytables of reference manager so after running this method reference manager contain keys of just read binary file.

Parameters
streamStream to read binarys data from.
Returns
Binary that was created from the given stream.
Exceptions
InstanceNotFoundIf instance for reading wasn't found.
UnreachableStreamIf given stream can't be read.
KeyAlreadyExistsKey was in use when trying to register object.
EndOfFileIf end of file were reached while it shouldn't.
OutOfRangeSome read value was out of range.
WrongSubclassSome class couldn't do what it was asked for.
UnexpectedValueIf there was unexpected value when reading.
UnresolvedReferenceIf there was unresolved references during reading.

Definition at line 88 of file BinaryReader.cc.

88  {
89  if (prototypes_ != NULL) {
90 
91  unsigned long startPos = stream.readPosition();
92 
93  set<BinaryReader*>::iterator readers = prototypes_->begin();
94 
95  // checks if any BinaryReader registered can read this stream
96  while (readers != prototypes_->end()) {
97  // isMyStreamType should not move stream position.
98  assert(stream.readPosition() == startPos);
99 
100  if ((*readers)->isMyStreamType(stream)) {
101 
102  // isMyStreamtype must leave stream as it was
103  assert(stream.readPosition() == startPos);
104 
106 
107  Binary *readBinary = (*readers)->readData(stream);
108 
109  try {
111  } catch (const UnresolvedReference &e) {
112  std::stringstream newErrorMsg;
113  newErrorMsg
114  << "Error was probably caused by a broken input file."
115  << std::endl
116  << "Unresolved references during reading: ";
117 
118  UnresolvedReference error(
119  __FILE__, __LINE__, __func__,
120  newErrorMsg.str() + e.errorMessage());
121 
122  error.setCause(e);
123  throw error;
124  }
125 
127 
128  try {
130  } catch (const UnresolvedReference &e) {
131  std::stringstream newErrorMsg;
132  newErrorMsg
133  << "Error was probably caused by a broken input file."
134  << std::endl
135  << "Unresolved references during finalization of "
136  << "reading: ";
137 
138  UnresolvedReference error(
139  __FILE__, __LINE__, __func__,
140  newErrorMsg.str() + e.errorMessage());
141 
142  error.setCause(e);
143  throw error;
144  }
145 
146  // clean up after reading, to avoid abuse of reference manager
148 
149  return readBinary;
150  }
151  readers++;
152  }
153  }
154  throw InstanceNotFound(
155  __FILE__, __LINE__, __func__,
156  "Cannot find suitable reader implementation for file.");
157 }

References __func__, assert, TPEF::ReferenceManager::SafePointer::cleanupKeyTables(), Exception::errorMessage(), TPEF::SectionReader::finalizeBinary(), prototypes_, TPEF::BinaryStream::readPosition(), TPEF::ReferenceManager::SafePointer::resolve(), and Exception::setCause().

Referenced by TTAProgram::Program::loadFromTPEF(), TTAProgram::Program::loadFromUnscheduledTPEF(), loadInputs(), loadTPEF(), and main().

Here is the call graph for this function:

◆ readData()

virtual Binary* TPEF::BinaryReader::readData ( BinaryStream stream) const
protectedpure virtual

Does actual reading and constructing part.

Implemented in TPEF::AOutReader, and TPEF::TPEFReader.

◆ registerBinaryReader()

void TPEF::BinaryReader::registerBinaryReader ( BinaryReader reader)
staticprotected

Registers a concrete BinaryReader instance for reading a file type.

Parameters
readerConcrete reader to be registred.

Definition at line 165 of file BinaryReader.cc.

165  {
166 
167  if (prototypes_ == NULL) {
168  prototypes_ = new set<BinaryReader*>();
169  }
170 
171  prototypes_->insert(prototypes_->end(),reader);
172 }

References prototypes_.

Referenced by TPEF::AOutReader::AOutReader(), and TPEF::TPEFReader::TPEFReader().

Member Data Documentation

◆ prototypes_

set< BinaryReader * > * TPEF::BinaryReader::prototypes_ = NULL
staticprivate

Contains instances of concrete BinaryReaders.

Definition at line 74 of file BinaryReader.hh.

Referenced by readBinary(), and registerBinaryReader().


The documentation for this class was generated from the following files:
UnresolvedReference
Definition: Exception.hh:370
TPEF::BinaryReader::prototypes_
static std::set< BinaryReader * > * prototypes_
Contains instances of concrete BinaryReaders.
Definition: BinaryReader.hh:74
TPEF::SectionReader::finalizeBinary
static void finalizeBinary(Binary *binaryToFinalize, BinaryReader *reader)
Definition: SectionReader.cc:99
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::ReferenceManager::SafePointer::cleanupKeyTables
static void cleanupKeyTables()
Definition: SafePointer.cc:644
__func__
#define __func__
Definition: Application.hh:67
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TPEF::ReferenceManager::SafePointer::resolve
static void resolve()
Definition: SafePointer.cc:480
TPEF::BinaryReader::readBinary
static Binary * readBinary(BinaryStream &stream)
Definition: BinaryReader.cc:88
InstanceNotFound
Definition: Exception.hh:304