1cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com/*
2cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * Copyright 2011 Google Inc. All Rights Reserved.
3cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com *
4cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * you may not use this file except in compliance with the License.
6cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * You may obtain a copy of the License at
7cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com *
8cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com *      http://www.apache.org/licenses/LICENSE-2.0
9cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com *
108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com * Unless required by applicable law or agreed to in writing, software
118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com * distributed under the License is distributed on an "AS IS" BASIS,
128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com * See the License for the specific language governing permissions and
148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com * limitations under the License.
158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com */
168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#ifndef SFNTLY_CPP_SRC_SFNTLY_PORT_JAVA_ITERATOR_H_
188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#define SFNTLY_CPP_SRC_SFNTLY_PORT_JAVA_ITERATOR_H_
198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include "sfntly/port/refcount.h"
218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// Interface of Java iterator.
238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// This is a forward read-only iterator that represents java.util.Iterator<E>
248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace sfntly {
268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comtemplate <typename ReturnType, typename ContainerBase>
288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comclass Iterator : public virtual RefCount {
298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com public:
308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual ~Iterator() {}
318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  virtual ContainerBase* container_base() = 0;
32cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
33 protected:
34  Iterator() {}
35  NO_COPY_AND_ASSIGN(Iterator);
36};
37
38template <typename ReturnType, typename Container,
39          typename ContainerBase = Container>
40class PODIterator : public Iterator<ReturnType, ContainerBase>,
41                    public RefCounted< PODIterator<ReturnType, Container> > {
42 public:
43  explicit PODIterator(Container* container) : container_(container) {}
44  virtual ~PODIterator() {}
45  virtual ContainerBase* container_base() {
46    return static_cast<ContainerBase*>(container_);
47  }
48
49  virtual bool HasNext() = 0;
50  virtual ReturnType Next() = 0;
51  virtual void Remove() {
52#if !defined (SFNTLY_NO_EXCEPTION)
53    // Default to no support.
54    throw UnsupportedOperationException();
55#endif
56  }
57
58 protected:
59  Container* container() { return container_; }
60
61 private:
62  Container* container_;  // Dumb pointer is used to avoid circular ref-counting
63};
64
65template <typename ReturnType, typename Container,
66          typename ContainerBase = Container>
67class RefIterator : public Iterator<ReturnType, ContainerBase>,
68                    public RefCounted< RefIterator<ReturnType, Container> > {
69 public:
70  explicit RefIterator(Container* container) : container_(container) {}
71  virtual ~RefIterator() {}
72  virtual ContainerBase* container_base() {
73    return static_cast<ContainerBase*>(container_);
74  }
75
76  virtual bool HasNext() = 0;
77  CALLER_ATTACH virtual ReturnType* Next() = 0;
78  virtual void Remove() {
79#if !defined (SFNTLY_NO_EXCEPTION)
80    // Default to no support.
81    throw UnsupportedOperationException();
82#endif
83  }
84
85 protected:
86  Container* container() { return container_; }
87
88 private:
89  Container* container_;  // Dumb pointer is used to avoid circular ref-counting
90};
91
92}  // namespace sfntly
93
94#endif  // SFNTLY_CPP_SRC_SFNTLY_PORT_JAVA_ITERATOR_H_
95