HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inMemoryAsset.h
Go to the documentation of this file.
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 
35 
36 /// \class ArInMemoryAsset
37 ///
38 /// ArAsset implementation that stores asset content in a heap-allocated
39 /// buffer managed by this object.
41  : public ArAsset
42 {
43 public:
44  /// Constructs a new instance containing the entire contents of \p srcAsset.
45  ///
46  /// Returns nullptr if a buffer large enough to hold \p srcAsset's contents
47  /// cannot be allocated or if an error occurs when reading \p srcAsset's
48  /// contents into the buffer.
49  AR_API
50  static std::shared_ptr<ArInMemoryAsset> FromAsset(
51  const ArAsset& srcAsset);
52 
53  /// Constructs a new instance sharing ownership of the given \p buffer
54  /// containing \p bufferSize bytes.
55  AR_API
56  static std::shared_ptr<ArInMemoryAsset> FromBuffer(
57  const std::shared_ptr<const char>& buffer,
58  size_t bufferSize);
59 
60  /// Constructs a new instance taking ownership of the given \p buffer
61  /// containing \p bufferSize bytes.
62  AR_API
63  static std::shared_ptr<ArInMemoryAsset> FromBuffer(
64  std::shared_ptr<const char>&& buffer,
65  size_t bufferSize);
66 
67  /// Destructor. Note that this may not destroy the associated buffer if
68  /// a client is holding on to the result of GetBuffer().
69  AR_API
71 
72  /// Returns the size of the buffer managed by this object.
73  AR_API
74  size_t GetSize() const override;
75 
76  /// Returns the buffer managed by this object.
77  AR_API
78  std::shared_ptr<const char> GetBuffer() const override;
79 
80  /// Reads \p count bytes from the buffer held by this object at the
81  /// given \p offset into \p buffer.
82  AR_API
83  size_t Read(void* buffer, size_t count, size_t offset) const override;
84 
85  /// Returns { nullptr, 0 } as this object is not associated with a file.
86  AR_API
87  std::pair<FILE*, size_t> GetFileUnsafe() const override;
88 
89  /// Returns a new ArInMemoryAsset instance that shares the same buffer
90  /// as this object.
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 
111 
112 #endif
AR_API std::shared_ptr< ArAsset > GetDetachedAsset() const override
AR_API size_t GetSize() const override
Returns the size of the buffer managed by this object.
Definition: asset.h:44
AR_API std::pair< FILE *, size_t > GetFileUnsafe() const override
Returns { nullptr, 0 } as this object is not associated with a file.
#define AR_API
Definition: api.h:40
static AR_API std::shared_ptr< ArInMemoryAsset > FromAsset(const ArAsset &srcAsset)
static AR_API std::shared_ptr< ArInMemoryAsset > FromBuffer(const std::shared_ptr< const char > &buffer, size_t bufferSize)
GLintptr offset
Definition: glcorearb.h:665
Definition: core.h:760
AR_API ~ArInMemoryAsset()
AR_API size_t Read(void *buffer, size_t count, size_t offset) const override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
AR_API std::shared_ptr< const char > GetBuffer() const override
Returns the buffer managed by this object.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
ArInMemoryAsset(BufferSharedPtr &&buffer, size_t bufferSize, PrivateCtorTag)
GLint GLsizei count
Definition: glcorearb.h:405