inMemoryAsset.h
1 //
2 // Copyright 2022 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 #ifndef PXR_USD_AR_IN_MEMORY_ASSET_H
24 #define PXR_USD_AR_IN_MEMORY_ASSET_H
25 
26 #include "pxr/pxr.h"
27 #include "pxr/usd/ar/api.h"
28 #include "pxr/usd/ar/asset.h"
29 
30 #include <cstdio>
31 #include <memory>
32 #include <utility>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
41  : public ArAsset
42 {
43 public:
49  AR_API
50  static std::shared_ptr<ArInMemoryAsset> FromAsset(
51  const ArAsset& srcAsset);
52 
55  AR_API
56  static std::shared_ptr<ArInMemoryAsset> FromBuffer(
57  const std::shared_ptr<const char>& buffer,
58  size_t bufferSize);
59 
62  AR_API
63  static std::shared_ptr<ArInMemoryAsset> FromBuffer(
64  std::shared_ptr<const char>&& buffer,
65  size_t bufferSize);
66 
69  AR_API
71 
73  AR_API
74  size_t GetSize() const override;
75 
77  AR_API
78  std::shared_ptr<const char> GetBuffer() const override;
79 
82  AR_API
83  size_t Read(void* buffer, size_t count, size_t offset) const override;
84 
86  AR_API
87  std::pair<FILE*, size_t> GetFileUnsafe() const override;
88 
91  AR_API
92  std::shared_ptr<ArAsset> GetDetachedAsset() const override;
93 
94 private:
95  struct PrivateCtorTag {};
96 public:
97  // "Private" c'tor. Must actually be public for std::make_shared,
98  // but the PrivateCtorTag prevents other code from using this.
99  template <class BufferSharedPtr>
101  BufferSharedPtr&& buffer,
102  size_t bufferSize,
103  PrivateCtorTag);
104 
105 private:
106  std::shared_ptr<const char> _buffer;
107  size_t _bufferSize;
108 };
109 
110 PXR_NAMESPACE_CLOSE_SCOPE
111 
112 #endif
AR_API ~ArInMemoryAsset()
Destructor.
AR_API size_t GetSize() const override
Returns the size of the buffer managed by this object.
AR_API std::shared_ptr< ArAsset > GetDetachedAsset() const override
Returns a new ArInMemoryAsset instance that shares the same buffer as this object.
AR_API std::pair< FILE *, size_t > GetFileUnsafe() const override
Returns { nullptr, 0 } as this object is not associated with a file.
Interface for accessing the contents of an asset.
Definition: asset.h:44
static AR_API std::shared_ptr< ArInMemoryAsset > FromAsset(const ArAsset &srcAsset)
Constructs a new instance containing the entire contents of srcAsset.
AR_API std::shared_ptr< const char > GetBuffer() const override
Returns the buffer managed by this object.
static AR_API std::shared_ptr< ArInMemoryAsset > FromBuffer(const std::shared_ptr< const char > &buffer, size_t bufferSize)
Constructs a new instance sharing ownership of the given buffer containing bufferSize bytes.
ArAsset implementation that stores asset content in a heap-allocated buffer managed by this object.
Definition: inMemoryAsset.h:40
AR_API size_t Read(void *buffer, size_t count, size_t offset) const override
Reads count bytes from the buffer held by this object at the given offset into buffer.