BundleResource¶
-
std::ostream &operator<<(std::ostream &os, BundleResource const &resource)¶
Streams the
resource
path into the streamos
.
-
class 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:
// 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 }
BundleResource objects have value semantics and copies are very inexpensive.
See also
Public Functions
-
BundleResource(BundleResource const &resource)¶
Copy constructor.
- Parameters:
resource – The object to be copied.
-
~BundleResource() = default¶
-
BundleResource &operator=(BundleResource const &resource) = default¶
Assignment operator.
- Parameters:
resource – The BundleResource object which is assigned to this instance.
- Returns:
A reference to this BundleResource instance.
-
bool operator<(BundleResource const &resource) const¶
A less then operator using the full resource path as returned by GetResourcePath() to define the ordering.
- Parameters:
resource – The object to which this BundleResource object is compared to.
- Returns:
true
if this BundleResource object is less thenresource
,false
otherwise.
-
bool operator==(BundleResource const &resource) const¶
Equality operator for BundleResource objects.
- Parameters:
resource – The object for testing equality.
- Returns:
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.
-
bool operator!=(BundleResource const &resource) const¶
Inequality operator for BundleResource objects.
- Parameters:
resource – The object for testing inequality.
- Returns:
The result of
!(*this == resource)
.
-
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.
- Returns:
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"
See also
- Returns:
The resource name.
-
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.
See also
- Returns:
The resource path without the name.
-
std::string GetResourcePath() const¶
Returns the resource path including the file name.
- 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"
- Returns:
The resource base name.
-
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"
See also
GetName(), GetSuffix(), GetCompleteSuffix(), and GetBaseName()
- Returns:
The resource’s complete base name.
-
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"
- Returns:
The resource name suffix.
-
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"
See also
GetName(), GetSuffix(), GetBaseName(), and GetCompleteBaseName()
- Returns:
The resource name suffix.
-
bool IsDir() const¶
Returns
true
if this BundleResource object points to a directory and thus may have child resources.- Returns:
true
if this object points to a directory,false
otherwise.
-
bool IsFile() const¶
Returns
true
if this BundleResource object points to a file resource.- Returns:
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.
- Returns:
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.
- Returns:
A list of child resource objects.
-
int GetSize() const¶
Returns the (uncompressed) size of the resource data for this BundleResource object.
- Returns:
The uncompressed resource data size.
-
int GetCompressedSize() const¶
Returns the compressed size of the resource data for this BundleResource object.
- Returns:
The compressed resource data size.
-
time_t GetLastModified() const¶
Returns the last modified time of this resource in seconds from the epoch.
- Returns:
Last modified time of this resource.
-
uint32_t GetCrc32() const¶
Returns the CRC-32 checksum of this resource.
- Returns:
CRC-32 checksum of this resource.
Friends
- friend struct ::std::hash< BundleResource >
-
BundleResource(BundleResource const &resource)¶
-
template<>
struct hash<cppmicroservices::BundleResource>¶ - #include <cppmicroservices/BundleResource.h>
Hash functor specialization for BundleResource objects.