From 33a8b31afd92bf6c51b665fa95cb3662e8e60038 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 27 Jan 2015 02:41:29 +0000 Subject: Move some bits from lib into more focused parts --- p2pvr/daemon/Jamfile.jam | 1 + p2pvr/daemon/containerIterator.h | 40 +++++++++++++++++ p2pvr/daemon/objectRowState.cpp | 89 ++++++++++++++++++++++++++++++++++++++ p2pvr/daemon/objectRowState.h | 48 ++++++++++++++++++++ p2pvr/daemon/resources.h | 12 +++++ p2pvr/daemon/unittests/Jamfile.jam | 2 +- p2pvr/daemonbase/fileSink.cpp | 27 ++++++++++++ p2pvr/daemonbase/fileSink.h | 21 +++++++++ p2pvr/dvb/bindSiParserHandler.h | 25 +++++++++++ p2pvr/lib/bindSiParserHandler.h | 25 ----------- p2pvr/lib/containerIterator.h | 40 ----------------- p2pvr/lib/fileSink.cpp | 27 ------------ p2pvr/lib/fileSink.h | 21 --------- p2pvr/lib/objectRowState.cpp | 89 -------------------------------------- p2pvr/lib/objectRowState.h | 48 -------------------- p2pvr/lib/resources.h | 12 ----- p2pvr/lib/serviceStreamer.cpp | 1 - 17 files changed, 264 insertions(+), 264 deletions(-) create mode 100644 p2pvr/daemon/containerIterator.h create mode 100644 p2pvr/daemon/objectRowState.cpp create mode 100644 p2pvr/daemon/objectRowState.h create mode 100644 p2pvr/daemon/resources.h create mode 100644 p2pvr/daemonbase/fileSink.cpp create mode 100644 p2pvr/daemonbase/fileSink.h create mode 100644 p2pvr/dvb/bindSiParserHandler.h delete mode 100644 p2pvr/lib/bindSiParserHandler.h delete mode 100644 p2pvr/lib/containerIterator.h delete mode 100644 p2pvr/lib/fileSink.cpp delete mode 100644 p2pvr/lib/fileSink.h delete mode 100644 p2pvr/lib/objectRowState.cpp delete mode 100644 p2pvr/lib/objectRowState.h delete mode 100644 p2pvr/lib/resources.h diff --git a/p2pvr/daemon/Jamfile.jam b/p2pvr/daemon/Jamfile.jam index e9c7327..8a9bc7f 100644 --- a/p2pvr/daemon/Jamfile.jam +++ b/p2pvr/daemon/Jamfile.jam @@ -22,6 +22,7 @@ lib p2pvrdaemon : ../daemonbase//p2pvrdaemonbase ../../libtmdb//tmdb slicer + . : : . ../ice//p2pvrice diff --git a/p2pvr/daemon/containerIterator.h b/p2pvr/daemon/containerIterator.h new file mode 100644 index 0000000..14c339d --- /dev/null +++ b/p2pvr/daemon/containerIterator.h @@ -0,0 +1,40 @@ +#ifndef CONTAINERITERATOR_H +#define CONTAINERITERATOR_H + +#include +#include "objectRowState.h" + +template +class ContainerIterator : public IHaveSubTasks { + public: + ContainerIterator(const T * con, const SelectedColumns & sc) : + SourceObject(__PRETTY_FUNCTION__), + IHaveSubTasks(NULL), + binder(boost::bind(&BindColumns, _1, sc, _2)), + container(con) + { + } + + void execute(ExecContext * ec) const + { + ObjectRowStateTmpl rs; + for (const auto & i : *container) { + binder(rs, i); + rs.process(boost::bind(&ContainerIterator::executeChildren, this, ec)); + } + } + + private: + boost::function binder; + const T * container; + + void executeChildren(ExecContext * ec) const + { + for (const Tasks::value_type & sq : normal) { + sq->execute(ec); + } + } +}; + +#endif + diff --git a/p2pvr/daemon/objectRowState.cpp b/p2pvr/daemon/objectRowState.cpp new file mode 100644 index 0000000..89e5ac8 --- /dev/null +++ b/p2pvr/daemon/objectRowState.cpp @@ -0,0 +1,89 @@ +#include "objectRowState.h" +#include + +const std::string md_mergeKey("merge:key"); +const std::string md_mergeOmit("merge:omit"); + +bool +mdHasFlag(const std::list & md, const std::string & flag) +{ + return std::find(md.begin(), md.end(), flag) != md.end(); +} + +SelectedColumns +CreateColumns(Slicer::ModelPartPtr mp, const ColumnCreator & cc) +{ + SelectedColumns cols; + mp->OnEachChild([cc,&cols](const std::string & name, Slicer::ModelPartPtr, Slicer::HookCommonPtr h) { + auto & md = h->GetMetadata(); + auto omitted = mdHasFlag(md, md_mergeOmit); + cols.push_back(!omitted); + if (omitted) return; + cc(name, mdHasFlag(md, md_mergeKey)); + }); + return cols; +} + +class SqlTarget : public Slicer::ValueTarget, + public Slicer::TValueTarget, + public Slicer::TValueTarget { + public: + SqlTarget(VariableType & v) : vt(v) {} + +#define BIND(Type) \ + virtual void get(const Type & t) const override { vt = t; } + BIND(bool); + BIND(::Ice::Byte); + BIND(::Ice::Short); + BIND(::Ice::Int); + BIND(::Ice::Long); + BIND(::Ice::Float); + BIND(::Ice::Double); + BIND(::std::string); + BIND(boost::posix_time::ptime); + BIND(boost::posix_time::time_duration); + + private: + VariableType & vt; +}; +typedef IceUtil::Handle SqlTargetPtr; + +void +BindColumns(Slicer::ModelPartPtr mp, const SelectedColumns & sc, RowState & rs) +{ + unsigned int field = 0; + mp->OnEachChild([&rs,&field,&sc](const std::string &, Slicer::ModelPartPtr mp, Slicer::HookCommonPtr) { + if (!sc[field]) return; + if (mp && mp->HasValue()) { + SqlTargetPtr st = new SqlTarget(rs.fields[field++]); + mp->GetValue(st); + } + else { + rs.fields[field++] = Null(); + } + }); +} + +ObjectRowState::ObjectRowState(const ColumnSpecifier & cs) : + columns(ColumnCreatorHelper(cs)) +{ + fields.resize(columns.size()); +} + +const Columns & +ObjectRowState::getColumns() const +{ + return columns; +} + +Columns +ObjectRowState::ColumnCreatorHelper(const ColumnSpecifier & cs) +{ + int index = 0; + Columns columns; + cs([&columns, &index](const std::string & name, bool) { + columns.insert(new Column(index++, name)); + }); + return columns; +} + diff --git a/p2pvr/daemon/objectRowState.h b/p2pvr/daemon/objectRowState.h new file mode 100644 index 0000000..3e3fd2f --- /dev/null +++ b/p2pvr/daemon/objectRowState.h @@ -0,0 +1,48 @@ +#ifndef OBJECT_ROW_STATE_H +#define OBJECT_ROW_STATE_H + +#include +#include +#include +#include + +typedef boost::function ColumnCreator; +typedef boost::function ColumnSpecifier; +typedef std::vector SelectedColumns; + +void BindColumns(Slicer::ModelPartPtr, const SelectedColumns &, RowState &); + +template +void BindColumns(RowState & rs, const SelectedColumns & sc, const V & v) +{ + BindColumns(Slicer::ModelPartFor(const_cast(v)), sc, rs); +} + +SelectedColumns CreateColumns(Slicer::ModelPartPtr, const ColumnCreator &); + +template +SelectedColumns CreateColumns(const ColumnCreator & cc) +{ + V v; + return CreateColumns(Slicer::ModelPartFor(v), cc); +} + +class ObjectRowState : public RowState { + public: + ObjectRowState(const ColumnSpecifier & cs); + const Columns & getColumns() const; + + private: + static Columns ColumnCreatorHelper(const ColumnSpecifier & cs); + + Columns columns; +}; + +template +class ObjectRowStateTmpl : public ObjectRowState { + public: + ObjectRowStateTmpl(const ColumnSpecifier & cs = CreateColumns) : ObjectRowState(cs) {} +}; + +#endif + diff --git a/p2pvr/daemon/resources.h b/p2pvr/daemon/resources.h new file mode 100644 index 0000000..18b1de2 --- /dev/null +++ b/p2pvr/daemon/resources.h @@ -0,0 +1,12 @@ +#ifndef RESOURCES_H +#define RESOURCES_H + +#define ResourceString(name, resource) \ +extern "C" { \ + extern char resource##_start, resource##_end;\ + extern unsigned int resource##_len; \ +} \ +static const std::string name(&resource##_start, &resource##_end); + +#endif + diff --git a/p2pvr/daemon/unittests/Jamfile.jam b/p2pvr/daemon/unittests/Jamfile.jam index d95db07..28d4fbd 100644 --- a/p2pvr/daemon/unittests/Jamfile.jam +++ b/p2pvr/daemon/unittests/Jamfile.jam @@ -26,7 +26,7 @@ run : : : BOOST_TEST_DYN_LINK ../..//boost_utf - ../../lib//p2pvrlib + .. : testEmbedding ; run diff --git a/p2pvr/daemonbase/fileSink.cpp b/p2pvr/daemonbase/fileSink.cpp new file mode 100644 index 0000000..dade921 --- /dev/null +++ b/p2pvr/daemonbase/fileSink.cpp @@ -0,0 +1,27 @@ +#include +#include "fileSink.h" + +FileSink::FileSink(const boost::filesystem::path & path) : + file(fopen(path.string().c_str(), "w")) +{ +} + +FileSink::FileSink(int fd) : + file(fdopen(fd, "w")) +{ +} + +FileSink::~FileSink() +{ + if (file) { + fclose(file); + } +} + +bool +FileSink::NewData(const P2PVR::Data & data, const Ice::Current &) +{ + fwrite(&data.front(), data.size(), 1, file); + return false; +} + diff --git a/p2pvr/daemonbase/fileSink.h b/p2pvr/daemonbase/fileSink.h new file mode 100644 index 0000000..8c7594b --- /dev/null +++ b/p2pvr/daemonbase/fileSink.h @@ -0,0 +1,21 @@ +#ifndef FILESINK_H +#define FILESINK_H + +#include +#include +#include + +class FileSink : public P2PVR::RawDataClient { + public: + FileSink(const boost::filesystem::path & path); + FileSink(int fd); + ~FileSink(); + + bool NewData(const P2PVR::Data & data, const Ice::Current &); + + private: + FILE * const file; +}; + +#endif + diff --git a/p2pvr/dvb/bindSiParserHandler.h b/p2pvr/dvb/bindSiParserHandler.h new file mode 100644 index 0000000..a4813c2 --- /dev/null +++ b/p2pvr/dvb/bindSiParserHandler.h @@ -0,0 +1,25 @@ +#ifndef BINDSIPARSERHANDLER_H +#define BINDSIPARSERHANDLER_H + +#include + +template +class BindSiParserHandler : public Base { + public: + typedef boost::function Callback; + BindSiParserHandler(const Callback & cb) : + callBack(cb) + { + } + + bool HandleTable(SIObject siObject) + { + return callBack(siObject); + } + + private: + const Callback callBack; +}; + +#endif + diff --git a/p2pvr/lib/bindSiParserHandler.h b/p2pvr/lib/bindSiParserHandler.h deleted file mode 100644 index a4813c2..0000000 --- a/p2pvr/lib/bindSiParserHandler.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef BINDSIPARSERHANDLER_H -#define BINDSIPARSERHANDLER_H - -#include - -template -class BindSiParserHandler : public Base { - public: - typedef boost::function Callback; - BindSiParserHandler(const Callback & cb) : - callBack(cb) - { - } - - bool HandleTable(SIObject siObject) - { - return callBack(siObject); - } - - private: - const Callback callBack; -}; - -#endif - diff --git a/p2pvr/lib/containerIterator.h b/p2pvr/lib/containerIterator.h deleted file mode 100644 index 14c339d..0000000 --- a/p2pvr/lib/containerIterator.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef CONTAINERITERATOR_H -#define CONTAINERITERATOR_H - -#include -#include "objectRowState.h" - -template -class ContainerIterator : public IHaveSubTasks { - public: - ContainerIterator(const T * con, const SelectedColumns & sc) : - SourceObject(__PRETTY_FUNCTION__), - IHaveSubTasks(NULL), - binder(boost::bind(&BindColumns, _1, sc, _2)), - container(con) - { - } - - void execute(ExecContext * ec) const - { - ObjectRowStateTmpl rs; - for (const auto & i : *container) { - binder(rs, i); - rs.process(boost::bind(&ContainerIterator::executeChildren, this, ec)); - } - } - - private: - boost::function binder; - const T * container; - - void executeChildren(ExecContext * ec) const - { - for (const Tasks::value_type & sq : normal) { - sq->execute(ec); - } - } -}; - -#endif - diff --git a/p2pvr/lib/fileSink.cpp b/p2pvr/lib/fileSink.cpp deleted file mode 100644 index dade921..0000000 --- a/p2pvr/lib/fileSink.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include "fileSink.h" - -FileSink::FileSink(const boost::filesystem::path & path) : - file(fopen(path.string().c_str(), "w")) -{ -} - -FileSink::FileSink(int fd) : - file(fdopen(fd, "w")) -{ -} - -FileSink::~FileSink() -{ - if (file) { - fclose(file); - } -} - -bool -FileSink::NewData(const P2PVR::Data & data, const Ice::Current &) -{ - fwrite(&data.front(), data.size(), 1, file); - return false; -} - diff --git a/p2pvr/lib/fileSink.h b/p2pvr/lib/fileSink.h deleted file mode 100644 index 8c7594b..0000000 --- a/p2pvr/lib/fileSink.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef FILESINK_H -#define FILESINK_H - -#include -#include -#include - -class FileSink : public P2PVR::RawDataClient { - public: - FileSink(const boost::filesystem::path & path); - FileSink(int fd); - ~FileSink(); - - bool NewData(const P2PVR::Data & data, const Ice::Current &); - - private: - FILE * const file; -}; - -#endif - diff --git a/p2pvr/lib/objectRowState.cpp b/p2pvr/lib/objectRowState.cpp deleted file mode 100644 index 89e5ac8..0000000 --- a/p2pvr/lib/objectRowState.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "objectRowState.h" -#include - -const std::string md_mergeKey("merge:key"); -const std::string md_mergeOmit("merge:omit"); - -bool -mdHasFlag(const std::list & md, const std::string & flag) -{ - return std::find(md.begin(), md.end(), flag) != md.end(); -} - -SelectedColumns -CreateColumns(Slicer::ModelPartPtr mp, const ColumnCreator & cc) -{ - SelectedColumns cols; - mp->OnEachChild([cc,&cols](const std::string & name, Slicer::ModelPartPtr, Slicer::HookCommonPtr h) { - auto & md = h->GetMetadata(); - auto omitted = mdHasFlag(md, md_mergeOmit); - cols.push_back(!omitted); - if (omitted) return; - cc(name, mdHasFlag(md, md_mergeKey)); - }); - return cols; -} - -class SqlTarget : public Slicer::ValueTarget, - public Slicer::TValueTarget, - public Slicer::TValueTarget { - public: - SqlTarget(VariableType & v) : vt(v) {} - -#define BIND(Type) \ - virtual void get(const Type & t) const override { vt = t; } - BIND(bool); - BIND(::Ice::Byte); - BIND(::Ice::Short); - BIND(::Ice::Int); - BIND(::Ice::Long); - BIND(::Ice::Float); - BIND(::Ice::Double); - BIND(::std::string); - BIND(boost::posix_time::ptime); - BIND(boost::posix_time::time_duration); - - private: - VariableType & vt; -}; -typedef IceUtil::Handle SqlTargetPtr; - -void -BindColumns(Slicer::ModelPartPtr mp, const SelectedColumns & sc, RowState & rs) -{ - unsigned int field = 0; - mp->OnEachChild([&rs,&field,&sc](const std::string &, Slicer::ModelPartPtr mp, Slicer::HookCommonPtr) { - if (!sc[field]) return; - if (mp && mp->HasValue()) { - SqlTargetPtr st = new SqlTarget(rs.fields[field++]); - mp->GetValue(st); - } - else { - rs.fields[field++] = Null(); - } - }); -} - -ObjectRowState::ObjectRowState(const ColumnSpecifier & cs) : - columns(ColumnCreatorHelper(cs)) -{ - fields.resize(columns.size()); -} - -const Columns & -ObjectRowState::getColumns() const -{ - return columns; -} - -Columns -ObjectRowState::ColumnCreatorHelper(const ColumnSpecifier & cs) -{ - int index = 0; - Columns columns; - cs([&columns, &index](const std::string & name, bool) { - columns.insert(new Column(index++, name)); - }); - return columns; -} - diff --git a/p2pvr/lib/objectRowState.h b/p2pvr/lib/objectRowState.h deleted file mode 100644 index 3e3fd2f..0000000 --- a/p2pvr/lib/objectRowState.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef OBJECT_ROW_STATE_H -#define OBJECT_ROW_STATE_H - -#include -#include -#include -#include - -typedef boost::function ColumnCreator; -typedef boost::function ColumnSpecifier; -typedef std::vector SelectedColumns; - -void BindColumns(Slicer::ModelPartPtr, const SelectedColumns &, RowState &); - -template -void BindColumns(RowState & rs, const SelectedColumns & sc, const V & v) -{ - BindColumns(Slicer::ModelPartFor(const_cast(v)), sc, rs); -} - -SelectedColumns CreateColumns(Slicer::ModelPartPtr, const ColumnCreator &); - -template -SelectedColumns CreateColumns(const ColumnCreator & cc) -{ - V v; - return CreateColumns(Slicer::ModelPartFor(v), cc); -} - -class ObjectRowState : public RowState { - public: - ObjectRowState(const ColumnSpecifier & cs); - const Columns & getColumns() const; - - private: - static Columns ColumnCreatorHelper(const ColumnSpecifier & cs); - - Columns columns; -}; - -template -class ObjectRowStateTmpl : public ObjectRowState { - public: - ObjectRowStateTmpl(const ColumnSpecifier & cs = CreateColumns) : ObjectRowState(cs) {} -}; - -#endif - diff --git a/p2pvr/lib/resources.h b/p2pvr/lib/resources.h deleted file mode 100644 index 18b1de2..0000000 --- a/p2pvr/lib/resources.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef RESOURCES_H -#define RESOURCES_H - -#define ResourceString(name, resource) \ -extern "C" { \ - extern char resource##_start, resource##_end;\ - extern unsigned int resource##_len; \ -} \ -static const std::string name(&resource##_start, &resource##_end); - -#endif - diff --git a/p2pvr/lib/serviceStreamer.cpp b/p2pvr/lib/serviceStreamer.cpp index 99a204a..bd7c04c 100644 --- a/p2pvr/lib/serviceStreamer.cpp +++ b/p2pvr/lib/serviceStreamer.cpp @@ -1,7 +1,6 @@ #include #include "serviceStreamer.h" #include -#include "fileSink.h" #include "bindSiParserHandler.h" ServiceStreamer::ServiceStreamer(int sid, P2PVR::RawDataClientPrx t, const Ice::CommunicatorPtr & ic, const Ice::ObjectAdapterPtr & a) : -- cgit v1.2.3