• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_ITEM_H_  // NOLINT
2 #define DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_ITEM_H_  // NOLINT
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 
8 #include "dynamic_depth/dimension.h"
9 #include "dynamic_depth/element.h"
10 #include "dynamic_depth/point.h"
11 #include "xmpmeta/xml/deserializer.h"
12 #include "xmpmeta/xml/serializer.h"
13 
14 namespace dynamic_depth {
15 struct ItemParams {
16   // Required fields.
17   string mime;          // Must not be empty.
18   unsigned int length;  // Must not be zero.
19 
20   // Optional.
21   unsigned int padding;
22   string data_uri;
23 
24   // Only for final file serialization - not used in XMP metadata I/O.
25   // IMPORTANT: Callers should enforce that this file exists.
26   // TODO(miraleung): This could be stored as char* to optimize performance.
27   string payload_to_serialize;
28 
ItemParamsItemParams29   ItemParams(const string& in_mime, unsigned int len)
30       : mime(in_mime),
31         length(len),
32         padding(0),
33         data_uri(""),
34         payload_to_serialize("") {}
ItemParamsItemParams35   ItemParams(const string& in_mime, unsigned int len, const string& uri)
36       : mime(in_mime),
37         length(len),
38         padding(0),
39         data_uri(uri),
40         payload_to_serialize("") {}
41 
42   inline bool operator==(const ItemParams& other) const {
43     return mime == other.mime && length == other.length &&
44            padding == other.padding && data_uri == other.data_uri &&
45            payload_to_serialize == other.payload_to_serialize;
46   }
47 
48   inline bool operator!=(const ItemParams& other) const {
49     return !(*this == other);
50   }
51 };
52 
53 class Item : public Element {
54  public:
55   void GetNamespaces(
56       std::unordered_map<string, string>* ns_name_href_map) override;
57 
58   bool Serialize(
59       ::dynamic_depth::xmpmeta::xml::Serializer* serializer) const override;
60 
61   static std::unique_ptr<Item> FromData(const ItemParams& params);
62 
63   // Returns the deserialized item elements, null if parsing failed for all
64   // items.
65   static std::unique_ptr<Item> FromDeserializer(
66       const ::dynamic_depth::xmpmeta::xml::Deserializer& parent_deserializer);
67 
68   const string& GetMime() const;
69   unsigned int GetLength() const;
70   const string& GetDataUri() const;
71   unsigned int GetPadding() const;
72   const string& GetPayloadToSerialize() const;
73 
74   // Disallow copying.
75   Item(const Item&) = delete;
76   void operator=(const Item&) = delete;
77 
78  private:
79   Item(const ItemParams& params);
80   static std::unique_ptr<Item> FromDataInternal(const ItemParams& params,
81                                                 bool check_filepath);
82 
83   ItemParams params_;
84 };
85 
86 }  // namespace dynamic_depth
87 
88 #endif  // DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_ITEM_H_  // NOLINT
89