hueplusplus  1.0.0
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
hueplusplus::ResourceList< Resource, IdT > Class Template Reference

Handles a list of a certain API resource. More...

#include <hueplusplus/ResourceList.h>

Public Types

using ResourceType = Resource
 
using IdType = IdT
 

Public Member Functions

 ResourceList (std::shared_ptr< APICache > baseCache, const std::string &cacheEntry, std::chrono::steady_clock::duration refreshDuration, bool sharedState=false, const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &factory=nullptr)
 Construct ResourceList using a base cache and optional factory function. More...
 
 ResourceList (const HueCommandAPI &commands, const std::string &path, std::chrono::steady_clock::duration refreshDuration, const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &factory=nullptr)
 Construct ResourceList with a separate cache and optional factory function. More...
 
 ResourceList (const ResourceList &)=delete
 Deleted copy constructor. More...
 
ResourceListoperator= (const ResourceList &)=delete
 Deleted copy assignment. More...
 
void refresh ()
 Refreshes internal state now. More...
 
void setRefreshDuration (std::chrono::steady_clock::duration refreshDuration)
 Sets custom refresh interval for this list and all resources created. More...
 
std::vector< Resource > getAll ()
 Get all resources that exist. More...
 
Resource get (const IdType &id)
 Get resource specified by id. More...
 
bool exists (const IdType &id)
 Checks whether resource with id exists. More...
 
bool exists (const IdType &id) const
 Checks whether resource with id exists. More...
 
bool remove (const IdType &id)
 Removes the resource. More...
 

Protected Member Functions

Resource construct (const IdType &id, const nlohmann::json &state)
 Constructs resource using factory or constructor, if available. More...
 
 ResourceList (ResourceList &&)=default
 Protected defaulted move constructor. More...
 
ResourceListoperator= (ResourceList &&)=default
 Protected defaulted move assignment. More...
 

Static Protected Member Functions

static IdType maybeStoi (const std::string &key)
 Calls std::stoi if IdType is int. More...
 
static std::string maybeToString (const IdType &id)
 Calls std::to_string if IdType is int. More...
 

Protected Attributes

std::shared_ptr< APICachestateCache
 
std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> factory
 
std::string path
 
bool sharedState
 

Detailed Description

template<typename Resource, typename IdT>
class hueplusplus::ResourceList< Resource, IdT >

Handles a list of a certain API resource.

Template Parameters
ResourceResource type that is in the list
IdTType of the resource id. int or std::string

The resources are assumed to be in an object with ids as keys. The Resource class needs a constructor that accepts id, HueCommandAPI, refreshDuration and state; otherwise a factory function needs to be provided that takes id, state and a base cache that is null when shared state is disabled.

Member Typedef Documentation

◆ IdType

template<typename Resource, typename IdT>
using hueplusplus::ResourceList< Resource, IdT >::IdType = IdT

◆ ResourceType

template<typename Resource, typename IdT>
using hueplusplus::ResourceList< Resource, IdT >::ResourceType = Resource

Constructor & Destructor Documentation

◆ ResourceList() [1/4]

template<typename Resource, typename IdT>
hueplusplus::ResourceList< Resource, IdT >::ResourceList ( std::shared_ptr< APICache baseCache,
const std::string &  cacheEntry,
std::chrono::steady_clock::duration  refreshDuration,
bool  sharedState = false,
const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &  factory = nullptr 
)
inline

Construct ResourceList using a base cache and optional factory function.

Parameters
baseCacheBase cache which holds the parent state, not nullptr
cacheEntryEntry name of the list state in the base cache
refreshDurationInterval between refreshing the cache
sharedStateWhether created resources should share the same base cache.
factoryOptional factory function to create Resources. Necessary if Resource is not constructible as described above.

◆ ResourceList() [2/4]

template<typename Resource, typename IdT>
hueplusplus::ResourceList< Resource, IdT >::ResourceList ( const HueCommandAPI commands,
const std::string &  path,
std::chrono::steady_clock::duration  refreshDuration,
const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &  factory = nullptr 
)
inline

Construct ResourceList with a separate cache and optional factory function.

Parameters
commandsHueCommandAPI for requests
pathPath of the resource list
refreshDurationInterval between refreshing the cache
factoryOptional factory function to create Resources. Necessary if Resource is not constructible as described above.

◆ ResourceList() [3/4]

template<typename Resource, typename IdT>
hueplusplus::ResourceList< Resource, IdT >::ResourceList ( const ResourceList< Resource, IdT > &  )
delete

Deleted copy constructor.

◆ ResourceList() [4/4]

template<typename Resource, typename IdT>
hueplusplus::ResourceList< Resource, IdT >::ResourceList ( ResourceList< Resource, IdT > &&  )
protecteddefault

Protected defaulted move constructor.

Member Function Documentation

