#ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
#define BASE_MEMORY_REF_COUNTED_MEMORY_H_
#include <stddef.h>
#include <memory>
#include <string>
#include <vector>
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/memory/raw_span.h"
#include "base/memory/ref_counted.h"
#include "base/memory/shared_memory_mapping.h"
namespace base {
class ReadOnlySharedMemoryRegion;
class BASE_EXPORT RefCountedMemory
: public RefCountedThreadSafe<RefCountedMemory> {
public:
bool Equals(const scoped_refptr<RefCountedMemory>& other) const;
const uint8_t* data() const LIFETIME_BOUND { return AsSpan().data(); }
size_t size() const { return AsSpan().size(); }
using iterator = base::span<const uint8_t>::iterator;
iterator begin() const LIFETIME_BOUND { return AsSpan().begin(); }
iterator end() const LIFETIME_BOUND { return AsSpan().end(); }
const uint8_t* front() const LIFETIME_BOUND { return AsSpan().data(); }
explicit operator base::span<const uint8_t>() const LIFETIME_BOUND {
return AsSpan();
}
protected:
friend class RefCountedThreadSafe<RefCountedMemory>;
RefCountedMemory();
virtual ~RefCountedMemory();
virtual base::span<const uint8_t> AsSpan() const LIFETIME_BOUND = 0;
};
class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
public:
RefCountedStaticMemory();
explicit RefCountedStaticMemory(base::span<const uint8_t> bytes);
RefCountedStaticMemory(const RefCountedStaticMemory&) = delete;
RefCountedStaticMemory& operator=(const RefCountedStaticMemory&) = delete;
private:
~RefCountedStaticMemory() override;
base::span<const uint8_t> AsSpan() const LIFETIME_BOUND override;
base::raw_span<const uint8_t> bytes_;
};
class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
public:
RefCountedBytes();
explicit RefCountedBytes(std::vector<uint8_t> initializer);
explicit RefCountedBytes(base::span<const uint8_t> initializer);
explicit RefCountedBytes(size_t size);
RefCountedBytes(const RefCountedBytes&) = delete;
RefCountedBytes& operator=(const RefCountedBytes&) = delete;
const std::vector<uint8_t>& as_vector() const { return bytes_; }
std::vector<uint8_t>& as_vector() { return bytes_; }
private:
~RefCountedBytes() override;
base::span<const uint8_t> AsSpan() const LIFETIME_BOUND override;
std::vector<uint8_t> bytes_;
};
class BASE_EXPORT RefCountedString : public RefCountedMemory {
public:
RefCountedString();
explicit RefCountedString(std::string value);
RefCountedString(const RefCountedString&) = delete;
RefCountedString& operator=(const RefCountedString&) = delete;
const std::string& as_string() const LIFETIME_BOUND { return string_; }
std::string& as_string() LIFETIME_BOUND { return string_; }
private:
~RefCountedString() override;
base::span<const uint8_t> AsSpan() const LIFETIME_BOUND override;
std::string string_;
};
class BASE_EXPORT RefCountedString16 : public base::RefCountedMemory {
public:
RefCountedString16();
explicit RefCountedString16(std::u16string value);
RefCountedString16(const RefCountedString16&) = delete;
RefCountedString16& operator=(const RefCountedString16&) = delete;
const std::u16string& as_string() const LIFETIME_BOUND { return string_; }
std::u16string& as_string() LIFETIME_BOUND { return string_; }
private:
~RefCountedString16() override;
base::span<const uint8_t> AsSpan() const LIFETIME_BOUND override;
std::u16string string_;
};
class BASE_EXPORT RefCountedSharedMemoryMapping : public RefCountedMemory {
public:
explicit RefCountedSharedMemoryMapping(ReadOnlySharedMemoryMapping mapping);
RefCountedSharedMemoryMapping(const RefCountedSharedMemoryMapping&) = delete;
RefCountedSharedMemoryMapping& operator=(
const RefCountedSharedMemoryMapping&) = delete;
static scoped_refptr<RefCountedSharedMemoryMapping> CreateFromWholeRegion(
const ReadOnlySharedMemoryRegion& region);
private:
~RefCountedSharedMemoryMapping() override;
base::span<const uint8_t> AsSpan() const LIFETIME_BOUND override;
const ReadOnlySharedMemoryMapping mapping_;
};
}
#endif