mirror of
https://github.com/Llewellynvdm/conky.git
synced 2025-01-02 14:31:57 +00:00
Simplify data source creation, create it directly inside lua userdata
This commit is contained in:
parent
fa02cdceab
commit
1d6009594c
@ -59,7 +59,7 @@ namespace conky {
|
|||||||
throw std::logic_error("Data source with name '" + name + "' already registered");
|
throw std::logic_error("Data source with name '" + name + "' already registered");
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::shared_ptr<data_source_base>
|
static void
|
||||||
disabled_source_factory(lua::state &l, const std::string &name, const std::string &setting)
|
disabled_source_factory(lua::state &l, const std::string &name, const std::string &setting)
|
||||||
{
|
{
|
||||||
// XXX some generic way of reporting errors? NORM_ERR?
|
// XXX some generic way of reporting errors? NORM_ERR?
|
||||||
@ -67,7 +67,7 @@ namespace conky {
|
|||||||
<< "' has been disabled during compilation. Please recompile with '"
|
<< "' has been disabled during compilation. Please recompile with '"
|
||||||
<< setting << "'" << std::endl;
|
<< setting << "'" << std::endl;
|
||||||
|
|
||||||
return simple_numeric_source<float>::factory(l, name, &NaN);
|
simple_numeric_source<float>::factory(l, name, &NaN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,11 +82,11 @@ namespace conky {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::shared_ptr<data_source_base>
|
void
|
||||||
simple_numeric_source<T>::factory(lua::state &l, const std::string &name, const T *source)
|
simple_numeric_source<T>::factory(lua::state &l, const std::string &name, const T *source)
|
||||||
{
|
{
|
||||||
l.pop();
|
l.pop();
|
||||||
return std::shared_ptr<simple_numeric_source>(new simple_numeric_source(name, source));
|
l.createuserdata<simple_numeric_source<T>>(name, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
register_data_source::register_data_source(const std::string &name,
|
register_data_source::register_data_source(const std::string &name,
|
||||||
|
@ -34,15 +34,12 @@
|
|||||||
|
|
||||||
namespace conky {
|
namespace conky {
|
||||||
|
|
||||||
class data_source_base;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Recieves a lua table on the stack and the name the object was registered with. It should
|
* Recieves a lua table on the stack and the name the object was registered with. It should
|
||||||
* pop the table after consuming it and return the data source.
|
* pop the table after consuming it. The result should be pushed on the stack as lua userdata
|
||||||
|
* containing (a subclass of) data_source_base.
|
||||||
*/
|
*/
|
||||||
typedef std::function<
|
typedef std::function<void (lua::state &l, const std::string &name)> data_source_factory;
|
||||||
std::shared_ptr<data_source_base> (lua::state &l, const std::string &name)
|
|
||||||
> data_source_factory;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A base class for all data sources.
|
* A base class for all data sources.
|
||||||
@ -76,13 +73,12 @@ namespace conky {
|
|||||||
static_assert(std::is_convertible<T, double>::value, "T must be convertible to double");
|
static_assert(std::is_convertible<T, double>::value, "T must be convertible to double");
|
||||||
|
|
||||||
const T *source;
|
const T *source;
|
||||||
|
public:
|
||||||
simple_numeric_source(const std::string &name_, const T *source_)
|
simple_numeric_source(const std::string &name_, const T *source_)
|
||||||
: data_source_base(name_), source(source_)
|
: data_source_base(name_), source(source_)
|
||||||
{}
|
{}
|
||||||
public:
|
|
||||||
static std::shared_ptr<data_source_base>
|
static void factory(lua::state &l, const std::string &name, const T *source);
|
||||||
factory(lua::state &l, const std::string &name, const T *source);
|
|
||||||
|
|
||||||
virtual double get_number() const
|
virtual double get_number() const
|
||||||
{ return *source; }
|
{ return *source; }
|
||||||
|
@ -40,7 +40,7 @@ namespace conky {
|
|||||||
if(not l->getmetatable(-2) or not l->rawequal(-1, -2))
|
if(not l->getmetatable(-2) or not l->rawequal(-1, -2))
|
||||||
throw std::runtime_error("Invalid parameter");
|
throw std::runtime_error("Invalid parameter");
|
||||||
|
|
||||||
return **static_cast<std::shared_ptr<data_source_base> *>(l->touserdata(1));
|
return *static_cast<data_source_base *>(l->touserdata(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
int data_source_asnumber(lua::state *l)
|
int data_source_asnumber(lua::state *l)
|
||||||
@ -59,7 +59,7 @@ namespace conky {
|
|||||||
|
|
||||||
int create_data_source(lua::state *l, const data_sources_t::value_type &v)
|
int create_data_source(lua::state *l, const data_sources_t::value_type &v)
|
||||||
{
|
{
|
||||||
l->createuserdata<std::shared_ptr<data_source_base>>(v.second(*l, v.first));
|
v.second(*l, v.first);
|
||||||
l->rawgetfield(lua::REGISTRYINDEX, data_source_metatable);
|
l->rawgetfield(lua::REGISTRYINDEX, data_source_metatable);
|
||||||
l->setmetatable(-2);
|
l->setmetatable(-2);
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user