summaryrefslogtreecommitdiff
path: root/includes/dynamic_depth/item.h
blob: d1fcd3685614f7965b570b7d3da39474dae07748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_ITEM_H_  // NOLINT
#define DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_ITEM_H_  // NOLINT

#include <memory>
#include <string>
#include <unordered_map>

#include "dynamic_depth/dimension.h"
#include "dynamic_depth/element.h"
#include "dynamic_depth/point.h"
#include "xmpmeta/xml/deserializer.h"
#include "xmpmeta/xml/serializer.h"

namespace photos_editing_formats {
namespace dynamic_depth {
struct ItemParams {
  // Required fields.
  string mime;          // Must not be empty.
  unsigned int length;  // Must not be zero.

  // Optional.
  unsigned int padding;
  string data_uri;

  // Only for final file serialization - not used in XMP metadata I/O.
  // IMPORTANT: Callers should enforce that this file exists.
  // TODO(miraleung): This could be stored as char* to optimize performance.
  string payload_to_serialize;

  ItemParams(const string& in_mime, unsigned int len)
      : mime(in_mime),
        length(len),
        padding(0),
        data_uri(""),
        payload_to_serialize("") {}
  ItemParams(const string& in_mime, unsigned int len, const string& uri)
      : mime(in_mime),
        length(len),
        padding(0),
        data_uri(uri),
        payload_to_serialize("") {}

  inline bool operator==(const ItemParams& other) const {
    return mime == other.mime && length == other.length &&
           padding == other.padding && data_uri == other.data_uri &&
           payload_to_serialize == other.payload_to_serialize;
  }

  inline bool operator!=(const ItemParams& other) const {
    return !(*this == other);
  }
};

class Item : public Element {
 public:
  void GetNamespaces(
      std::unordered_map<string, string>* ns_name_href_map) override;

  bool Serialize(xml::Serializer* serializer) const override;

  static std::unique_ptr<Item> FromData(const ItemParams& params);

  // Returns the deserialized item elements, null if parsing failed for all
  // items.
  static std::unique_ptr<Item> FromDeserializer(
      const xml::Deserializer& parent_deserializer);

  const string& GetMime() const;
  unsigned int GetLength() const;
  const string& GetDataUri() const;
  unsigned int GetPadding() const;
  const string& GetPayloadToSerialize() const;

  // Disallow copying.
  Item(const Item&) = delete;
  void operator=(const Item&) = delete;

 private:
  Item(const ItemParams& params);
  static std::unique_ptr<Item> FromDataInternal(const ItemParams& params,
                                                bool check_filepath);

  ItemParams params_;
};

}  // namespace dynamic_depth
}  // namespace photos_editing_formats

#endif // DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_ITEM_H_  // NOLINT