summaryrefslogtreecommitdiff
path: root/internal/dynamic_depth/profiles.cc
blob: bc49d1d12cc8da8f9d28e4b500e7be2eb9063798 (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
90
91
92
93
94
95
96
97
#include "dynamic_depth/profiles.h"

#include "android-base/logging.h"
#include "dynamic_depth/const.h"

using ::dynamic_depth::xmpmeta::xml::Deserializer;
using ::dynamic_depth::xmpmeta::xml::Serializer;

namespace dynamic_depth {

void Profiles::GetNamespaces(
    std::unordered_map<string, string>* ns_name_href_map) {
  if (ns_name_href_map == nullptr || profile_list_.empty()) {
    LOG(ERROR) << "Namespace list is null or profile list is empty";
    return;
  }
  for (const auto& profile : profile_list_) {
    profile->GetNamespaces(ns_name_href_map);
  }
}

std::unique_ptr<Profiles> Profiles::FromProfileArray(
    std::vector<std::unique_ptr<Profile>>* profile_list) {
  if (profile_list->empty()) {
    LOG(ERROR) << "Profile list is empty";
    return nullptr;
  }
  std::unique_ptr<Profiles> profiles(new Profiles());
  profiles->profile_list_ = std::move(*profile_list);
  return profiles;
}

std::unique_ptr<Profiles> Profiles::FromDeserializer(
    const Deserializer& parent_deserializer) {
  std::unique_ptr<Profiles> profiles(new Profiles());
  int i = 0;
  for (std::unique_ptr<Deserializer> deserializer =
           parent_deserializer.CreateDeserializerFromListElementAt(
               DynamicDepthConst::Namespace(DynamicDepthConst::Profiles()),
               DynamicDepthConst::Profiles(), i);
       deserializer != nullptr;
       deserializer = parent_deserializer.CreateDeserializerFromListElementAt(
           DynamicDepthConst::Namespace(DynamicDepthConst::Profiles()),
           DynamicDepthConst::Profiles(), ++i)) {
    std::unique_ptr<Profile> profile = Profile::FromDeserializer(*deserializer);
    if (profile != nullptr) {
      profiles->profile_list_.emplace_back(std::move(profile));
    }
  }

  if (profiles->profile_list_.empty()) {
    return nullptr;
  }
  return profiles;
}

const std::vector<const Profile*> Profiles::GetProfiles() const {
  std::vector<const Profile*> profile_list;
  for (const auto& profile : profile_list_) {
    profile_list.push_back(profile.get());
  }
  return profile_list;
}

bool Profiles::Serialize(Serializer* serializer) const {
  if (profile_list_.empty()) {
    LOG(ERROR) << "Profile list is empty";
    return false;
  }
  bool success = true;
  int i = 0;
  std::unique_ptr<Serializer> profiles_serializer =
      serializer->CreateListSerializer(
          DynamicDepthConst::Namespace(DynamicDepthConst::Profiles()),
          DynamicDepthConst::Profiles());
  if (profiles_serializer == nullptr) {
    // Error is logged in Serializer.
    return false;
  }
  for (const auto& profile : profile_list_) {
    std::unique_ptr<Serializer> profile_serializer =
        profiles_serializer->CreateItemSerializer(
            DynamicDepthConst::Namespace(DynamicDepthConst::Profile()),
            DynamicDepthConst::Profile());
    if (profile_serializer == nullptr) {
      continue;
    }
    success &= profile->Serialize(profile_serializer.get());
    if (!success) {
      LOG(ERROR) << "Could not serialize profile " << i;
    }
    ++i;
  }
  return success;
}

}  // namespace dynamic_depth