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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 #ifndef PXR_USD_AR_IN_MEMORY_ASSET_H
7 #define PXR_USD_AR_IN_MEMORY_ASSET_H
8 
9 #include "pxr/pxr.h"
10 #include "pxr/usd/ar/api.h"
11 #include "pxr/usd/ar/asset.h"
12 
13 #include <cstdio>
14 #include <memory>
15 #include <utility>
16 
18 
19 /// \class ArInMemoryAsset
20 ///
21 /// ArAsset implementation that stores asset content in a heap-allocated
22 /// buffer managed by this object.
24  : public ArAsset
25 {
26 public:
27  /// Constructs a new instance containing the entire contents of \p srcAsset.
28  ///
29  /// Returns nullptr if a buffer large enough to hold \p srcAsset's contents
30  /// cannot be allocated or if an error occurs when reading \p srcAsset's
31  /// contents into the buffer.
32  AR_API
33  static std::shared_ptr<ArInMemoryAsset> FromAsset(
34  const ArAsset& srcAsset);
35 
36  /// Constructs a new instance sharing ownership of the given \p buffer
37  /// containing \p bufferSize bytes.
38  AR_API
39  static std::shared_ptr<ArInMemoryAsset> FromBuffer(
40  const std::shared_ptr<const char>& buffer,
41  size_t bufferSize);
42 
43  /// Constructs a new instance taking ownership of the given \p buffer
44  /// containing \p bufferSize bytes.
45  AR_API
46  static std::shared_ptr<ArInMemoryAsset> FromBuffer(
47  std::shared_ptr<const char>&& buffer,
48  size_t bufferSize);
49 
50  /// Destructor. Note that this may not destroy the associated buffer if
51  /// a client is holding on to the result of GetBuffer().
52  AR_API
54 
55  /// Returns the size of the buffer managed by this object.
56  AR_API
57  size_t GetSize() const override;
58 
59  /// Returns the buffer managed by this object.
60  AR_API
61  std::shared_ptr<const char> GetBuffer() const override;
62 
63  /// Reads \p count bytes from the buffer held by this object at the
64  /// given \p offset into \p buffer.
65  AR_API
66  size_t Read(void* buffer, size_t count, size_t offset) const override;
67 
68  /// Returns { nullptr, 0 } as this object is not associated with a file.
69  AR_API
70  std::pair<FILE*, size_t> GetFileUnsafe() const override;
71 
72  /// Returns a new ArInMemoryAsset instance that shares the same buffer
73  /// as this object.
74  AR_API
75  std::shared_ptr<ArAsset> GetDetachedAsset() const override;
76 
77 private:
78  struct PrivateCtorTag {};
79 public:
80  // "Private" c'tor. Must actually be public for std::make_shared,
81  // but the PrivateCtorTag prevents other code from using this.
82  template <class BufferSharedPtr>
84  BufferSharedPtr&& buffer,
85  size_t bufferSize,
86  PrivateCtorTag);
87 
88 private:
89  std::shared_ptr<const char> _buffer;
90  size_t _bufferSize;
91 };
92 
94 
95 #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:27
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:23
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)
GLuint buffer
Definition: glcorearb.h:660
GLintptr offset
Definition: glcorearb.h:665
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:1425
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:74
ArInMemoryAsset(BufferSharedPtr &&buffer, size_t bufferSize, PrivateCtorTag)
GLint GLsizei count
Definition: glcorearb.h:405