p26001-store-acs-soc-fw-app/util/Broadcast.h
2026-09-28 21:33:04 +08:00

117 lines
3.2 KiB
C++

#ifndef C81E1B2C_54AF_4E7D_800B_F21E0DACFD8B
#define C81E1B2C_54AF_4E7D_800B_F21E0DACFD8B
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <unordered_map>
#include <vector>
#include <Poco/Format.h>
#include <Poco/SharedPtr.h>
#include <Poco/Any.h>
namespace Util
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class Intent
{
public:
using Ptr = Poco::SharedPtr<Intent>;
template <class T> Intent& putExtra(const std::string& key, const T& value);
Intent& putExtra(const std::string& key, const char* value);
template <class T> const T getExtra(const std::string& key) const;
const std::string& getAction() const;
void setAction(const std::string& action);
private:
using Value = Poco::Any;
using Values = std::unordered_map<std::string, Value>;
Values _values;
std::string _action;
};
template <class T> inline Intent& Intent::putExtra(const std::string& key, const T& value)
{
_values[key] = value;
return *this;
}
template <class T> inline const T Intent::getExtra(const std::string& key) const
{
if (_values.find(key) == _values.end())
throw Poco::NotFoundException(Poco::format("Intent: Can't find value: %s", key), -ENODATA);
return Poco::AnyCast<T>(_values.at(key));
}
inline const std::string& Intent::getAction() const
{
return _action;
}
inline void Intent::setAction(const std::string& action)
{
_action = action;
}
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class IntentFilter
{
public:
using Ptr = Poco::SharedPtr<IntentFilter>;
void addAction(const std::string& action);
bool match(Intent::Ptr pIntent) const;
private:
std::vector<std::string> _actions;
};
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class BroadcastReceiver
{
public:
virtual void onReceive(Intent::Ptr pIntent) = 0;
};
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class BroadcastSender
{
public:
void sendBroadcast(Intent::Ptr pIntent);
};
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class BroadcastReceiverRegistry
{
public:
void registerReceiver(BroadcastReceiver& receiver, IntentFilter::Ptr pFilter);
void unregisterReceiver(BroadcastReceiver& receiver);
};
}
#endif