1 /*
2  * Copyright 2011 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SFNTLY_CPP_SRC_SFNTLY_PORT_JAVA_ITERATOR_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_PORT_JAVA_ITERATOR_H_
19 
20 #include "sfntly/port/refcount.h"
21 
22 // Interface of Java iterator.
23 // This is a forward read-only iterator that represents java.util.Iterator<E>
24 
25 namespace sfntly {
26 
27 template <typename ReturnType, typename ContainerBase>
28 class Iterator : public virtual RefCount {
29  public:
~Iterator()30   virtual ~Iterator() {}
31   virtual ContainerBase* container_base() = 0;
32 
33  protected:
Iterator()34   Iterator() {}
35   NO_COPY_AND_ASSIGN(Iterator);
36 };
37 
38 template <typename ReturnType, typename Container,
39           typename ContainerBase = Container>
40 class PODIterator : public Iterator<ReturnType, ContainerBase>,
41                     public RefCounted< PODIterator<ReturnType, Container> > {
42  public:
PODIterator(Container * container)43   explicit PODIterator(Container* container) : container_(container) {}
~PODIterator()44   virtual ~PODIterator() {}
container_base()45   virtual ContainerBase* container_base() {
46     return static_cast<ContainerBase*>(container_);
47   }
48 
49   virtual bool HasNext() = 0;
50   virtual ReturnType Next() = 0;
Remove()51   virtual void Remove() {
52 #if !defined (SFNTLY_NO_EXCEPTION)
53     // Default to no support.
54     throw UnsupportedOperationException();
55 #endif
56   }
57 
58  protected:
container()59   Container* container() { return container_; }
60 
61  private:
62   Container* container_;  // Dumb pointer is used to avoid circular ref-counting
63 };
64 
65 template <typename ReturnType, typename Container,
66           typename ContainerBase = Container>
67 class RefIterator : public Iterator<ReturnType, ContainerBase>,
68                     public RefCounted< RefIterator<ReturnType, Container> > {
69  public:
RefIterator(Container * container)70   explicit RefIterator(Container* container) : container_(container) {}
~RefIterator()71   virtual ~RefIterator() {}
container_base()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;
Remove()78   virtual void Remove() {
79 #if !defined (SFNTLY_NO_EXCEPTION)
80     // Default to no support.
81     throw UnsupportedOperationException();
82 #endif
83   }
84 
85  protected:
container()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