Loading...
Searching...
No Matches
hueplusplus::ResourceList< Resource, IdT > Class Template Reference

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

`#include <hueplusplus/ResourceList.h>`

Inheritance diagram for hueplusplus::ResourceList< Resource, IdT >:
[legend]

## 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.
| 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.
| ResourceList (const ResourceList &)=delete
| Deleted copy constructor.
ResourceList & | operator= (const ResourceList &)=delete
| Deleted copy assignment.
void | refresh ()
| Refreshes internal state now.
void | setRefreshDuration (std::chrono::steady_clock::duration refreshDuration)
| Sets custom refresh interval for this list and all resources created.
std::vector< Resource > | getAll ()
| Get all resources that exist.
Resource | get (const IdType &id)
| Get resource specified by id.
bool | exists (const IdType &id)
| Checks whether resource with id exists.
bool | exists (const IdType &id) const
| Checks whether resource with id exists.
bool | remove (const IdType &id)
| Removes the resource.

## Protected Member Functions

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

## Static Protected Member Functions

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

## Protected Attributes

std::shared_ptr< APICache > | stateCache
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

  Resource | Resource type that is in the list
  IdT | Type 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

  baseCache | Base cache which holds the parent state, not nullptr
  cacheEntry | Entry name of the list state in the base cache
  refreshDuration | Interval between refreshing the cache
  sharedState | Whether created resources should share the same base cache.
  factory | Optional 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

  commands | HueCommandAPI for requests
  path | Path of the resource list
  refreshDuration | Interval between refreshing the cache
  factory | Optional 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 > && |  | ) |

protected default

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
| ) |  |

inline protected

Constructs resource using factory or constructor, if available.

Exceptions

  HueException | when 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

  id | Identifier of the resource to check

Returns
  true when the resource with given id exists

Exceptions

  std::system_error | when system or socket operations fail
  HueException | when response contains no body
  HueAPIResponseException | when response contains an error
  nlohmann::json::parse_error | when 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

  id | Identifier of the resource to check

Returns
  true when the resource with given id exists

Note
  This will not update the cache

Exceptions

  HueException | when 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

  id | Identifier of the resource

Returns
  The resource matching the id

Exceptions

  std::system_error | when system or socket operations fail
  HueException | when id does not exist
  HueAPIResponseException | when response contains an error
  nlohmann::json::parse_error | when 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_error | when system or socket operations fail
  HueException | when response contains no body
  HueAPIResponseException | when response contains an error
  nlohmann::json::parse_error | when response could not be parsed

## maybeStoi()

template<typename Resource , typename IdT >

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

inline static protected

Calls std::stoi if IdType is int.

## maybeToString()

template<typename Resource , typename IdT >

static std::string hueplusplus::ResourceList< Resource, IdT >::maybeToString | ( | const IdType & | id | ) |

inline static protected

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 > && |  | ) |

protected default

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

  id | Identifier of the resource to remove

Returns
  true on success

Exceptions

  std::system_error | when system or socket operations fail
  HueException | when response contains no body
  HueAPIResponseException | when response contains an error
  nlohmann::json::parse_error | when 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

  refreshDuration | The 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:

- include/hueplusplus/ResourceList.h