From 1d6009594c85e6ff8dcc5dad529a18650abe41c1 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 14 Feb 2010 20:36:17 +0100 Subject: [PATCH] Simplify data source creation, create it directly inside lua userdata --- src/data-source.cc | 8 ++++---- src/data-source.hh | 16 ++++++---------- src/lua-config.cc | 4 ++-- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/data-source.cc b/src/data-source.cc index 0b0238b3..46697838 100644 --- a/src/data-source.cc +++ b/src/data-source.cc @@ -59,7 +59,7 @@ namespace conky { throw std::logic_error("Data source with name '" + name + "' already registered"); } - static std::shared_ptr + 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::factory(l, name, &NaN); + simple_numeric_source::factory(l, name, &NaN); } } @@ -82,11 +82,11 @@ namespace conky { } template - std::shared_ptr + void simple_numeric_source::factory(lua::state &l, const std::string &name, const T *source) { l.pop(); - return std::shared_ptr(new simple_numeric_source(name, source)); + l.createuserdata>(name, source); } register_data_source::register_data_source(const std::string &name, diff --git a/src/data-source.hh b/src/data-source.hh index 11713784..bd72c77a 100644 --- a/src/data-source.hh +++ b/src/data-source.hh @@ -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 (lua::state &l, const std::string &name) - > data_source_factory; + typedef std::function data_source_factory; /* * A base class for all data sources. @@ -76,13 +73,12 @@ namespace conky { static_assert(std::is_convertible::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 - 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; } diff --git a/src/lua-config.cc b/src/lua-config.cc index 455e871e..c1b32a68 100644 --- a/src/lua-config.cc +++ b/src/lua-config.cc @@ -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 *>(l->touserdata(1)); + return *static_cast(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>(v.second(*l, v.first)); + v.second(*l, v.first); l->rawgetfield(lua::REGISTRYINDEX, data_source_metatable); l->setmetatable(-2); return 1;