OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
TPEF::ValueReplacer Class Referenceabstract

#include <ValueReplacer.hh>

Inheritance diagram for TPEF::ValueReplacer:
Inheritance graph
Collaboration diagram for TPEF::ValueReplacer:
Collaboration graph

Public Member Functions

void resolve ()
 

Static Public Member Functions

static void finalize ()
 
static void initialize (BinaryStream &stream)
 

Protected Member Functions

 ValueReplacer (const SafePointable *obj)
 
 ValueReplacer (const ValueReplacer &replacer)
 
virtual ~ValueReplacer ()
 
virtual bool tryToReplace ()=0
 Does replacement if can. If can't returns false.
 
virtual ValueReplacerclone ()=0
 Creates dynamically allocated clone of object.
 
const SafePointablereference () const
 
unsigned int streamPosition () const
 

Static Protected Member Functions

static BinaryStreamstream ()
 

Static Private Member Functions

static void addReplacement (ValueReplacer *obj)
 

Private Attributes

unsigned int streamPosition_
 File offset where replacement is done.
 
const SafePointablereference_
 Reference which to be written.
 

Static Private Attributes

static BinaryStreamstream_ = NULL
 Stream for writing replacements.
 
static std::set< ValueReplacer * > replacements_
 Replacements which should be done afterwards.
 

Detailed Description

Abstract base class of value replacer classes.

A value replacer class handles writing of object references into an output binary stream. An object reference is represented with a value in the stream. The order in which reference values are computed may depend on the data already written into the stream, which may include other references whose value is not yet available. When an object reference must be written out and its corresponding value is not yet available, value replacers are used.

E.g., If file offset to the body of the section should is written while writing section header, FileOffsetReplacer is used to write FileOffset of the first element of the section. When file offset of the first element is known, the FileOffsetReplacer object knows where to replace write that offset.

Value replacers defer the writing of the actual reference values. A dummy reference value is written out in stream sequential order and later replaced witht he actual value whent his becomes available.

The ValueReplacer base class is not a pure interface: it handles all bookkeeping of deferred replacements.

Each concrete value replacer handles the writing of reference to a specific object type in a specific data format.

Definition at line 71 of file ValueReplacer.hh.

Constructor & Destructor Documentation

◆ ValueReplacer() [1/2]

TPEF::ValueReplacer::ValueReplacer ( const SafePointable obj)
protected

Constructor

Parameters
objObject whole key is used for replacing bytes in stream.

Definition at line 53 of file ValueReplacer.cc.

53 :
54 reference_(obj) {
55
56 assert(stream_ != NULL);
57 assert(obj != NULL);
58
59 try {
61 } catch (UnreachableStream const& e) {
62 bool replacerConstructorBadStream = false;
63 assert(replacerConstructorBadStream);
64 }
65}
#define assert(condition)
unsigned int writePosition()
unsigned int streamPosition_
File offset where replacement is done.
static BinaryStream * stream_
Stream for writing replacements.
const SafePointable * reference_
Reference which to be written.

References assert, stream_, streamPosition_, and TPEF::BinaryStream::writePosition().

Here is the call graph for this function:

◆ ValueReplacer() [2/2]

TPEF::ValueReplacer::ValueReplacer ( const ValueReplacer replacer)
protected

Copy constructor

@para replacer Replacer which is copied.

Definition at line 72 of file ValueReplacer.cc.

72 :
73 streamPosition_(replacer.streamPosition_),
74 reference_(replacer.reference_) {
75}

◆ ~ValueReplacer()

TPEF::ValueReplacer::~ValueReplacer ( )
protectedvirtual

Destructor

Definition at line 80 of file ValueReplacer.cc.

80 {
81}

Member Function Documentation

◆ addReplacement()

void TPEF::ValueReplacer::addReplacement ( ValueReplacer replacer)
staticprivate

Adds a replacement object for later writing.

Parameters
replacerReplacement that should be done later.

Definition at line 150 of file ValueReplacer.cc.

150 {
151 replacements_.insert(replacements_.end(),replacer);
152}
static std::set< ValueReplacer * > replacements_
Replacements which should be done afterwards.

References replacements_.

Referenced by resolve().

◆ clone()

virtual ValueReplacer * TPEF::ValueReplacer::clone ( )
protectedpure virtual

Creates dynamically allocated clone of object.

