OpenASIP  2.0
ValueReplacer.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 ValueReplacer.cc
26  *
27  * Definition of ValueReplacer class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include <set>
35 #include <iterator>
36 
37 #include "ValueReplacer.hh"
38 #include "BinaryStream.hh"
39 #include "SafePointer.hh"
40 #include "SafePointable.hh"
41 #include "Exception.hh"
42 
43 namespace TPEF {
44 
45 std::set<ValueReplacer*> ValueReplacer::replacements_;
46 BinaryStream* ValueReplacer::stream_ = NULL;
47 
48 /**
49  * Constructor
50  *
51  * @param obj Object whole key is used for replacing bytes in stream.
52  */
54  reference_(obj) {
55 
56  assert(stream_ != NULL);
57  assert(obj != NULL);
58 
59  try {
61  } catch (UnreachableStream e) {
62  bool replacerConstructorBadStream = false;
63  assert(replacerConstructorBadStream);
64  }
65 }
66 
67 /**
68  * Copy constructor
69  *
70  * @para replacer Replacer which is copied.
71  */
73  streamPosition_(replacer.streamPosition_),
74  reference_(replacer.reference_) {
75 }
76 
77 /**
78  * Destructor
79  */
81 }
82 
83 /**
84  * Does replacement or set it to be done later.
85  *
86  * @exception UnreachableStream Writing can't be done, because of bad stream.
87  * @exception WritePastEOF If replacer tried to write past end of file.
88  */
89 void
91  if(!tryToReplace()) {
93  }
94 }
95 
96 /**
97  * Initializes ValueReplacer for writing.
98  *
99  * Sets stream which we want write to and clears up key table of reference
100  * manager so there won't be any keys with wrong values.
101  *
102  * @param stream Stream where replacements are done.
103  */
104 void
106  stream_ = &stream;
107  assert(stream_ != NULL);
108  replacements_.clear();
109 }
110 
111 /**
112  * Tries to do those replacement which couldn't do while writing.
113  *
114  * Deletes all remaining replacer instances after replacements were done.
115  * When method is runned and no exceptions are thrown method cleans up
116  * key tables of reference manager and unset's BinaryStream where
117  * replacements were written. So initilize() method must be called again
118  * before starting new writing. If MissingKeys exception is thrown,
119  * then those replacements are not freed, which could not be done before.
120  * Keys can be added and finalize may be called again.
121  *
122  * @exception MissingKeys If ReferenceManager doesn't contain needed keys.
123  * @exception UnreachableStream Writing can't be done, because of bad stream.
124  * @exception WritePastEOF If replacer tried to write past end of file.
125  */
126 void
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 }
143 
144 /**
145  * Adds a replacement object for later writing.
146  *
147  * @param replacer Replacement that should be done later.
148  */
149 void
151  replacements_.insert(replacements_.end(),replacer);
152 }
153 
154 /**
155  * Returns stream where to write.
156  *
157  * @return Stream where to write reference.
158  */
161  return *stream_;
162 }
163 
164 /**
165  * Returns referenced object.
166  *
167  * @return Referenced object.
168  */
169 const SafePointable*
171  return reference_;
172 }
173 
174 /**
175  * Stream position where to write reference.
176  *
177  * @return Position where to write reference.
178  */
179 unsigned int
181  return streamPosition_;
182 }
183 
184 }
MissingKeys
Definition: Exception.hh:405
ValueReplacer.hh
TPEF::ValueReplacer::stream_
static BinaryStream * stream_
Stream for writing replacements.
Definition: ValueReplacer.hh:104
UnreachableStream
Definition: Exception.hh:171
TPEF::ValueReplacer::addReplacement
static void addReplacement(ValueReplacer *obj)
Definition: ValueReplacer.cc:150
Exception.hh
TPEF::ValueReplacer::initialize
static void initialize(BinaryStream &stream)
Definition: ValueReplacer.cc:105
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
SafePointer.hh
TPEF::ValueReplacer::~ValueReplacer
virtual ~ValueReplacer()
Definition: ValueReplacer.cc:80
TPEF::ValueReplacer::clone
virtual ValueReplacer * clone()=0
Creates dynamically allocated clone of object.
TPEF::ValueReplacer::resolve
void resolve()
Definition: ValueReplacer.cc:90
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::ValueReplacer::stream
static BinaryStream & stream()
Definition: ValueReplacer.cc:160
__func__
#define __func__
Definition: Application.hh:67
TPEF::ValueReplacer::streamPosition_
unsigned int streamPosition_
File offset where replacement is done.
Definition: ValueReplacer.hh:100
TPEF::SafePointable
Definition: SafePointable.hh:48
TPEF::ValueReplacer::streamPosition
unsigned int streamPosition() const
Definition: ValueReplacer.cc:180
TPEF::ValueReplacer::tryToReplace
virtual bool tryToReplace()=0
Does replacement if can. If can't returns false.
BinaryStream.hh
SafePointable.hh
TPEF::ValueReplacer::reference_
const SafePointable * reference_
Reference which to be written.
Definition: ValueReplacer.hh:102
TPEF::ValueReplacer::finalize
static void finalize()
Definition: ValueReplacer.cc:127
TPEF::ValueReplacer::ValueReplacer
ValueReplacer(const SafePointable *obj)
Definition: ValueReplacer.cc:53
TPEF::ValueReplacer
Definition: ValueReplacer.hh:71
TPEF
Definition: Assembler.hh:43
TPEF::ValueReplacer::reference
const SafePointable * reference() const
Definition: ValueReplacer.cc:170
TPEF::ValueReplacer::replacements_
static std::set< ValueReplacer * > replacements_
Replacements which should be done afterwards.
Definition: ValueReplacer.hh:107