OpenASIP  2.0
SectionReader.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 SectionReader.cc
26  *
27  * Definition of SectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include "SectionReader.hh"
35 
36 #include <map>
37 #include <utility>
38 
39 #include "BinaryStream.hh"
40 #include "Section.hh"
41 #include "Binary.hh"
42 #include "ContainerTools.hh"
43 #include "AssocTools.hh"
44 #include "ReferenceKey.hh"
45 #include "TPEFBaseType.hh"
46 
47 namespace TPEF {
48 
49 using std::map;
50 using std::pair;
51 
53 
54 /**
55  * Constructor
56  */
58 }
59 
60 /**
61  * Destructor
62  */
64 }
65 
66 /**
67  * Reads a section from BinaryStream.
68  *
69  * Finds correct concrete SectionReader object for reading section and
70  * uses it for reading.
71  *
72  * @param stream Stream from which section's data is read.
73  * @param section Section instance where section elements are stored.
74  * @param reader BinaryReader* of file type that we try to read.
75  * @exception InstanceNotFound There is no prototype registred for reading.
76  * @exception UnreachableStream If reading of section fails.
77  * @exception KeyAlreadyExists Key was in use when trying to register object.
78  * @exception EndOfFile If end of file were reached while it shouldn't.
79  * @exception OutOfRange Some of read values were out of range.
80  * @exception WrongSubclass Some class couldn't do what it was asked for.
81  * @exception UnexpectedValue If there was unexpected value when reading.
82  */
83 void
85  BinaryStream& stream, Section* section, BinaryReader* reader) {
86  const SectionReader* sectionReader =
87  findSectionReader(section->type(), reader);
88 
89  sectionReader->readData(stream, section);
90 }
91 
92 /**
93  * Finalizes binary whose all data is read and resolved.
94  *
95  * @param bin Binary to resolve.
96  * @param reader Reader which was used for reading.
97  */
98 void
100 
101  for (Word i = 0; i < bin->sectionCount(); i++) {
102  // get section and finalize it
103  Section* sect = bin->section(i);
104 
105  try {
106  const SectionReader* sectionReader =
107  findSectionReader(sect->type(), reader);
108  sectionReader->finalize(sect);
109 
110  } catch (const InstanceNotFound &e) {
111  // there is not always reader for every created section
112  // and it's ok.
113  }
114  }
115 }
116 
117 /**
118  * Finds SectionReader instance by SectionType and BinaryReader*.
119  *
120  * @param type Type of section to find.
121  * @param reader BinaryReader which requested finding section.
122  * @return Instance which can read section.
123  * @exception InstanceNotFound Reader instance was not found.
124 */
125 const SectionReader*
127  const Section::SectionType type, const BinaryReader* reader) {
128  MapKey key(type, reader);
129 
130  if (prototypes_ == NULL ||
132  throw InstanceNotFound(__FILE__, __LINE__,
133  "SectionReader::findSectionReader");
134  }
135 
136  return (*prototypes_)[key];
137 }
138 
139 /**
140  * Registers SectionReader instance for reading specific section type.
141  *
142  * @param reader Instance to register for reading.
143  */
144 void
146 
147  MapKey key(reader->type(), reader->parent());
148 
149  // We can't define a static prototypes_ map, because we cannot guarantee
150  // that it is initialized before this method is called.
151  if (prototypes_ == NULL) {
152  prototypes_ = new MapType();
153  }
154 
156 
157  (*prototypes_)[key] = reader;
158 }
159 
160 /**
161  * Default finalizer method all sections.
162  *
163  * Does nothing. This method is runned for every read section after body of
164  * every section is read and references are resolved once.
165  *
166  * @param section Section to finalize.
167  */
168 void
169 SectionReader::finalize(Section* /*section*/) const {
170 }
171 
172 }
TPEF::SectionReader::SectionReader
SectionReader()
Definition: SectionReader.cc:57
TPEF::SectionReader::prototypes_
static MapType * prototypes_
Contains section readers for all kinds of sections and all kinds of binary formats that are supported...
Definition: SectionReader.hh:101
TPEF::SectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const =0
Does actual reading part for constructing section.
TPEF::Binary
Definition: Binary.hh:49
TPEF::Section::type
virtual SectionType type() const =0
Returns SectioType of actual section instance.
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::SectionReader::finalizeBinary
static void finalizeBinary(Binary *binaryToFinalize, BinaryReader *reader)
Definition: SectionReader.cc:99
TPEF::Binary::section
Section * section(Word index) const
TPEF::Binary::sectionCount
Word sectionCount() const
TPEF::BinaryReader
Definition: BinaryReader.hh:51
TPEF::Section
Definition: Section.hh:64
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::SectionReader::finalize
virtual void finalize(Section *section) const
Definition: SectionReader.cc:169
TPEF::SectionReader::parent
virtual BinaryReader * parent() const =0
Returns binary reader whose sections actual section reader reads.
SectionReader.hh
TPEF::SectionReader
Definition: SectionReader.hh:56
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::SectionReader::type
virtual Section::SectionType type() const =0
Returns type (TPEF) section which actual section reader reads.
TPEF::SectionReader::MapKey
std::pair< const Section::SectionType, const BinaryReader * > MapKey
Key type for finding values in map of section readers.
Definition: SectionReader.hh:94
TPEF::SectionReader::findSectionReader
static const SectionReader * findSectionReader(const Section::SectionType type, const BinaryReader *bReader)
Definition: SectionReader.cc:126
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
AssocTools.hh
Section.hh
TPEF::SectionReader::MapType
std::map< MapKey, const SectionReader * > MapType
Map type that contains instances of registered section readers.
Definition: SectionReader.hh:97
TPEF::SectionReader::~SectionReader
virtual ~SectionReader()
Definition: SectionReader.cc:63
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEFBaseType.hh
ReferenceKey.hh
InstanceNotFound
Definition: Exception.hh:304
TPEF
Definition: Assembler.hh:43
Binary.hh
TPEF::SectionReader::readSection
static void readSection(BinaryStream &stream, Section *section, BinaryReader *reader)
Definition: SectionReader.cc:84
ContainerTools.hh