BundleResource¶
-
std::ostream &
cppmicroservices::
operator<<
(std::ostream &os, const BundleResource &resource)¶ Streams the
resource
path into the streamos
.
-
class
cppmicroservices::
BundleResource
¶ - #include <cppmicroservices/BundleResource.h>
Represents a resource (text file, image, etc.) embedded in a CppMicroServices bundle.
A
BundleResource
object provides information about a resource (external file) which was embedded into this bundle’s shared library.BundleResource
objects can be obtained be calling Bundle::GetResource or Bundle::FindResources.Example code for retreiving a resource object and reading its contents:
BundleResource objects have value semantics and copies are very inexpensive.// Get this bundle's Bundle object auto bundle = GetBundleContext().GetBundle(); BundleResource resource = bundle.GetResource("config.properties"); if (resource.IsValid()) { // Create a BundleResourceStream object BundleResourceStream resourceStream(resource); // Read the contents line by line std::string line; while (std::getline(resourceStream, line)) { // Process the content std::cout << line << std::endl; } } else { // Error handling }
See also
Public Functions
-
BundleResource
(const BundleResource &resource)¶ Copy constructor.
- Parameters
resource
: The object to be copied.
-
~BundleResource
()¶
-
BundleResource &
operator=
(const BundleResource &resource)¶ Assignment operator.
- Return
- A reference to this BundleResource instance.
- Parameters
resource
: The BundleResource object which is assigned to this instance.
-
bool
operator<
(const BundleResource &resource) const¶ A less then operator using the full resource path as returned by GetResourcePath() to define the ordering.
- Return
true
if this BundleResource object is less thenresource
,false
otherwise.- Parameters
resource
: The object to which this BundleResource object is compared to.
-
bool
operator==
(const BundleResource &resource) const¶ Equality operator for BundleResource objects.
- Return
true
if this BundleResource object is equal toresource
, i.e. they are coming from the same bundle (shared or static) and have an equal resource path,false
otherwise.- Parameters
resource
: The object for testing equality.
-
bool
operator!=
(const BundleResource &resource) const¶ Inequality operator for BundleResource objects.
- Return
- The result of
!(*this == resource)
. - Parameters
resource
: The object for testing inequality.
-
bool
IsValid
() const¶ Tests this BundleResource object for validity.
Invalid BundleResource objects are created by the default constructor or can be returned by the Bundle class if the resource path is not found.
- Return
true
if this BundleReource object is valid and can safely be used,false
otherwise.
-
std::string
GetName
() const¶ Returns the name of the resource, excluding the path.
Example:
BundleResource resource = bundle->GetResource("/data/archive.tar.gz"); std::string name = resource.GetName(); // name = "archive.tar.gz"
- Return
- The resource name.
- See
- GetPath(), GetResourcePath()
-
std::string
GetPath
() const¶ Returns the resource’s path, without the file name.
Example:
BundleResource resource = bundle->GetResource("/data/archive.tar.gz"); std::string path = resource.GetPath(); // path = "/data/"
The path with always begin and end with a forward slash.
- Return
- The resource path without the name.
- See
- GetResourcePath(), GetName() and IsDir()
-
std::string
GetResourcePath
() const¶ Returns the resource path including the file name.
-
std::string
GetBaseName
() const¶ Returns the base name of the resource without the path.
Example:
BundleResource resource = bundle->GetResource("/data/archive.tar.gz"); std::string base = resource.GetBaseName(); // base = "archive"
- Return
- The resource base name.
- See
- GetName(), GetSuffix(), GetCompleteSuffix() and GetCompleteBaseName()
-
std::string
GetCompleteBaseName
() const¶ Returns the complete base name of the resource without the path.
Example:
BundleResource resource = bundle->GetResource("/data/archive.tar.gz"); std::string base = resource.GetCompleteBaseName(); // base = "archive.tar"
- Return
- The resource’s complete base name.
- See
- GetName(), GetSuffix(), GetCompleteSuffix(), and GetBaseName()
-
std::string
GetSuffix
() const¶ Returns the suffix of the resource.
The suffix consists of all characters in the resource name after (but not including) the last ‘.’.
Example:
BundleResource resource = bundle->GetResource("/data/archive.tar.gz"); std::string suffix = resource.GetSuffix(); // suffix = "gz"
- Return
- The resource name suffix.
- See
- GetName(), GetCompleteSuffix(), GetBaseName() and GetCompleteBaseName()
-
std::string
GetCompleteSuffix
() const¶ Returns the complete suffix of the resource.
The suffix consists of all characters in the resource name after (but not including) the first ‘.’.
Example:
BundleResource resource = bundle->GetResource("/data/archive.tar.gz"); std::string suffix = resource.GetCompleteSuffix(); // suffix = "tar.gz"
- Return
- The resource name suffix.
- See
- GetName(), GetSuffix(), GetBaseName(), and GetCompleteBaseName()
-
bool
IsDir
() const¶ Returns
true
if this BundleResource object points to a directory and thus may have child resources.- Return
true
if this object points to a directory,false
otherwise.
-
bool
IsFile
() const¶ Returns
true
if this BundleResource object points to a file resource.- Return
true
if this object points to an embedded file,false
otherwise.
-
std::vector<std::string>
GetChildren
() const¶ Returns a list of resource names which are children of this object.
The returned names are relative to the path of this BundleResource object and may contain file as well as directory entries.
- Return
- A list of child resource names.
-
std::vector<BundleResource>
GetChildResources
() const¶ Returns a list of resource objects which are children of this object.
The returned BundleResource objects may contain files as well as directory resources.
- Return
- A list of child resource objects.
-
int
GetSize
() const¶ Returns the (uncompressed) size of the resource data for this BundleResource object.
- Return
- The uncompressed resource data size.
-
int
GetCompressedSize
() const¶ Returns the compressed size of the resource data for this BundleResource object.
- Return
- The compressed resource data size.
-
time_t
GetLastModified
() const¶ Returns the last modified time of this resource in seconds from the epoch.
- Return
- Last modified time of this resource.
-
uint32_t
GetCrc32
() const¶ Returns the CRC-32 checksum of this resource.
- Return
- CRC-32 checksum of this resource.
Friends
-
friend
gr_bundleresource::::std::hash< BundleResource >
-
-
template<>
structstd::
hash
<cppmicroservices::BundleResource>¶ - #include <cppmicroservices/BundleResource.h>
Hash functor specialization for BundleResource objects.