Dict.h 3.98 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8
//========================================================================
//
// Dict.h
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

9 10 11 12
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
13 14 15
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
16 17 18
// Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
// Copyright (C) 2007-2008 Julien Rebetez <julienr@svn.gnome.org>
19
// Copyright (C) 2010, 2017-2019 Albert Astals Cid <aacid@kde.org>
20
// Copyright (C) 2010 Paweł Wiejacha <pawel.wiejacha@gmail.com>
21
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
22
// Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
23
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
24 25 26 27 28 29
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

Kristian Høgsberg's avatar
Kristian Høgsberg committed
30 31 32
#ifndef DICT_H
#define DICT_H

33
#include <atomic>
34
#include <mutex>
35 36 37 38
#include <string>
#include <vector>
#include <utility>

39
#include "poppler-config.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
40 41 42 43 44 45 46 47 48 49 50
#include "Object.h"

//------------------------------------------------------------------------
// Dict
//------------------------------------------------------------------------

class Dict {
public:

  // Constructor.
  Dict(XRef *xrefA);
51 52
  Dict(const Dict *dictA);
  Dict *copy(XRef *xrefA) const;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
53

54 55 56
  Dict(const Dict &) = delete;
  Dict& operator=(const Dict &) = delete;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
57
  // Get number of entries.
58
  int getLength() const { return static_cast<int>(entries.size()); }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
59

60
  // Add an entry. (Copies key into Dict.)
61
  // val becomes a dead object after the call
62
  void add(const char *key, Object &&val);
63

64 65 66
  // Add an entry. (Takes ownership of key.)
  void add(char *key, Object &&val) = delete;

67
  // Update the value of an existing entry, otherwise create it
68
  // val becomes a dead object after the call
Albert Astals Cid's avatar
Albert Astals Cid committed
69
  void set(const char *key, Object &&val);
70
  // Remove an entry. This invalidate indexes
71
  void remove(const char *key);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
72 73

  // Check if dictionary is of specified type.
74
  bool is(const char *type) const;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
75 76 77

  // Look up an entry and return the value.  Returns a null object
  // if <key> is not in the dictionary.
78
  Object lookup(const char *key, int recursion = 0) const;
79
  // Same as above but if the returned object is a fetched Ref returns such Ref in returnRef, otherwise returnRef is Ref::INVALID()
80
  Object lookup(const char *key, Ref *returnRef, int recursion = 0) const;
81
  const Object &lookupNF(const char *key) const;
82
  bool lookupInt(const char *key, const char *alt_key, int *value) const;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
83 84

  // Iterative accessors.
85 86
  const char *getKey(int i) const { return entries[i].first.c_str(); }
  Object getVal(int i) const { return entries[i].second.fetch(xref); }
87 88
  // Same as above but if the returned object is a fetched Ref returns such Ref in returnRef, otherwise returnRef is Ref::INVALID()
  Object getVal(int i, Ref *returnRef) const;
89
  const Object &getValNF(int i) const { return entries[i].second; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
90 91 92 93 94

  // Set the xref pointer.  This is only used in one special case: the
  // trailer dictionary, which is read before the xref table is
  // parsed.
  void setXRef(XRef *xrefA) { xref = xrefA; }
95
  
96
  XRef *getXRef() const { return xref; }
97
  
98
  bool hasKey(const char *key) const;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
99 100

private:
101 102 103
  friend class Object; // for incRef/decRef

  // Reference counting.
104 105 106 107 108
  int incRef() { return ++ref; }
  int decRef() { return --ref; }

  using DictEntry = std::pair<std::string, Object>;
  struct CmpDictEntry;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
109

110
  std::atomic_bool sorted;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
111
  XRef *xref;			// the xref table for this PDF file
112 113
  std::vector<DictEntry> entries;
  std::atomic_int ref;			// reference count
114
  mutable std::recursive_mutex mutex;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
115

116 117
  const DictEntry *find(const char *key) const;
  DictEntry *find(const char *key);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
118 119 120
};

#endif