Implemented in TPEF::FileOffsetReplacer, TPEF::SectionIdReplacer, TPEF::SectionIndexReplacer, TPEF::SectionOffsetReplacer, and TPEF::SectionSizeReplacer.

Referenced by resolve().

◆ finalize()

void TPEF::ValueReplacer::finalize ( )
static

Tries to do those replacement which couldn't do while writing.

Deletes all remaining replacer instances after replacements were done. When method is runned and no exceptions are thrown method cleans up key tables of reference manager and unset's BinaryStream where replacements were written. So initilize() method must be called again before starting new writing. If MissingKeys exception is thrown, then those replacements are not freed, which could not be done before. Keys can be added and finalize may be called again.

Exceptions
MissingKeysIf ReferenceManager doesn't contain needed keys.
UnreachableStreamWriting can't be done, because of bad stream.
WritePastEOFIf replacer tried to write past end of file.

Definition at line 127 of file ValueReplacer.cc.

127 {
128 while (!replacements_.empty()) {
129 ValueReplacer* replacer = *(replacements_.begin());
130
131 if(!replacer->tryToReplace()) {
132 throw MissingKeys(
133 __FILE__, __LINE__, __func__,
134 "Can't replace object because of missing key.");
135 }
136
137 delete replacer;
138 replacements_.erase(replacer);
139 }
140
141 stream_ = NULL;
142}
#define __func__
ValueReplacer(const SafePointable *obj)

References __func__, replacements_, stream_, and tryToReplace().

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

Here is the call graph for this function:

◆ initialize()

void TPEF::ValueReplacer::initialize ( BinaryStream stream)
static

Initializes ValueReplacer for writing.

Sets stream which we want write to and clears up key table of reference manager so there won't be any keys with wrong values.

Parameters
streamStream where replacements are done.

Definition at line 105 of file ValueReplacer.cc.

105 {
106 stream_ = &stream;
107 assert(stream_ != NULL);
108 replacements_.clear();
109}
static BinaryStream & stream()

References assert, replacements_, stream(), and stream_.

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

Here is the call graph for this function:

◆ reference()

const SafePointable * TPEF::ValueReplacer::reference ( ) const
protected

◆ resolve()

void TPEF::ValueReplacer::resolve ( )

◆ stream()

BinaryStream & TPEF::ValueReplacer::stream ( )
staticprotected

◆ streamPosition()

unsigned int TPEF::ValueReplacer::streamPosition ( ) const
protected

Stream position where to write reference.

Returns
Position where to write reference.

Definition at line 180 of file ValueReplacer.cc.

180 {
181 return streamPosition_;
182}

References streamPosition_.

Referenced by TPEF::FileOffsetReplacer::tryToReplace(), TPEF::SectionIdReplacer::tryToReplace(), TPEF::SectionIndexReplacer::tryToReplace(), TPEF::SectionOffsetReplacer::tryToReplace(), and TPEF::SectionSizeReplacer::tryToReplace().

◆ tryToReplace()

virtual bool TPEF::ValueReplacer::tryToReplace ( )
protectedpure virtual

Does replacement if can. If can't returns false.

Implemented in TPEF::FileOffsetReplacer, TPEF::SectionIdReplacer, TPEF::SectionIndexReplacer, TPEF::SectionOffsetReplacer, and TPEF::SectionSizeReplacer.

Referenced by finalize(), and resolve().

Member Data Documentation

◆ reference_

const SafePointable* TPEF::ValueReplacer::reference_
private

Reference which to be written.

Definition at line 102 of file ValueReplacer.hh.

Referenced by reference().

◆ replacements_

std::set< ValueReplacer * > TPEF::ValueReplacer::replacements_
staticprivate

Replacements which should be done afterwards.

Definition at line 107 of file ValueReplacer.hh.

Referenced by addReplacement(), finalize(), and initialize().

◆ stream_

BinaryStream * TPEF::ValueReplacer::stream_ = NULL
staticprivate

Stream for writing replacements.

Definition at line 104 of file ValueReplacer.hh.

Referenced by finalize(), initialize(), stream(), and ValueReplacer().

◆ streamPosition_

unsigned int TPEF::ValueReplacer::streamPosition_
private

File offset where replacement is done.

Definition at line 100 of file ValueReplacer.hh.

Referenced by streamPosition(), and ValueReplacer().


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