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

#include <SectionWriter.hh>

Inheritance diagram for TPEF::SectionWriter:
Inheritance graph
Collaboration diagram for TPEF::SectionWriter:
Collaboration graph

Static Public Member Functions

static void writeHeader (BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
 
static void writeData (BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
 
static void finalizeBinary (BinaryStream &stream, const Binary *binaryToFinalize, const BinaryWriter *writer)
 

Protected Member Functions

 SectionWriter ()
 
virtual ~SectionWriter ()
 
virtual const BinaryWriterparent () const =0
 Gets parent of instance which for SectionWriter is created. More...
 
virtual Section::SectionType type () const =0
 Returns SectionType that actual reader or writer instance can handle. More...
 
virtual void finalize (BinaryStream &stream, Section *section) const
 
virtual void actualWriteHeader (BinaryStream &stream, const Section *sect) const =0
 Does actual writing of sections header. More...
 
virtual void actualWriteData (BinaryStream &stream, const Section *sect) const =0
 Does actual writing of sections data. More...
 

Static Protected Member Functions

static void registerSectionWriter (const SectionWriter *sWriter)
 
static SectionId getUniqueSectionId ()
 

Private Types

typedef std::pair< const Section::SectionType, const BinaryWriter * > MapKey
 Key type for finding values in map of section writers. More...
 
typedef std::map< MapKey, const SectionWriter * > MapType
 Map type that contains instances of registered section writers. More...
 

Private Member Functions

 SectionWriter (const SectionWriter &)
 

Static Private Member Functions

static const SectionWriterfindSectionWriter (const Section::SectionType type, const BinaryWriter *bWriter)
 

Static Private Attributes

static MapTypeprototypes_ = NULL
 Contains section writers for all kinds of sections and all kinds of binrary fromats that are supported. More...
 

Detailed Description

Abstract base class for actual section writer classes.

SectionWriter base class stores instances of all registed section writer classes and serve registeration methods for actual SectionWriter instances. Actual section writer classes needs also static instance() method to work correctly.

Definition at line 58 of file SectionWriter.hh.

Member Typedef Documentation

◆ MapKey

typedef std::pair<const Section::SectionType, const BinaryWriter*> TPEF::SectionWriter::MapKey
private

Key type for finding values in map of section writers.

Definition at line 104 of file SectionWriter.hh.

◆ MapType

typedef std::map<MapKey, const SectionWriter*> TPEF::SectionWriter::MapType
private

Map type that contains instances of registered section writers.

Definition at line 107 of file SectionWriter.hh.

Constructor & Destructor Documentation

◆ SectionWriter() [1/2]

TPEF::SectionWriter::SectionWriter ( )
protected

Constructor

Definition at line 44 of file SectionWriter.cc.

44  {
45 }

◆ ~SectionWriter()

TPEF::SectionWriter::~SectionWriter ( )
protectedvirtual

Destructor

Definition at line 50 of file SectionWriter.cc.

50  {
51 }

◆ SectionWriter() [2/2]

TPEF::SectionWriter::SectionWriter ( const SectionWriter )
private

Member Function Documentation

◆ actualWriteData()

virtual void TPEF::SectionWriter::actualWriteData ( BinaryStream stream,
const Section sect 
) const
protectedpure virtual

◆ actualWriteHeader()

virtual void TPEF::SectionWriter::actualWriteHeader ( BinaryStream stream,
const Section sect 
) const
protectedpure virtual

Does actual writing of sections header.

Implemented in TPEF::TPEFSectionWriter.

Referenced by writeHeader().

◆ finalize()

void TPEF::SectionWriter::finalize ( BinaryStream stream,
Section section 
) const
protectedvirtual

Default finalizer method.

This is used, if section does not have its own method defined.

Parameters
streamStream that contains binary to finalize.
sectionSection to finalize.

Definition at line 192 of file SectionWriter.cc.

193  {
194 }

Referenced by finalizeBinary().

◆ finalizeBinary()

void TPEF::SectionWriter::finalizeBinary ( BinaryStream stream,
const Binary bin,
const BinaryWriter writer 
)
static

Writes those values to stream, which can't be written before all objects are written down.

This is used for example when some section writer has to update data of other section or sections.

Parameters
streamStream that contains binary to finalize.
BinaryBinary file which needs finalization.
writerWriter, which was used for writing binary.

Definition at line 99 of file SectionWriter.cc.

102  {
103 
104  for (Word i = 0; i < bin->sectionCount(); i++) {
105  // get section and finalize it
106  Section* sect =bin->section(i);
107 
108  try {
109  const SectionWriter* sectionWriter =
110  findSectionWriter(sect->type(), writer);
111  sectionWriter->finalize(stream, sect);
112 
113  } catch (const InstanceNotFound &e) {
114  // there is not always reader for every created section
115  // and it's ok.
116  }
117  }
118 }

References finalize(), findSectionWriter(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), and TPEF::Section::type().

Referenced by TPEF::BinaryWriter::writeBinary().

Here is the call graph for this function:

◆ findSectionWriter()

const SectionWriter * TPEF::SectionWriter::findSectionWriter ( const Section::SectionType  type,
const BinaryWriter bWriter 
)
staticprivate

Finds SectionWriter instance by SectionType and BinaryWriter*.

Parameters
typeType of section to find.
bWriterBinaryWriter which requested finding section.
Returns
Instance which can write section.
Exceptions
InstanceNotFoundWriter instance was not found.

Definition at line 129 of file SectionWriter.cc.

130  {
131  MapKey key(type, bWriter);
132 
133  if (prototypes_ == NULL ||
135  throw InstanceNotFound(__FILE__, __LINE__,
136  "SectionWriter::findSectionWriter");
137  }
138 
139  return (*prototypes_)[key];
140 }

References MapTools::containsKey(), prototypes_, and type().

Referenced by finalizeBinary(), writeData(), and writeHeader().

Here is the call graph for this function:

◆ getUniqueSectionId()

SectionId TPEF::SectionWriter::getUniqueSectionId ( )
staticprotected

Generates a unique section identification code.

If several files are written out, the section identification codes of each file will be starting from a different numeric code. Never returns the reserved identification code for null/invalid section.

Definition at line 172 of file SectionWriter.cc.

172  {
173 
174  // counter used to generate unique identification codes
175  static SectionId counter = 0;
176 
177  counter++;
178  // skip zero (reserved identification code) if the counter wraps around
179  if (counter == 0) counter++;
180  return counter;
181 }

Referenced by TPEF::TPEFSectionWriter::getSectionId().

◆ parent()

virtual const BinaryWriter& TPEF::SectionWriter::parent ( ) const
protectedpure virtual

Gets parent of instance which for SectionWriter is created.

Implemented in TPEF::TPEFSectionWriter.

Referenced by registerSectionWriter().

◆ registerSectionWriter()

void TPEF::SectionWriter::registerSectionWriter ( const SectionWriter sWriter)
staticprotected

Registers SectionWriter instance for writing specific section type.

Parameters
sWriterInstance to register for writing.

Definition at line 148 of file SectionWriter.cc.

148  {
149 
150  MapKey key(sWriter->type(), &sWriter->parent());
151 
152  // We can't create prototypes_ map statically, because we don't know
153  // if it is initialized before this method is called.
154  if (prototypes_ == NULL) {
155  prototypes_ = new MapType();
156  }
157 
159 
160  (*prototypes_)[key] = sWriter;
161 
162 }

References assert, MapTools::containsKey(), parent(), prototypes_, and type().

Referenced by TPEF::TPEFASpaceSectionWriter::TPEFASpaceSectionWriter(), TPEF::TPEFCodeSectionWriter::TPEFCodeSectionWriter(), TPEF::TPEFDataSectionWriter::TPEFDataSectionWriter(), TPEF::TPEFDebugSectionWriter::TPEFDebugSectionWriter(), TPEF::TPEFLEDataSectionWriter::TPEFLEDataSectionWriter(), TPEF::TPEFLineNumSectionWriter::TPEFLineNumSectionWriter(), TPEF::TPEFNullSectionWriter::TPEFNullSectionWriter(), TPEF::TPEFRelocSectionWriter::TPEFRelocSectionWriter(), TPEF::TPEFResourceSectionWriter::TPEFResourceSectionWriter(), TPEF::TPEFStringSectionWriter::TPEFStringSectionWriter(), TPEF::TPEFSymbolSectionWriter::TPEFSymbolSectionWriter(), and TPEF::TPEFUDataSectionWriter::TPEFUDataSectionWriter().

Here is the call graph for this function:

◆ type()

virtual Section::SectionType TPEF::SectionWriter::type ( ) const
protectedpure virtual

◆ writeData()

void TPEF::SectionWriter::writeData ( BinaryStream stream,
const Section sect,
const BinaryWriter writer 
)
static

Finds correct SectionWriter instance and command it to write section data.

Parameters
streamStream where to write.
sectSection which data will be written.
writerBinaryWriter which filetype we want to write.
Exceptions
InstanceNotFoundIf there wasn't found any writer for section.

Definition at line 79 of file SectionWriter.cc.

82  {
83 
84  findSectionWriter(sect->type(), writer)->actualWriteData(stream, sect);
85 }

References actualWriteData(), findSectionWriter(), and TPEF::Section::type().

Referenced by TPEF::TPEFWriter::actualWriteBinary().

Here is the call graph for this function:

◆ writeHeader()

void TPEF::SectionWriter::writeHeader ( BinaryStream stream,
const Section sect,
const BinaryWriter writer 
)
static

Finds correct SectionWriter instance and use it to write section header.

Parameters
streamStream where to write.
sectSection which header will be written.
writerBinaryWriter which filetype we want to write.
Exceptions
InstanceNotFoundIf there wasn't found any writer for section.

Definition at line 62 of file SectionWriter.cc.

65  {
66 
67  findSectionWriter(sect->type(), writer)->actualWriteHeader(stream, sect);
68 }

References actualWriteHeader(), findSectionWriter(), and TPEF::Section::type().

Referenced by TPEF::TPEFWriter::actualWriteBinary().

Here is the call graph for this function:

Member Data Documentation

◆ prototypes_

SectionWriter::MapType * TPEF::SectionWriter::prototypes_ = NULL
staticprivate

Contains section writers for all kinds of sections and all kinds of binrary fromats that are supported.

Definition at line 116 of file SectionWriter.hh.

Referenced by findSectionWriter(), and registerSectionWriter().


The documentation for this class was generated from the following files:
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::SectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const =0
Does actual writing of sections data.
TPEF::SectionWriter::type
virtual Section::SectionType type() const =0
Returns SectionType that actual reader or writer instance can handle.
TPEF::SectionWriter::actualWriteHeader
virtual void actualWriteHeader(BinaryStream &stream, const Section *sect) const =0
Does actual writing of sections header.
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::SectionWriter::findSectionWriter
static const SectionWriter * findSectionWriter(const Section::SectionType type, const BinaryWriter *bWriter)
Definition: SectionWriter.cc:129
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
TPEF::SectionWriter::MapKey
std::pair< const Section::SectionType, const BinaryWriter * > MapKey
Key type for finding values in map of section writers.
Definition: SectionWriter.hh:104
TPEF::SectionWriter::SectionWriter
SectionWriter()
Definition: SectionWriter.cc:44
TPEF::SectionWriter::prototypes_
static MapType * prototypes_
Contains section writers for all kinds of sections and all kinds of binrary fromats that are supporte...
Definition: SectionWriter.hh:116
TPEF::SectionWriter::MapType
std::map< MapKey, const SectionWriter * > MapType
Map type that contains instances of registered section writers.
Definition: SectionWriter.hh:107
InstanceNotFound
Definition: Exception.hh:304