Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
#ifndef INCLUDE_HUEPLUSPLUS_UTILS_H
#define INCLUDE_HUEPLUSPLUS_UTILS_H
#include <nlohmann/json.hpp>
namespace hueplusplus
{
namespace utils
{
namespace detail
{
// Forward declaration
template <typename KeyT, typename... Paths>
nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths);
inline nlohmann::json safeGetMemberHelper(const nlohmann::json& json)
{
return json;
}
template <typename KeyT, typename... Paths,
std::enable_if_t<!std::is_integral<std::remove_reference_t<KeyT>>::value>* = nullptr>
nlohmann::json safeGetMemberHelper(const nlohmann::json& json, KeyT&& key, Paths&&... otherPaths)
{
auto memberIt = json.find(std::forward<KeyT>(key));
if (memberIt == json.end())
{
return nullptr;
}
return safeGetMemberHelper(*memberIt, std::forward<Paths>(otherPaths)...);
}
// Needs to be after the other safeGetMemberHelper, otherwise another forward declaration is needed
template <typename... Paths>
nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths)
{
if (!json.is_array() || json.size() <= index)
{
return nullptr;
}
return safeGetMemberHelper(json[index], std::forward<Paths>(otherPaths)...);
}
} // namespace detail
bool validatePUTReply(const std::string& path, const nlohmann::json& request, const nlohmann::json& reply);
bool validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId);
inline bool floatEquals(float lhs, float rhs)
{
return std::abs(lhs - rhs) <= 1E-4f;
}
template <typename... Paths>
nlohmann::json safeGetMember(const nlohmann::json& json, Paths&&... paths)
{
return detail::safeGetMemberHelper(json, std::forward<Paths>(paths)...);
}
} // namespace utils
namespace detail
{
template <typename T>
class MakeCopyable : public T
{
public:
// Make copy constructor and assignment operator public
using T::T;
using T::operator=;
};
} // namespace detail
} // namespace hueplusplus
#endif
hueplusplus::utils::validateReplyForLight
bool validateReplyForLight(const nlohmann::json &request, const nlohmann::json &reply, int lightId)
Definition Utils.cpp:89
hueplusplus::utils::validatePUTReply
bool validatePUTReply(const std::string &path, const nlohmann::json &request, const nlohmann::json &reply)
Function for validating that a request was executed correctly.
Definition Utils.cpp:31
hueplusplus::utils::floatEquals
bool floatEquals(float lhs, float rhs)
Checks equality to 4 decimal places.
Definition Utils.h:81
hueplusplus::utils::safeGetMember
nlohmann::json safeGetMember(const nlohmann::json &json, Paths &&... paths)
Returns the object/array member or null if it does not exist.
Definition Utils.h:92
hueplusplus
Namespace for the hueplusplus library.
Definition Action.h:28