1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-30 21:41:46 +00:00

Simplify data source creation, create it directly inside lua userdata

This commit is contained in:
Pavel Labath 2010-02-14 20:36:17 +01:00
parent fa02cdceab
commit 1d6009594c
3 changed files with 12 additions and 16 deletions

View File

@ -59,7 +59,7 @@ namespace conky {
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)
{
// XXX some generic way of reporting errors? NORM_ERR?
@ -67,7 +67,7 @@ namespace conky {
<< "' has been disabled during compilation. Please recompile with '"
<< 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>
std::shared_ptr<data_source_base>
void
simple_numeric_source<T>::factory(lua::state &l, const std::string &name, const T *source)
{
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,

View File

@ -34,15 +34,12 @@
namespace conky {
class data_source_base;
/*
* 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<
std::shared_ptr<data_source_base> (lua::state &l, const std::string &name)
> data_source_factory;
typedef std::function<void (lua::state &l, const std::string &name)> data_source_factory;
/*
* 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");
const T *source;
public:
simple_numeric_source(const std::string &name_, const T *source_)
: data_source_base(name_), source(source_)
{}
public:
static std::shared_ptr<data_source_base>
factory(lua::state &l, const std::string &name, const T *source);
static void factory(lua::state &l, const std::string &name, const T *source);
virtual double get_number() const
{ return *source; }

View File

@ -40,7 +40,7 @@ namespace conky {
if(not l->getmetatable(-2) or not l->rawequal(-1, -2))
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)
@ -59,7 +59,7 @@ namespace conky {
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->setmetatable(-2);
return 1;