OpenASIP 2.2
Loading...
Searching...
No Matches
SectionWriter.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file SectionWriter.cc
26 *
27 * Definition of SectionWriter class.
28 *
29 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30 *
31 * @note rating: yellow
32 */
33
34#include "SectionWriter.hh"
35#include "BinaryWriter.hh"
36
37namespace TPEF {
38
40
41/**
42 * Constructor
43 */
46
47/**
48 * Destructor
49 */
52
53/**
54 * Finds correct SectionWriter instance and use it to write section header.
55 *
56 * @param stream Stream where to write.
57 * @param sect Section which header will be written.
58 * @param writer BinaryWriter which filetype we want to write.
59 * @exception InstanceNotFound If there wasn't found any writer for section.
60 */
61void
63 BinaryStream& stream,
64 const Section* sect,
65 const BinaryWriter* writer) {
66
67 findSectionWriter(sect->type(), writer)->actualWriteHeader(stream, sect);
68}
69
70/**
71 * Finds correct SectionWriter instance and command it to write section data.
72 *
73 * @param stream Stream where to write.
74 * @param sect Section which data will be written.
75 * @param writer BinaryWriter which filetype we want to write.
76 * @exception InstanceNotFound If there wasn't found any writer for section.
77 */
78void
80 BinaryStream& stream,
81 const Section* sect,
82 const BinaryWriter* writer) {
83
84 findSectionWriter(sect->type(), writer)->actualWriteData(stream, sect);
85}
86
87/**
88 * Writes those values to stream, which can't be written before all objects
89 * are written down.
90 *
91 * This is used for example when some section writer has to update data of
92 * other section or sections.
93 *
94 * @param stream Stream that contains binary to finalize.
95 * @param Binary Binary file which needs finalization.
96 * @param writer Writer, which was used for writing binary.
97 */
98void
100 BinaryStream& stream,
101 const Binary* bin,
102 const BinaryWriter* writer) {
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}
119
120/**
121 * Finds SectionWriter instance by SectionType and BinaryWriter*.
122 *
123 * @param type Type of section to find.
124 * @param bWriter BinaryWriter which requested finding section.
125 * @return Instance which can write section.
126 * @exception InstanceNotFound Writer instance was not found.
127 */
128const SectionWriter*
130 const Section::SectionType type, const BinaryWriter* bWriter) {
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}
141
142/**
143 * Registers SectionWriter instance for writing specific section type.
144 *
145 * @param sWriter Instance to register for writing.
146 */
147void
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}
163
164/**
165 * Generates a unique section identification code.
166 *
167 * If several files are written out, the section identification codes of
168 * each file will be starting from a different numeric code. Never returns
169 * the reserved identification code for null/invalid section.
170 */
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}
182
183/**
184 * Default finalizer method.
185 *
186 * This is used, if section does not have its own method defined.
187 *
188 * @param stream Stream that contains binary to finalize.
189 * @param section Section to finalize.
190 */
191void
195
196}
#define assert(condition)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
Word sectionCount() const
Section * section(Word index) const
static void finalizeBinary(BinaryStream &stream, const Binary *binaryToFinalize, const BinaryWriter *writer)
static void writeHeader(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
static void writeData(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
virtual const BinaryWriter & parent() const =0
Gets parent of instance which for SectionWriter is created.
virtual Section::SectionType type() const =0
Returns SectionType that actual reader or writer instance can handle.
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const =0
Does actual writing of sections data.
virtual void finalize(BinaryStream &stream, Section *section) const
std::pair< const Section::SectionType, const BinaryWriter * > MapKey
Key type for finding values in map of section writers.
static SectionId getUniqueSectionId()
virtual void actualWriteHeader(BinaryStream &stream, const Section *sect) const =0
Does actual writing of sections header.
std::map< MapKey, const SectionWriter * > MapType
Map type that contains instances of registered section writers.
static void registerSectionWriter(const SectionWriter *sWriter)
static MapType * prototypes_
Contains section writers for all kinds of sections and all kinds of binrary fromats that are supporte...
static const SectionWriter * findSectionWriter(const Section::SectionType type, const BinaryWriter *bWriter)
virtual SectionType type() const =0
Returns SectioType of actual section instance.
HalfWord SectionId
Type for storing binary file section ids.