• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CORE_FPDFAPI_PARSER_CPDF_OBJECT_WALKER_H_
6 #define CORE_FPDFAPI_PARSER_CPDF_OBJECT_WALKER_H_
7 
8 #include <memory>
9 #include <stack>
10 
11 #include "core/fpdfapi/parser/cpdf_dictionary.h"
12 
13 // Walk on all non-null sub-objects in an object in depth, include itself,
14 // like in flat list.
15 class CPDF_ObjectWalker {
16  public:
17   class SubobjectIterator {
18    public:
19     virtual ~SubobjectIterator();
IsStarted()20     bool IsStarted() const { return is_started_; }
21     bool virtual IsFinished() const = 0;
22     const CPDF_Object* Increment();
object()23     const CPDF_Object* object() const { return object_; }
24 
25    protected:
26     explicit SubobjectIterator(const CPDF_Object* object);
27 
28     virtual const CPDF_Object* IncrementImpl() = 0;
29     virtual void Start() = 0;
30 
31    private:
32     const CPDF_Object* object_;
33     bool is_started_ = false;
34   };
35 
36   explicit CPDF_ObjectWalker(const CPDF_Object* root);
37   ~CPDF_ObjectWalker();
38 
39   const CPDF_Object* GetNext();
40   void SkipWalkIntoCurrentObject();
41 
current_depth()42   size_t current_depth() const { return current_depth_; }
GetParent()43   const CPDF_Object* GetParent() const { return parent_object_; }
dictionary_key()44   const ByteString& dictionary_key() const { return dict_key_; }
45 
46  private:
47   static std::unique_ptr<SubobjectIterator> MakeIterator(
48       const CPDF_Object* object);
49 
50   const CPDF_Object* next_object_;
51   const CPDF_Object* parent_object_;
52 
53   ByteString dict_key_;
54   size_t current_depth_;
55 
56   std::stack<std::unique_ptr<SubobjectIterator>> stack_;
57 };
58 
59 class CPDF_NonConstObjectWalker : public CPDF_ObjectWalker {
60  public:
CPDF_NonConstObjectWalker(CPDF_Object * root)61   explicit CPDF_NonConstObjectWalker(CPDF_Object* root)
62       : CPDF_ObjectWalker(root) {}
63 
GetNext()64   CPDF_Object* GetNext() {
65     return const_cast<CPDF_Object*>(CPDF_ObjectWalker::GetNext());
66   }
67 };
68 
69 #endif  // CORE_FPDFAPI_PARSER_CPDF_OBJECT_WALKER_H_
70