• 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/fxcrt/fx_string.h"
12 #include "core/fxcrt/retain_ptr.h"
13 
14 class CPDF_Object;
15 
16 // Walk on all non-null sub-objects in an object in depth, include itself,
17 // like in flat list.
18 class CPDF_ObjectWalker {
19  public:
20   class SubobjectIterator {
21    public:
22     virtual ~SubobjectIterator();
23     virtual bool IsFinished() const = 0;
IsStarted()24     bool IsStarted() const { return is_started_; }
25     const CPDF_Object* Increment();
object()26     const CPDF_Object* object() const { return object_.Get(); }
27 
28    protected:
29     explicit SubobjectIterator(const CPDF_Object* object);
30 
31     virtual const CPDF_Object* IncrementImpl() = 0;
32     virtual void Start() = 0;
33 
34    private:
35     RetainPtr<const CPDF_Object> object_;
36     bool is_started_ = false;
37   };
38 
39   explicit CPDF_ObjectWalker(const CPDF_Object* root);
40   ~CPDF_ObjectWalker();
41 
42   const CPDF_Object* GetNext();
43   void SkipWalkIntoCurrentObject();
44 
current_depth()45   size_t current_depth() const { return current_depth_; }
GetParent()46   const CPDF_Object* GetParent() const { return parent_object_.Get(); }
dictionary_key()47   const ByteString& dictionary_key() const { return dict_key_; }
48 
49  private:
50   static std::unique_ptr<SubobjectIterator> MakeIterator(
51       const CPDF_Object* object);
52 
53   RetainPtr<const CPDF_Object> next_object_;
54   RetainPtr<const CPDF_Object> parent_object_;
55   ByteString dict_key_;
56   size_t current_depth_ = 0;
57   std::stack<std::unique_ptr<SubobjectIterator>> stack_;
58 };
59 
60 class CPDF_NonConstObjectWalker final : public CPDF_ObjectWalker {
61  public:
CPDF_NonConstObjectWalker(CPDF_Object * root)62   explicit CPDF_NonConstObjectWalker(CPDF_Object* root)
63       : CPDF_ObjectWalker(root) {}
64 
GetNext()65   CPDF_Object* GetNext() {
66     return const_cast<CPDF_Object*>(CPDF_ObjectWalker::GetNext());
67   }
68 };
69 
70 #endif  // CORE_FPDFAPI_PARSER_CPDF_OBJECT_WALKER_H_
71