HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
asset.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_AR_ASSET_H
8 #define PXR_USD_AR_ASSET_H
9 
10 /// \file ar/asset.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/ar/ar.h"
14 #include "pxr/usd/ar/api.h"
15 
16 #include <cstdio>
17 #include <memory>
18 #include <utility>
19 
21 
22 /// \class ArAsset
23 ///
24 /// Interface for accessing the contents of an asset.
25 ///
26 /// \see ArResolver::OpenAsset for how to retrieve instances of this object.
27 class ArAsset {
28 public:
29  AR_API
30  virtual ~ArAsset();
31 
32  ArAsset(const ArAsset&) = delete;
33  ArAsset& operator=(const ArAsset&) = delete;
34 
35  /// Returns size of the asset.
36  AR_API
37  virtual size_t GetSize() const = 0;
38 
39  /// Returns a pointer to a buffer with the contents of the asset,
40  /// with size given by GetSize(). Returns an invalid std::shared_ptr
41  /// if the contents could not be retrieved.
42  ///
43  /// The data in the returned buffer must remain valid while there are
44  /// outstanding copies of the returned std::shared_ptr. Note that the
45  /// deleter stored in the std::shared_ptr may contain additional data
46  /// needed to maintain the buffer's validity.
47  AR_API
48  virtual std::shared_ptr<const char> GetBuffer() const = 0;
49 
50  /// Read \p count bytes at \p offset from the beginning of the asset
51  /// into \p buffer. Returns number of bytes read, or 0 on error.
52  ///
53  /// Implementers should range-check calls and return zero for out-of-bounds
54  /// reads.
55  AR_API
56  virtual size_t Read(void* buffer, size_t count, size_t offset) const = 0;
57 
58  /// Returns a read-only FILE* handle and offset for this asset if
59  /// available, or (nullptr, 0) otherwise.
60  ///
61  /// The returned handle must remain valid for the lifetime of this
62  /// ArAsset object. The returned offset is the offset from the beginning
63  /// of the FILE* where the asset's contents begins.
64  ///
65  /// This function is marked unsafe because the handle may wind up
66  /// being used in multiple threads depending on the underlying
67  /// resolver implementation. For instance, a resolver may cache
68  /// and return ArAsset objects with the same FILE* to multiple
69  /// threads.
70  ///
71  /// Clients MUST NOT use this handle with functions that cannot be
72  /// called concurrently on the same file descriptor, e.g. read,
73  /// fread, fseek, etc. See ArchPRead for a function that can be used
74  /// to read data from this handle safely.
75  AR_API
76  virtual std::pair<FILE*, size_t> GetFileUnsafe() const = 0;
77 
78  /// Returns an ArAsset with the contents of this asset detached from
79  /// from this asset's serialized data. External changes to the serialized
80  /// data must not have any effect on the ArAsset returned by this function.
81  ///
82  /// The default implementation returns a new instance of an ArInMemoryAsset
83  /// that reads the entire contents of this asset into a heap-allocated
84  /// buffer.
85  AR_API
86  virtual std::shared_ptr<ArAsset> GetDetachedAsset() const;
87 
88 protected:
89  AR_API
90  ArAsset();
91 };
92 
94 
95 #endif // PXR_USD_AR_ASSET_H
ArAsset & operator=(const ArAsset &)=delete
virtual AR_API std::shared_ptr< ArAsset > GetDetachedAsset() const
Definition: asset.h:27
virtual AR_API std::shared_ptr< const char > GetBuffer() const =0
virtual AR_API size_t GetSize() const =0
Returns size of the asset.
#define AR_API
Definition: api.h:23
GLuint buffer
Definition: glcorearb.h:660
virtual AR_API size_t Read(void *buffer, size_t count, size_t offset) const =0
GLintptr offset
Definition: glcorearb.h:665
virtual AR_API ~ArAsset()
AR_API ArAsset()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
virtual AR_API std::pair< FILE *, size_t > GetFileUnsafe() const =0
GLint GLsizei count
Definition: glcorearb.h:405