mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I2cf5bf225b76f4c1541954651f3a7544a14e0cec
87 lines
2.1 KiB
C++
87 lines
2.1 KiB
C++
#ifndef CORE_PAYLOAD_H_
|
|
#define CORE_PAYLOAD_H_
|
|
|
|
#include <cstdint>
|
|
|
|
#include "platform/api/input_file.h"
|
|
#include "platform/api/input_stream.h"
|
|
#include "platform/byte_array.h"
|
|
#include "platform/ptr.h"
|
|
|
|
namespace location {
|
|
namespace nearby {
|
|
namespace connections {
|
|
|
|
class Payload {
|
|
public:
|
|
struct Type {
|
|
enum Value { UNKNOWN = 0, BYTES = 1, FILE = 2, STREAM = 3 };
|
|
};
|
|
|
|
class Stream {
|
|
public:
|
|
Ptr<InputStream> asInputStream() const;
|
|
|
|
private:
|
|
template <typename>
|
|
friend class InternalPayloadFactory;
|
|
friend class Payload;
|
|
|
|
explicit Stream(Ptr<InputStream> input_stream);
|
|
ScopedPtr<Ptr<InputStream> > input_stream_;
|
|
};
|
|
|
|
class File {
|
|
public:
|
|
Ptr<InputFile> asInputFile() const;
|
|
|
|
private:
|
|
template <typename>
|
|
friend class InternalPayloadFactory;
|
|
friend class Payload;
|
|
|
|
explicit File(const Ptr<InputFile>& input_file);
|
|
ScopedPtr<Ptr<InputFile> > input_file_;
|
|
};
|
|
|
|
static Ptr<Payload> fromBytes(ConstPtr<ByteArray> bytes);
|
|
static Ptr<Payload> fromStream(Ptr<InputStream> input_stream);
|
|
static Ptr<Payload> fromFile(const Ptr<InputFile>& input_file);
|
|
|
|
ConstPtr<ByteArray> asBytes() const;
|
|
ConstPtr<Stream> asStream() const;
|
|
ConstPtr<File> asFile() const;
|
|
|
|
// For when clients of this class want to assume ownership of the
|
|
// ConstPtr<ByteArray> that represents a BYTES Payload.
|
|
ConstPtr<ByteArray> releaseBytes() const;
|
|
|
|
std::int64_t getId() const;
|
|
Type::Value getType() const;
|
|
|
|
private:
|
|
template <typename>
|
|
friend class InternalPayloadFactory;
|
|
|
|
static std::int64_t generateId();
|
|
|
|
Payload(std::int64_t id, ConstPtr<ByteArray> bytes);
|
|
Payload(std::int64_t id, ConstPtr<Stream> stream);
|
|
Payload(std::int64_t id, ConstPtr<File> file);
|
|
|
|
std::int64_t id_;
|
|
Type::Value type_;
|
|
// This field is mutable because of releaseBytes(), which is just a physically
|
|
// non-const operation that doesn't alter the conceptual const-ness of the
|
|
// Payload object.
|
|
mutable ScopedPtr<ConstPtr<ByteArray> > bytes_;
|
|
ScopedPtr<ConstPtr<File> > file_;
|
|
ScopedPtr<ConstPtr<Stream> > stream_;
|
|
};
|
|
|
|
} // namespace connections
|
|
} // namespace nearby
|
|
} // namespace location
|
|
|
|
#endif // CORE_PAYLOAD_H_
|