◆ construct()

template<typename Resource, typename IdT>
Resource hueplusplus::ResourceList< Resource, IdT >::construct ( const IdType id,
const nlohmann::json &  state 
)
inlineprotected

Constructs resource using factory or constructor, if available.

Exceptions
HueExceptionwhen factory is nullptr and Resource cannot be constructed as specified above.

◆ exists() [1/2]

template<typename Resource, typename IdT>
bool hueplusplus::ResourceList< Resource, IdT >::exists ( const IdType id)
inline

Checks whether resource with id exists.

Parameters
idIdentifier of the resource to check
Returns
true when the resource with given id exists
Exceptions
std::system_errorwhen system or socket operations fail
HueExceptionwhen response contains no body
HueAPIResponseExceptionwhen response contains an error
nlohmann::json::parse_errorwhen response could not be parsed

◆ exists() [2/2]

template<typename Resource, typename IdT>
bool hueplusplus::ResourceList< Resource, IdT >::exists ( const IdType id) const
inline

Checks whether resource with id exists.

Parameters
idIdentifier of the resource to check
Returns
true when the resource with given id exists
Note
This will not update the cache
Exceptions
HueExceptionwhen the cache is empty

◆ get()

template<typename Resource, typename IdT>
Resource hueplusplus::ResourceList< Resource, IdT >::get ( const IdType id)
inline

Get resource specified by id.

Parameters
idIdentifier of the resource
Returns
The resource matching the id
Exceptions
std::system_errorwhen system or socket operations fail
HueExceptionwhen id does not exist
HueAPIResponseExceptionwhen response contains an error
nlohmann::json::parse_errorwhen response could not be parsed

◆ getAll()

template<typename Resource, typename IdT>
std::vector<Resource> hueplusplus::ResourceList< Resource, IdT >::getAll ( )
inline

Get all resources that exist.

Returns
A vector of references to every Resource
Exceptions
std::system_errorwhen system or socket operations fail
HueExceptionwhen response contains no body
HueAPIResponseExceptionwhen response contains an error
nlohmann::json::parse_errorwhen response could not be parsed

◆ maybeStoi()

template<typename Resource, typename IdT>
static IdType hueplusplus::ResourceList< Resource, IdT >::maybeStoi ( const std::string &  key)
inlinestaticprotected

Calls std::stoi if IdType is int.

◆ maybeToString()

template<typename Resource, typename IdT>
static std::string hueplusplus::ResourceList< Resource, IdT >::maybeToString ( const IdType id)
inlinestaticprotected

Calls std::to_string if IdType is int.

◆ operator=() [1/2]

template<typename Resource, typename IdT>
ResourceList& hueplusplus::ResourceList< Resource, IdT >::operator= ( const ResourceList< Resource, IdT > &  )
delete

Deleted copy assignment.

◆ operator=() [2/2]

template<typename Resource, typename IdT>
ResourceList& hueplusplus::ResourceList< Resource, IdT >::operator= ( ResourceList< Resource, IdT > &&  )
protecteddefault

Protected defaulted move assignment.

◆ refresh()

template<typename Resource, typename IdT>
void hueplusplus::ResourceList< Resource, IdT >::refresh ( )
inline

Refreshes internal state now.

◆ remove()

template<typename Resource, typename IdT>
bool hueplusplus::ResourceList< Resource, IdT >::remove ( const IdType id)
inline

Removes the resource.

Parameters
idIdentifier of the resource to remove
Returns
true on success
Exceptions
std::system_errorwhen system or socket operations fail
HueExceptionwhen response contains no body
HueAPIResponseExceptionwhen response contains an error
nlohmann::json::parse_errorwhen response could not be parsed

If successful, invalidates references to the Resource removed.

◆ setRefreshDuration()

template<typename Resource, typename IdT>
void hueplusplus::ResourceList< Resource, IdT >::setRefreshDuration ( std::chrono::steady_clock::duration  refreshDuration)
inline

Sets custom refresh interval for this list and all resources created.

Parameters
refreshDurationThe new minimum duration between refreshes. May be 0 or c_refreshNever.

Member Data Documentation

◆ factory

template<typename Resource, typename IdT>
std::function<Resource(IdType, const nlohmann::json&, const std::shared_ptr<APICache>&)> hueplusplus::ResourceList< Resource, IdT >::factory
protected

◆ path

template<typename Resource, typename IdT>
std::string hueplusplus::ResourceList< Resource, IdT >::path
protected

◆ sharedState

template<typename Resource, typename IdT>
bool hueplusplus::ResourceList< Resource, IdT >::sharedState
protected

◆ stateCache

template<typename Resource, typename IdT>
std::shared_ptr<APICache> hueplusplus::ResourceList< Resource, IdT >::stateCache
protected

The documentation for this class was generated from the following file: