Path: blob/master/src/hotspot/share/jfr/utilities/jfrIterator.hpp
41149 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_JFR_UTILITIES_JFRITERATOR_HPP25#define SHARE_JFR_UTILITIES_JFRITERATOR_HPP2627#include "memory/allocation.hpp"2829template <typename List>30class StopOnNullCondition {31typedef typename List::Node Node;32private:33List& _list;34mutable Node* _node;35public:36StopOnNullCondition(List& list) : _list(list), _node(list.head()) {}37bool has_next() const {38return _node != NULL;39}40Node* next() const {41assert(_node != NULL, "invariant");42Node* temp = _node;43_node = (Node*)_node->_next;44return temp;45}46};4748template <typename List>49class StopOnNullConditionRemoval {50typedef typename List::Node Node;51private:52List& _list;53mutable Node* _node;54public:55StopOnNullConditionRemoval(List& list) : _list(list), _node(NULL) {}56bool has_next() const {57_node = _list.remove();58return _node != NULL;59}60Node* next() const {61assert(_node != NULL, "invariant");62return _node;63}64};6566template <typename List, template <typename> class ContinuationPredicate>67class Navigator {68public:69typedef typename List::Node Node;70Navigator(List& list) : _continuation(list) {}71bool has_next() const {72return _continuation.has_next();73}74Node* next() const {75return _continuation.next();76}77private:78ContinuationPredicate<List> _continuation;79mutable Node* _node;80};8182template <typename List>83class NavigatorStopOnNull : public Navigator<List, StopOnNullCondition> {84public:85NavigatorStopOnNull(List& list) : Navigator<List, StopOnNullCondition>(list) {}86};8788template <typename List>89class NavigatorStopOnNullRemoval : public Navigator<List, StopOnNullConditionRemoval> {90public:91NavigatorStopOnNullRemoval(List& list) : Navigator<List, StopOnNullConditionRemoval>(list) {}92};9394template<typename List, template <typename> class Navigator, typename AP = StackObj>95class IteratorHost : public AP {96private:97Navigator<List> _navigator;98public:99typedef typename List::NodePtr NodePtr;100IteratorHost(List& list) : AP(), _navigator(list) {}101void reset() { _navigator.reset(); }102bool has_next() const { return _navigator.has_next(); }103NodePtr next() const { return _navigator.next(); }104};105106template<typename List, typename AP = StackObj>107class StopOnNullIterator : public IteratorHost<List, NavigatorStopOnNull, AP> {108public:109StopOnNullIterator(List& list) : IteratorHost<List, NavigatorStopOnNull, AP>(list) {}110};111112template<typename List, typename AP = StackObj>113class StopOnNullIteratorRemoval : public IteratorHost<List, NavigatorStopOnNullRemoval, AP> {114public:115StopOnNullIteratorRemoval(List& list) : IteratorHost<List, NavigatorStopOnNullRemoval, AP>(list) {}116};117118#endif // SHARE_JFR_UTILITIES_JFRITERATOR_HPP119120121