OpenASIP 2.2
Loading...
Searching...
No Matches
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
43namespace TPEF {
44
45std::set<ValueReplacer*> ValueReplacer::replacements_;
46BinaryStream* 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 const& 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 */
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 */
89void
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 */
104void
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 */
126void
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 */
149void
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 */
169const SafePointable*
171 return reference_;
172}
173
174/**
175 * Stream position where to write reference.
176 *
177 * @return Position where to write reference.
178 */
179unsigned int
183
184}
#define __func__
#define assert(condition)
unsigned int writePosition()
ValueReplacer(const SafePointable *obj)
static BinaryStream & stream()
static void finalize()
static void initialize(BinaryStream &stream)
static std::set< ValueReplacer * > replacements_
Replacements which should be done afterwards.
static void addReplacement(ValueReplacer *obj)
virtual ValueReplacer * clone()=0
Creates dynamically allocated clone of object.
unsigned int streamPosition_
File offset where replacement is done.
virtual bool tryToReplace()=0
Does replacement if can. If can't returns false.
static BinaryStream * stream_
Stream for writing replacements.
unsigned int streamPosition() const
const SafePointable * reference() const
const SafePointable * reference_
Reference which to be written.