AERA
out_stream.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Copyright (c) 2010 Nathaniel Thurston
13 //_/_/ Center for Analysis and Design of Intelligent Agents
14 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
15 //_/_/ http://cadia.ru.is
16 //_/_/
17 //_/_/ Part of this software was developed by Eric Nivel
18 //_/_/ in the HUMANOBS EU research project, which included
19 //_/_/ the following parties:
20 //_/_/
21 //_/_/ Autonomous Systems Laboratory
22 //_/_/ Technical University of Madrid, Spain
23 //_/_/ http://www.aslab.org/
24 //_/_/
25 //_/_/ Communicative Machines
26 //_/_/ Edinburgh, United Kingdom
27 //_/_/ http://www.cmlabs.com/
28 //_/_/
29 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
30 //_/_/ University of Lugano and SUPSI, Switzerland
31 //_/_/ http://www.idsia.ch/
32 //_/_/
33 //_/_/ Institute of Cognitive Sciences and Technologies
34 //_/_/ Consiglio Nazionale delle Ricerche, Italy
35 //_/_/ http://www.istc.cnr.it/
36 //_/_/
37 //_/_/ Dipartimento di Ingegneria Informatica
38 //_/_/ University of Palermo, Italy
39 //_/_/ http://diid.unipa.it/roboticslab/
40 //_/_/
41 //_/_/
42 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
43 //_/_/
44 //_/_/ Redistribution and use in source and binary forms, with or without
45 //_/_/ modification, is permitted provided that the following conditions
46 //_/_/ are met:
47 //_/_/ - Redistributions of source code must retain the above copyright
48 //_/_/ and collaboration notice, this list of conditions and the
49 //_/_/ following disclaimer.
50 //_/_/ - Redistributions in binary form must reproduce the above copyright
51 //_/_/ notice, this list of conditions and the following disclaimer
52 //_/_/ in the documentation and/or other materials provided with
53 //_/_/ the distribution.
54 //_/_/
55 //_/_/ - Neither the name of its copyright holders nor the names of its
56 //_/_/ contributors may be used to endorse or promote products
57 //_/_/ derived from this software without specific prior
58 //_/_/ written permission.
59 //_/_/
60 //_/_/ - CADIA Clause: The license granted in and to the software
61 //_/_/ under this agreement is a limited-use license.
62 //_/_/ The software may not be used in furtherance of:
63 //_/_/ (i) intentionally causing bodily injury or severe emotional
64 //_/_/ distress to any person;
65 //_/_/ (ii) invading the personal privacy or violating the human
66 //_/_/ rights of any person; or
67 //_/_/ (iii) committing or preparing for any act of war.
68 //_/_/
69 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
70 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
71 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
72 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
73 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
76 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
78 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
80 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
82 //_/_/ OF SUCH DAMAGE.
83 //_/_/
84 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
85 
86 #ifndef out_stream_h
87 #define out_stream_h
88 
89 #include <iostream>
90 
91 #include "../r_code/resized_vector.h"
92 
93 namespace r_comp {
94 
95 // Allows inserting data at a specified index and right shifting the current content;
96 // ex: labels and variables, i.e. when iptrs are discovered and these hold indexes are < read_index and do not point to variables
97 class OutStream {
98 public:
99  r_code::resized_vector<uint16> code_indexes_to_stream_indexes_;
101  OutStream(std::ostringstream *s) : stream_(s) {}
102  std::ostringstream *stream_;
103  template<typename T> OutStream &push(const T &t, uint16 code_index) {
104  positions_.push_back(stream_->tellp());
105  code_indexes_to_stream_indexes_[code_index] = positions_.size() - 1;
106  return *this << t;
107  }
108  OutStream &push() { // to keep adding entries in v without outputing anything (e.g. for wildcards after ::)
109  std::streampos p;
110  positions_.push_back(p);
111  return *this;
112  }
113  template<typename T> OutStream &operator <<(const T &t) {
114  *stream_ << t;
115  return *this;
116  }
117  template<typename T> OutStream &insert(uint32 index, const T &t) { // inserts before code_indexes_to_stream_indexes_[index]
118  uint16 stream_index = code_indexes_to_stream_indexes_[index];
119  stream_->seekp(positions_[stream_index]);
120  std::string s = stream_->str().substr(positions_[stream_index]);
121  *stream_ << t;
122  std::streamoff offset = stream_->tellp() - positions_[stream_index];
123  *stream_ << s;
124  for (uint16 i = stream_index + 1; i < positions_.size(); ++i) // right-shift
125  positions_[i] += offset;
126  return *this;
127  }
128 };
129 
130 class NoStream :
131  public std::ostream {
132 public:
133  NoStream() : std::ostream(NULL) {}
134  template<typename T> NoStream& operator <<(T &t) {
135  return *this;
136  }
137 };
138 
140  public std::ostream {
141 public:
142  CompilerOutput() : std::ostream(NULL) {}
143  template<typename T> std::ostream& operator <<(T &t) {
144  if (1) // njt: was if (Output); HACK for linux compatibility
145  return std::cout << t;
146  return *this;
147  }
148 };
149 }
150 
151 
152 #endif
r_code::resized_vector< uint16 >
r_comp::CompilerOutput
Definition: out_stream.h:140
r_comp::NoStream
Definition: out_stream.h:131
r_comp::OutStream
Definition: out_stream.h:97