replicode
out_stream.h
Go to the documentation of this file.
1 // out_stream.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef out_stream_h
32 #define out_stream_h
33 
34 #include <iostream>
35 
36 #include "../r_code/vector.h"
37 
38 
39 using namespace r_code;
40 
41 namespace r_comp {
42 
43 // Allows inserting data at a specified index and right shifting the current content;
44 // ex: labels and variables, i.e. when iptrs are discovered and these hold indexes are < read_index and do not point to variables
45 class OutStream {
46 public:
48  uint16_t code_index;
50  OutStream(std::ostringstream *s): stream(s) {}
51  std::ostringstream *stream;
52  template<typename T> OutStream &push(const T &t, uint16_t code_index) {
53  positions.push_back(stream->tellp());
54  code_indexes_to_stream_indexes[code_index] = positions.size() - 1;
55  return *this << t;
56  }
57  OutStream &push() { // to keep adding entries in v without outputing anything (e.g. for wildcards after ::)
58  std::streampos p;
59  positions.push_back(p);
60  return *this;
61  }
62  template<typename T> OutStream &operator <<(const T &t) {
63  *stream << t;
64  return *this;
65  }
66  template<typename T> OutStream &insert(uint64_t index, const T &t) { // inserts before code_indexes_to_stream_indexes[index]
67  uint16_t stream_index = code_indexes_to_stream_indexes[index];
68  stream->seekp(positions[stream_index]);
69  std::string s = stream->str().substr(positions[stream_index]);
70  *stream << t;
71  std::streamoff offset = stream->tellp() - positions[stream_index];
72  *stream << s;
73  for (uint16_t i = stream_index + 1; i < positions.size(); ++i) // right-shift
74  positions[i] += offset;
75  return *this;
76  }
77 };
78 
79 class NoStream:
80  public std::ostream {
81 public:
82  NoStream(): std::ostream(NULL) {}
83  template<typename T> NoStream& operator <<(T &t) {
84  return *this;
85  }
86 };
87 
89  public std::ostream {
90 public:
91  CompilerOutput(): std::ostream(NULL) {}
92  template<typename T> std::ostream& operator <<(T &t) {
93  if (1) // njt: was if (Output); HACK for linux compatibility
94  return std::cout << t;
95  return *this;
96  }
97 };
98 }
99 
100 
101 #endif
OutStream & insert(uint64_t index, const T &t)
Definition: out_stream.h:66
CompilerOutput()
Definition: out_stream.h:91
r_code::vector< std::streampos > positions
Definition: out_stream.h:49
std::ostream & operator<<(std::ostream &os, RepliStruct *structure)
Definition: replistruct.cpp:718
NoStream()
Definition: out_stream.h:82
Definition: atom.cpp:36
uint16_t code_index
Definition: out_stream.h:48
r_code::vector< uint16_t > code_indexes_to_stream_indexes
Definition: out_stream.h:47
OutStream & push()
Definition: out_stream.h:57
std::ostringstream * stream
Definition: out_stream.h:51
Definition: out_stream.h:79
OutStream(std::ostringstream *s)
Definition: out_stream.h:50
size_t size() const
Definition: vector.h:44
void push_back(T t)
Definition: vector.h:55
Definition: out_stream.h:88
Definition: image.h:41
OutStream & push(const T &t, uint16_t code_index)
Definition: out_stream.h:52
Definition: out_stream.h:45