CppMicroServices

C++ Micro Services: ModuleResource Class Reference
ModuleResource Class Reference

Represents a resource (text file, image, etc.) embedded in a CppMicroServices module. More...

Public Member Functions

 ModuleResource ()
 Creates in invalid ModuleResource object. More...
 
 ModuleResource (const ModuleResource &resource)
 Copy constructor. More...
 
 ~ModuleResource ()
 
ModuleResourceoperator= (const ModuleResource &resource)
 Assignment operator. More...
 
bool operator< (const ModuleResource &resource) const
 A less then operator using the full resource path as returned by GetResourcePath() to define the ordering. More...
 
bool operator== (const ModuleResource &resource) const
 Equality operator for ModuleResource objects. More...
 
bool operator!= (const ModuleResource &resource) const
 Inequality operator for ModuleResource objects. More...
 
bool IsValid () const
 Tests this ModuleResource object for validity. More...
 
bool IsCompressed () const
 Returns true if the resource represents a file and the resource data is in a compressed format, false otherwise. More...
 
 operator bool_type () const
 Boolean conversion operator using IsValid(). More...
 
std::string GetName () const
 Returns the name of the resource, excluding the path. More...
 
std::string GetPath () const
 Returns the resource's path, without the file name. More...
 
std::string GetResourcePath () const
 Returns the resource path including the file name. More...
 
std::string GetBaseName () const
 Returns the base name of the resource without the path. More...
 
std::string GetCompleteBaseName () const
 Returns the complete base name of the resource without the path. More...
 
std::string GetSuffix () const
 Returns the suffix of the resource. More...
 
std::string GetCompleteSuffix () const
 Returns the complete suffix of the resource. More...
 
bool IsDir () const
 Returns true if this ModuleResource object points to a directory and thus may have child resources. More...
 
bool IsFile () const
 Returns true if this ModuleResource object points to a file resource. More...
 
std::vector< std::string > GetChildren () const
 Returns a list of resource names which are children of this object. More...
 
int GetSize () const
 Returns the size of the raw embedded data for this ModuleResource object. More...
 
const unsigned char * GetData () const
 Returns a data pointer pointing to the raw data of this ModuleResource object. More...
 

Detailed Description

Represents a resource (text file, image, etc.) embedded in a CppMicroServices module.

A ModuleResource object provides information about a resource (external file) which was embedded into this module's shared library. ModuleResource objects can be obtained be calling Module::GetResource or Module::FindResources.

Example code for retreiving a resource object and reading its contents:

// Get this module's Module object
ModuleResource resource = module->GetResource("config.properties");
if (resource.IsValid())
{
// Create a ModuleResourceStream object
ModuleResourceStream 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
}

ModuleResource objects have value semantics and copies are very inexpensive.

See Also
ModuleResourceStream
The Resources System

Constructor & Destructor Documentation

ModuleResource::ModuleResource ( )

Creates in invalid ModuleResource object.

ModuleResource::ModuleResource ( const ModuleResource resource)

Copy constructor.

Parameters
resourceThe object to be copied.
ModuleResource::~ModuleResource ( )

Member Function Documentation

std::string ModuleResource::GetBaseName ( ) const

Returns the base name of the resource without the path.

Example:

* ModuleResource resource = module->GetResource("/data/archive.tar.gz");
* std::string base = resource.GetBaseName(); // base = "archive"
*
Returns
The resource base name.
See Also
GetName(), GetSuffix(), GetCompleteSuffix() and GetCompleteBaseName()
std::vector<std::string> ModuleResource::GetChildren ( ) const

Returns a list of resource names which are children of this object.

The returned names are relative to the path of this ModuleResource object and may contain duplicates in case of modules which are statically linked into the module from which this object was retreived.

Returns
A list of child resource names.
std::string ModuleResource::GetCompleteBaseName ( ) const

Returns the complete base name of the resource without the path.

Example:

