Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
safestack.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/scripting/safestack.hpp
20 \brief stack with safety checks and pop() that returns rvalue reference of top element
21 \ingroup utilities
22*/
23
24#pragma once
25
26#include <ql/errors.hpp>
27
28#include <stack>
29
30namespace ore {
31namespace data {
32
33template <typename T> class SafeStack {
34public:
35 const T& top() const { return stack_.top(); }
36 T& top() {
37 QL_REQUIRE(!stack_.empty(), "SafeStack::top(): empty stack");
38 return stack_.top();
39 }
40 T pop() {
41 T v(std::move(top()));
42 stack_.pop();
43 return v;
44 }
45 bool empty() const { return stack_.empty(); }
46 typename std::stack<T>::size_type size() const { return stack_.size(); }
47 void push(const T& t) { stack_.push(t); }
48 void push(T&& t) { stack_.push(std::forward<T>(t)); }
49 template <typename... Args> void emplace(Args&&... t) { stack_.emplace(std::forward<Args>(t)...); }
50 void swap(const SafeStack<T>& other) { stack_.swap(other.stack_); }
51
52private:
53 std::stack<T> stack_;
54};
55
56} // namespace data
57} // namespace ore
void emplace(Args &&... t)
Definition: safestack.hpp:49
std::stack< T > stack_
Definition: safestack.hpp:53
bool empty() const
Definition: safestack.hpp:45
void push(const T &t)
Definition: safestack.hpp:47
std::stack< T >::size_type size() const
Definition: safestack.hpp:46
void swap(const SafeStack< T > &other)
Definition: safestack.hpp:50
const T & top() const
Definition: safestack.hpp:35
void push(T &&t)
Definition: safestack.hpp:48
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23