// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef CORE_PAYLOAD_H_ #define CORE_PAYLOAD_H_ #include #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 asInputStream() const; private: template friend class InternalPayloadFactory; friend class Payload; explicit Stream(Ptr input_stream); ScopedPtr > input_stream_; }; class File { public: Ptr asInputFile() const; private: template friend class InternalPayloadFactory; friend class Payload; explicit File(const Ptr& input_file); ScopedPtr > input_file_; }; static Ptr fromBytes(ConstPtr bytes); static Ptr fromStream(Ptr input_stream); static Ptr fromFile(const Ptr& input_file); ConstPtr asBytes() const; ConstPtr asStream() const; ConstPtr asFile() const; // For when clients of this class want to assume ownership of the // ConstPtr that represents a BYTES Payload. ConstPtr releaseBytes() const; std::int64_t getId() const; Type::Value getType() const; private: template friend class InternalPayloadFactory; static std::int64_t generateId(); Payload(std::int64_t id, ConstPtr bytes); Payload(std::int64_t id, ConstPtr stream); Payload(std::int64_t id, ConstPtr 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 > bytes_; ScopedPtr > file_; ScopedPtr > stream_; }; } // namespace connections } // namespace nearby } // namespace location #endif // CORE_PAYLOAD_H_