OpenASIP  2.0
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 
37 namespace TPEF {
38 
40 
41 /**
42  * Constructor
43  */
45 }
46 
47 /**
48  * Destructor
49  */
51 }
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  */
61 void
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  */
78 void
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  */
98 void
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  */
128 const 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  */
147 void
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  */
171 SectionId
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  */
191 void
193  BinaryStream&, Section*) const {
194 }
195 
196 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::SectionWriter::writeData
static void writeData(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
Definition: SectionWriter.cc:79
TPEF::SectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const =0
Does actual writing of sections data.
SectionWriter.hh
TPEF::Binary
Definition: Binary.hh:49
TPEF::SectionWriter::finalize
virtual void finalize(BinaryStream &stream, Section *section) const
Definition: SectionWriter.cc:192
TPEF::SectionWriter::type
virtual Section::SectionType type() const =0
Returns SectionType that actual reader or writer instance can handle.
TPEF::Section::type
virtual SectionType type() const =0
Returns SectioType of actual section instance.
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::SectionWriter::~SectionWriter
virtual ~SectionWriter()
Definition: SectionWriter.cc:50
TPEF::Binary::section
Section * section(Word index) const
TPEF::Binary::sectionCount
Word sectionCount() const
TPEF::Section
Definition: Section.hh:64
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::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::SectionWriter::writeHeader
static void writeHeader(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
Definition: SectionWriter.cc:62
TPEF::SectionWriter::findSectionWriter
static const SectionWriter * findSectionWriter(const Section::SectionType type, const BinaryWriter *bWriter)
Definition: SectionWriter.cc:129
BinaryWriter.hh
TPEF::SectionWriter
Definition: SectionWriter.hh:58
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::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::SectionWriter::parent
virtual const BinaryWriter & parent() const =0
Gets parent of instance which for SectionWriter is created.
TPEF::SectionWriter::finalizeBinary
static void finalizeBinary(BinaryStream &stream, const Binary *binaryToFinalize, const BinaryWriter *writer)
Definition: SectionWriter.cc:99
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::getUniqueSectionId
static SectionId getUniqueSectionId()
Definition: SectionWriter.cc:172
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
TPEF::BinaryWriter
Definition: BinaryWriter.hh:50
TPEF
Definition: Assembler.hh:43