#ifndef MEDIAPIPE_FRAMEWORK_COLLECTION_H_
#define MEDIAPIPE_FRAMEWORK_COLLECTION_H_
#include <cstdlib>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#include "absl/base/macros.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/collection_item_id.h"
#include "mediapipe/framework/tool/tag_map.h"
#include "mediapipe/framework/tool/tag_map_helper.h"
#include "mediapipe/framework/tool/validate_name.h"
#include "mediapipe/framework/type_map.h"
namespace mediapipe {
namespace internal {
template <typename T>
struct CollectionErrorHandlerFatal { … };
enum class CollectionStorage { … };
template <typename T,
CollectionStorage storage = CollectionStorage::kStoreValue,
typename ErrorHandler = CollectionErrorHandlerFatal<T>>
class Collection {
private:
template <typename ItType>
class DoubleDerefIterator;
public:
using value_type = T;
using iterator =
typename std::conditional<storage == CollectionStorage::kStorePointer,
DoubleDerefIterator<value_type>,
value_type*>::type;
using const_iterator =
typename std::conditional<storage == CollectionStorage::kStorePointer,
DoubleDerefIterator<const value_type>,
const value_type*>::type;
using difference_type = ptrdiff_t;
using size_type = size_t;
using pointer = value_type*;
using reference = value_type&;
using stored_type =
typename std::conditional<storage == CollectionStorage::kStorePointer,
value_type*, value_type>::type;
Collection() = delete;
Collection(const Collection&) = delete;
Collection& operator=(const Collection&) = delete;
explicit Collection(std::shared_ptr<tool::TagMap> tag_map);
ABSL_DEPRECATED("Use Collection(tool::TagMap)")
explicit Collection(const tool::TagAndNameInfo& info);
ABSL_DEPRECATED("Use Collection(tool::TagMap)")
explicit Collection(int num_entries);
ABSL_DEPRECATED("Use Collection(tool::TagMap)")
Collection(const std::initializer_list<std::string>& tag_names);
value_type& Get(CollectionItemId id);
const value_type& Get(CollectionItemId id) const;
value_type& Get(absl::string_view tag, int index);
const value_type& Get(absl::string_view tag, int index) const;
value_type& Index(int index);
const value_type& Index(int index) const;
value_type& Tag(absl::string_view tag);
const value_type& Tag(absl::string_view tag) const;
value_type*& GetPtr(CollectionItemId id);
const value_type* GetPtr(CollectionItemId id) const;
bool UsesTags() const;
std::string DebugString() const;
const std::shared_ptr<tool::TagMap>& TagMap() const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
const ErrorHandler& GetErrorHandler() const { … }
bool HasTag(const absl::string_view tag) const { … }
int NumEntries() const { … }
int NumEntries(const absl::string_view tag) const { … }
CollectionItemId GetId(const absl::string_view tag, int index) const { … }
std::set<std::string> GetTags() const { … }
std::pair<std::string, int> TagAndIndexFromId(CollectionItemId id) const { … }
CollectionItemId BeginId() const { … }
CollectionItemId EndId() const { … }
CollectionItemId BeginId(const absl::string_view tag) const { … }
CollectionItemId EndId(const absl::string_view tag) const { … }
bool operator==(const Collection<T>& other) const { … }
bool operator!=(const Collection<T>& other) const { … }
private:
template <typename ItType>
class DoubleDerefIterator {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = ItType;
using difference_type = std::ptrdiff_t;
using pointer = ItType*;
using reference = ItType&;
DoubleDerefIterator() : ptr_(nullptr) {}
reference operator*() { return **ptr_; }
pointer operator->() { return *ptr_; }
reference operator[](difference_type d) { return **(ptr_ + d); }
DoubleDerefIterator& operator++() {
++ptr_;
return *this;
}
DoubleDerefIterator operator++(int) {
DoubleDerefIterator output(ptr_);
++ptr_;
return output;
}
DoubleDerefIterator& operator--() {
--ptr_;
return *this;
}
DoubleDerefIterator operator--(int) {
DoubleDerefIterator output(ptr_);
--ptr_;
return output;
}
DoubleDerefIterator& operator+=(difference_type d) {
ptr_ += d;
return *this;
}
DoubleDerefIterator& operator-=(difference_type d) {
ptr_ -= d;
return *this;
}
friend bool operator==(DoubleDerefIterator lhs, DoubleDerefIterator rhs) {
return lhs.ptr_ == rhs.ptr_;
}
friend bool operator!=(DoubleDerefIterator lhs, DoubleDerefIterator rhs) {
return lhs.ptr_ != rhs.ptr_;
}
friend bool operator<(DoubleDerefIterator lhs, DoubleDerefIterator rhs) {
return lhs.ptr_ < rhs.ptr_;
}
friend bool operator<=(DoubleDerefIterator lhs, DoubleDerefIterator rhs) {
return lhs.ptr_ <= rhs.ptr_;
}
friend bool operator>(DoubleDerefIterator lhs, DoubleDerefIterator rhs) {
return lhs.ptr_ > rhs.ptr_;
}
friend bool operator>=(DoubleDerefIterator lhs, DoubleDerefIterator rhs) {
return lhs.ptr_ >= rhs.ptr_;
}
friend DoubleDerefIterator operator+(DoubleDerefIterator lhs,
difference_type d) {
return lhs.ptr_ + d;
}
friend DoubleDerefIterator operator+(difference_type d,
DoubleDerefIterator rhs) {
return rhs.ptr_ + d;
}
friend DoubleDerefIterator& operator-(DoubleDerefIterator lhs,
difference_type d) {
return lhs.ptr_ - d;
}
friend difference_type operator-(DoubleDerefIterator lhs,
DoubleDerefIterator rhs) {
return lhs.ptr_ - rhs.ptr_;
}
private:
explicit DoubleDerefIterator(ItType* const* data) : ptr_(data) {}
ItType* const* ptr_;
friend class Collection;
};
std::shared_ptr<tool::TagMap> tag_map_;
std::unique_ptr<stored_type[]> data_;
ErrorHandler error_handler_;
};
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(
std::shared_ptr<tool::TagMap> tag_map)
: … { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(
const tool::TagAndNameInfo& info)
: … { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(const int num_entries)
: … { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(
const std::initializer_list<std::string>& tag_names)
: … { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
bool Collection<T, storage, ErrorHandler>::UsesTags() const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Get(CollectionItemId id) { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
const typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Get(CollectionItemId id) const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::value_type*&
Collection<T, storage, ErrorHandler>::GetPtr(CollectionItemId id) { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
const typename Collection<T, storage, ErrorHandler>::value_type*
Collection<T, storage, ErrorHandler>::GetPtr(CollectionItemId id) const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Get(const absl::string_view tag,
int index) { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
const typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Get(const absl::string_view tag,
int index) const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Index(int index) { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
const typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Index(int index) const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Tag(const absl::string_view tag) { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
const typename Collection<T, storage, ErrorHandler>::value_type&
Collection<T, storage, ErrorHandler>::Tag(const absl::string_view tag) const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
std::string Collection<T, storage, ErrorHandler>::DebugString() const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
const std::shared_ptr<tool::TagMap>&
Collection<T, storage, ErrorHandler>::TagMap() const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::iterator
Collection<T, storage, ErrorHandler>::begin() { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::iterator
Collection<T, storage, ErrorHandler>::end() { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::const_iterator
Collection<T, storage, ErrorHandler>::begin() const { … }
template <typename T, CollectionStorage storage, typename ErrorHandler>
typename Collection<T, storage, ErrorHandler>::const_iterator
Collection<T, storage, ErrorHandler>::end() const { … }
}
template <class S>
bool HasTagValue(const internal::Collection<S*>& c,
const absl::string_view tag) { … }
template <class S>
bool HasTagValue(const internal::Collection<S>& c,
const absl::string_view tag) { … }
template <class C>
bool HasTagValue(const C& c, const absl::string_view tag) { … }
}
#endif