LOOS  v2.3.2
KernelStack.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 #include <KernelStack.hpp>
24 
25 
26 namespace loos {
27  namespace internal {
28 
29  void ValueStack::requireNotEmpty(void) const {
30  if (values.size() == 0)
31  throw(LOOSError("Operation requested on an empty stack."));
32  }
33 
34  void ValueStack::push(const Value& val) { values.push_back(val); };
35 
36  Value ValueStack::pop(void) {
37  requireNotEmpty();
38  Value val = values.back();
39  values.pop_back();
40  return(val);
41  }
42 
43  // Duplicate the top entry...
44  void ValueStack::dup(void) {
45  requireNotEmpty();
46  Value val = values.back();
47  push(val);
48  }
49 
50  // Just drop the top entry, i.e. (void)pop()
51  void ValueStack::drop(void) {
52  requireNotEmpty();
53  values.pop_back();
54  }
55 
56  // Peek at the top value without popping it...
57  Value ValueStack::peek(int i) {
58  if (i < 0)
59  i += values.size();
60  if ((unsigned int)i >= values.size())
61  throw(LOOSError("Peeking beyond the stack!"));
62 
63  return(values[i]);
64  }
65 
66  unsigned int ValueStack::size(void) const { return(values.size()); }
67 
68  void ValueStack::clear(void) { values.clear(); }
69 
70  std::ostream& operator<<(std::ostream& os, const ValueStack& s) {
71  os << "<STACK>\n";
72  std::vector<Value>::const_iterator i;
73 
74  for (i=s.values.begin(); i != s.values.end(); i++)
75  os << " " << *i << std::endl;
76 
77  return(os);
78  }
79 
80  }
81 }
82 
Namespace for most things not already encapsulated within a class.