Add some more access to PKL.
authorCarl Hetherington <cth@carlh.net>
Mon, 18 Jan 2021 21:53:12 +0000 (22:53 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 18 Jan 2021 21:53:12 +0000 (22:53 +0100)
src/pkl.cc
src/pkl.h

index 5fb27aab7f6b8eaf70d6ef51a3cc017192ee6fc3..2a5b8b7dc68a66c162e989bf96a40f5c46e780f7 100644 (file)
@@ -42,6 +42,7 @@
 
 using std::string;
 using std::shared_ptr;
+using std::make_shared;
 using boost::optional;
 using namespace dcp;
 
@@ -68,15 +69,15 @@ PKL::PKL (boost::filesystem::path file)
        _issuer = pkl.string_child ("Issuer");
        _creator = pkl.string_child ("Creator");
 
-       BOOST_FOREACH (cxml::ConstNodePtr i, pkl.node_child("AssetList")->node_children("Asset")) {
-               _asset_list.push_back (shared_ptr<Asset> (new Asset (i)));
+       for (auto i: pkl.node_child("AssetList")->node_children("Asset")) {
+               _asset_list.push_back (make_shared<Asset>(i));
        }
 }
 
 void
 PKL::add_asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type)
 {
-       _asset_list.push_back (shared_ptr<Asset> (new Asset (id, annotation_text, hash, size, type)));
+       _asset_list.push_back (make_shared<Asset>(id, annotation_text, hash, size, type));
 }
 
 void
@@ -98,16 +99,16 @@ PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> sig
        pkl->add_child("Issuer")->add_child_text (_issuer);
        pkl->add_child("Creator")->add_child_text (_creator);
 
-       xmlpp::Element* asset_list = pkl->add_child("AssetList");
-       BOOST_FOREACH (shared_ptr<Asset> i, _asset_list) {
-               xmlpp::Element* asset = asset_list->add_child("Asset");
+       auto asset_list = pkl->add_child("AssetList");
+       for (auto i: _asset_list) {
+               auto asset = asset_list->add_child("Asset");
                asset->add_child("Id")->add_child_text ("urn:uuid:" + i->id());
-               if (i->annotation_text) {
-                       asset->add_child("AnnotationText")->add_child_text (*i->annotation_text);
+               if (i->annotation_text()) {
+                       asset->add_child("AnnotationText")->add_child_text (*i->annotation_text());
                }
-               asset->add_child("Hash")->add_child_text (i->hash);
-               asset->add_child("Size")->add_child_text (raw_convert<string> (i->size));
-               asset->add_child("Type")->add_child_text (i->type);
+               asset->add_child("Hash")->add_child_text (i->hash());
+               asset->add_child("Size")->add_child_text (raw_convert<string>(i->size()));
+               asset->add_child("Type")->add_child_text (i->type());
        }
 
        indent (pkl, 0);
@@ -123,23 +124,23 @@ PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> sig
 optional<string>
 PKL::hash (string id) const
 {
-       BOOST_FOREACH (shared_ptr<Asset> i, _asset_list) {
+       for (auto i: _asset_list) {
                if (i->id() == id) {
-                       return i->hash;
+                       return i->hash();
                }
        }
 
-       return optional<string>();
+       return {};
 }
 
 optional<string>
 PKL::type (string id) const
 {
-       BOOST_FOREACH (shared_ptr<Asset> i, _asset_list) {
+       for (auto i: _asset_list) {
                if (i->id() == id) {
-                       return i->type;
+                       return i->type();
                }
        }
 
-       return optional<string>();
+       return {};
 }
index 0ea87219ce944c7b436d8b219a8808b8b70b2ea5..515c88b8783c89525b7db127e5664e2c6d70551a 100644 (file)
--- a/src/pkl.h
+++ b/src/pkl.h
@@ -60,6 +60,10 @@ public:
                return _standard;
        }
 
+       boost::optional<std::string> annotation_text () const {
+               return _annotation_text;
+       }
+
        boost::optional<std::string> hash (std::string id) const;
        boost::optional<std::string> type (std::string id) const;
 
@@ -71,33 +75,54 @@ public:
                return _file;
        }
 
-private:
-
        class Asset : public Object
        {
        public:
                Asset (cxml::ConstNodePtr node)
                        : Object (remove_urn_uuid(node->string_child("Id")))
-                       , annotation_text (node->optional_string_child("AnnotationText"))
-                       , hash (node->string_child("Hash"))
-                       , size (node->number_child<int64_t>("Size"))
-                       , type (node->string_child("Type"))
+                       , _annotation_text (node->optional_string_child("AnnotationText"))
+                       , _hash (node->string_child("Hash"))
+                       , _size (node->number_child<int64_t>("Size"))
+                       , _type (node->string_child("Type"))
                {}
 
-               Asset (std::string id_, boost::optional<std::string> annotation_text_, std::string hash_, int64_t size_, std::string type_)
-                       : Object (id_)
-                       , annotation_text (annotation_text_)
-                       , hash (hash_)
-                       , size (size_)
-                       , type (type_)
+               Asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type)
+                       : Object (id)
+                       , _annotation_text (annotation_text)
+                       , _hash (hash)
+                       , _size (size)
+                       , _type (type)
                {}
 
-               boost::optional<std::string> annotation_text;
-               std::string hash;
-               int64_t size;
-               std::string type;
+               boost::optional<std::string> annotation_text () const {
+                       return _annotation_text;
+               }
+
+               std::string hash () const {
+                       return _hash;
+               }
+
+               int64_t size () const {
+                       return _size;
+               }
+
+               std::string type () const {
+                       return _type;
+               }
+
+       private:
+               boost::optional<std::string> _annotation_text;
+               std::string _hash;
+               int64_t _size;
+               std::string _type;
        };
 
+       std::vector<std::shared_ptr<Asset>> asset_list () const {
+               return _asset_list;
+       }
+
+private:
+
        Standard _standard;
        boost::optional<std::string> _annotation_text;
        std::string _issue_date;