* ModuleResource resource = module->GetResource("/data/archive.tar.gz");
* std::string base = resource.GetCompleteBaseName(); // base = "archive.tar"
*
Returns
The resource's complete base name.
See Also
GetName(), GetSuffix(), GetCompleteSuffix(), and GetBaseName()
std::string ModuleResource::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:

* ModuleResource resource = module->GetResource("/data/archive.tar.gz");
* std::string suffix = resource.GetCompleteSuffix(); // suffix = "tar.gz"
*
Returns
The resource name suffix.
See Also
GetName(), GetSuffix(), GetBaseName(), and GetCompleteBaseName()
const unsigned char* ModuleResource::GetData ( ) const

Returns a data pointer pointing to the raw data of this ModuleResource object.

If the resource is compressed the data returned is compressed and UncompressResourceData() must be used to access the data. If the resource represents a directory 0 is returned.

Returns
A raw pointer to the embedded data, or 0 if the resource is not a file resource.
std::string ModuleResource::GetName ( ) const

Returns the name of the resource, excluding the path.

Example:

* ModuleResource resource = module->GetResource("/data/archive.tar.gz");
* std::string name = resource.GetName(); // name = "archive.tar.gz"
*
Returns
The resource name.
See Also
GetPath(), GetResourcePath()
std::string ModuleResource::GetPath ( ) const

Returns the resource's path, without the file name.

Example:

* ModuleResource resource = module->GetResource("/data/archive.tar.gz");
* std::string path = resource.GetPath(); // path = "/data"
*
Returns
The resource path without the name.
See Also
GetResourcePath(), GetName() and IsDir()
std::string ModuleResource::GetResourcePath ( ) const

Returns the resource path including the file name.

Returns
The resource path including the file name.
See Also
GetPath(), GetName() and IsDir()
int ModuleResource::GetSize ( ) const

Returns the size of the raw embedded data for this ModuleResource object.

Returns
The resource data size.
std::string ModuleResource::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:

* ModuleResource resource = module->GetResource("/data/archive.tar.gz");
* std::string suffix = resource.GetSuffix(); // suffix = "gz"
*
Returns
The resource name suffix.
See Also
GetName(), GetCompleteSuffix(), GetBaseName() and GetCompleteBaseName()
bool ModuleResource::IsCompressed ( ) const

Returns true if the resource represents a file and the resource data is in a compressed format, false otherwise.

Returns
true if the resource data is compressed, false otherwise.
bool ModuleResource::IsDir ( ) const

Returns true if this ModuleResource object points to a directory and thus may have child resources.

Returns
true if this object points to a directory, false otherwise.
bool ModuleResource::IsFile ( ) const

Returns true if this ModuleResource object points to a file resource.

Returns
true if this object points to an embedded file, false otherwise.
bool ModuleResource::IsValid ( ) const

Tests this ModuleResource object for validity.

Invalid ModuleResource objects are created by the default constructor or can be returned by the Module class if the resource path is not found. If a module from which ModuleResource objects have been obtained is un-loaded, these objects become invalid.

Returns
true if this ModuleReource object is valid and can safely be used, false otherwise.
ModuleResource::operator bool_type ( ) const

Boolean conversion operator using IsValid().

bool ModuleResource::operator!= ( const ModuleResource resource) const

Inequality operator for ModuleResource objects.

Parameters
resourceThe object for testing inequality.
Returns
The result of !(*this == resource).
bool ModuleResource::operator< ( const ModuleResource resource) const

A less then operator using the full resource path as returned by GetResourcePath() to define the ordering.

Parameters
resourceThe object to which this ModuleResource object is compared to.
Returns
true if this ModuleResource object is less then resource, false otherwise.
ModuleResource& ModuleResource::operator= ( const ModuleResource resource)

Assignment operator.

Parameters
resourceThe ModuleResource object which is assigned to this instance.
Returns
A reference to this ModuleResource instance.
bool ModuleResource::operator== ( const ModuleResource resource) const

Equality operator for ModuleResource objects.

Parameters
resourceThe object for testing equality.
Returns
true if this ModuleResource object is equal to resource, i.e. they are coming from the same module (shared or static) and have an equal resource path, false otherwise.