diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a39f45b..b2ce1c7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ cmake_minimum_required (VERSION 3.1) ######### cmake_policy( SET CMP0048 NEW ) # version in project() -project( Nymph VERSION 1.4.7 ) +project( Nymph VERSION 1.4.10 ) list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/Scarab/cmake ) include( PackageBuilder ) @@ -25,8 +25,8 @@ include( Nymph ) # Nymph options # ################# -# require C++11 -set_option( USE_CPP11 TRUE ) +# require C++17 +set_option( USE_CPP17 TRUE ) # Build the Nymph executable (also requires Nymph_ENABLE_EXECUTABLES) option( Nymph_BUILD_NYMPH_EXE "Flag to build the Nymph executable; also requires Nymph_ENABLE_EXECUTABLES" ON ) diff --git a/Executables/Validation/CMakeLists.txt b/Executables/Validation/CMakeLists.txt index 5e5f9cf7..b04fab12 100644 --- a/Executables/Validation/CMakeLists.txt +++ b/Executables/Validation/CMakeLists.txt @@ -44,6 +44,7 @@ if (Nymph_ENABLE_TESTING) TestApplication TestApplyCut TestCacheDirectory + TestContext TestCut TestCutFilter TestLogger diff --git a/Executables/Validation/KTTestProcessor.cc b/Executables/Validation/KTTestProcessor.cc index ad14cdf0..36ef014f 100644 --- a/Executables/Validation/KTTestProcessor.cc +++ b/Executables/Validation/KTTestProcessor.cc @@ -13,7 +13,10 @@ namespace Nymph { KTLOGGER(testsiglog, "KTTestProcessor") - KTTestProcessorA::KTTestProcessorA() : + KT_REGISTER_PROCESSOR(KTTestProcessorA, "test-proc-a"); + + KTTestProcessorA::KTTestProcessorA(const std::string& name) : + KTProcessor(name), fTheSignal() { RegisterSignal("the_signal", &fTheSignal); @@ -35,9 +38,10 @@ namespace Nymph } + KT_REGISTER_PROCESSOR(KTTestProcessorB, "test-proc-b"); - - KTTestProcessorB::KTTestProcessorB() + KTTestProcessorB::KTTestProcessorB(const std::string& name) : + KTProcessor(name) { RegisterSlot("first_slot", this, &KTTestProcessorB::Slot1); RegisterSlot("second_slot", this, &KTTestProcessorB::Slot2); diff --git a/Executables/Validation/KTTestProcessor.hh b/Executables/Validation/KTTestProcessor.hh index 77560935..eb6df849 100644 --- a/Executables/Validation/KTTestProcessor.hh +++ b/Executables/Validation/KTTestProcessor.hh @@ -19,7 +19,7 @@ namespace Nymph typedef KTSignalConcept< void (int) >::signal TheSignal; public: - KTTestProcessorA(); + KTTestProcessorA(const std::string& name = "test-proc-a"); virtual ~KTTestProcessorA(); bool Configure(const scarab::param_node* node); @@ -34,7 +34,7 @@ namespace Nymph class KTTestProcessorB : public KTProcessor { public: - KTTestProcessorB(); + KTTestProcessorB(const std::string& name = "test-proc-b"); virtual ~KTTestProcessorB(); bool Configure(const scarab::param_node* node); diff --git a/Executables/Validation/TestContext.cc b/Executables/Validation/TestContext.cc new file mode 100644 index 00000000..b8e9448d --- /dev/null +++ b/Executables/Validation/TestContext.cc @@ -0,0 +1,55 @@ +/* + * TestContext.cc + * + * Created on: Aug 9, 2024 + * Author: N.S. Oblath + * + * Usage: > ./TestContext + * + * Purpose: Test access to context from multiple processors + * + */ + +#include "KTTestProcessor.hh" + +#include "KTContext.hh" +#include "KTLogger.hh" +#include "KTProcessorToolbox.hh" + +using namespace Nymph; + +KTLOGGER(testlog, "TestContext"); + +int main() +{ + KTTestProcessorA procA; + KTTestProcessorB procB; + + KTINFO(testlog, "Contexts belonging to procA and procB should be independent\n&procA.Context() == &procB.Context(): " << (&procA.Context() == &procB.Context())); + + KTProcessorToolbox toolbox; + + if (! toolbox.AddProcessor("test-proc-a", "tpa")) + { + KTERROR(testlog, "Unable to create test proc A with the Processor Toolbox"); + return 1; + } + if (! toolbox.AddProcessor("test-proc-b", "tpb")) + { + KTERROR(testlog, "Unable to create test proc B with the Processor Toolbox"); + return 1; + } + + KTProcessor& procAtb = *toolbox.GetProcessor("tpa"); + KTProcessor& procBtb = *toolbox.GetProcessor("tpb"); + + KTINFO(testlog, "Contexts belonging to procAtb and procBtb should be identical\n&procAtb.Context() == &procBtb.Context(): " << (&procAtb.Context() == &procBtb.Context())); + + toolbox.Context().Add("test", scarab::param_value(5)); + KTINFO(testlog, "toolbox has context \"test\"? (expect \"1\") " << toolbox.Context().Has("test")); + KTINFO(testlog, "procAtb has context \"test\"? (expect \"1\") " << procAtb.Context().Has("test")); + KTINFO(testlog, "procBtb has context \"test\"? (expect \"1\") " << procBtb.Context().Has("test")); + KTINFO(testlog, "Values are all equal? (expect \"1 1\") " << (toolbox.Context().Get("test")().as_int() == procAtb.Context().Get("test")().as_int()) << " " << (toolbox.Context().Get("test")().as_int() == procBtb.Context().Get("test")().as_int())); + + return 0; +} diff --git a/Library/Application/KTProcessorToolbox.cc b/Library/Application/KTProcessorToolbox.cc index 1623e2b8..705a5e94 100644 --- a/Library/Application/KTProcessorToolbox.cc +++ b/Library/Application/KTProcessorToolbox.cc @@ -31,6 +31,7 @@ namespace Nymph KTProcessorToolbox::KTProcessorToolbox(const std::string& name) : KTConfigurable(name), + KTHasContext(), fProcFactory(scarab::factory< KTProcessor, const std::string& >::get_instance()), fRunQueue(), fProcMap() @@ -318,6 +319,9 @@ namespace Nymph ProcMapIt it = fProcMap.find(procName); if (it == fProcMap.end()) { + // share the context + proc->SetContext(fContext); + ProcessorInfo pInfo; pInfo.fProc = proc; fProcMap.insert(ProcMapValue(procName, pInfo)); diff --git a/Library/Application/KTProcessorToolbox.hh b/Library/Application/KTProcessorToolbox.hh index 555b2162..517d49cf 100644 --- a/Library/Application/KTProcessorToolbox.hh +++ b/Library/Application/KTProcessorToolbox.hh @@ -10,6 +10,7 @@ #define KTPROCESSORTOOLBOX_HH_ #include "KTConfigurable.hh" +#include "KTContext.hh" //#include "KTNOFactory.hh" #include "factory.hh" @@ -71,7 +72,7 @@ namespace Nymph */ - class KTProcessorToolbox : public KTConfigurable + class KTProcessorToolbox : public KTConfigurable, public KTHasContext { public: KTProcessorToolbox(const std::string& name = "processor-toolbox"); @@ -199,7 +200,5 @@ namespace Nymph return; } - - } /* namespace Nymph */ #endif /* KTPROCESSORTOOLBOX_HH_ */ diff --git a/Library/CMakeLists.txt b/Library/CMakeLists.txt index c7d0c0d5..a88501c1 100644 --- a/Library/CMakeLists.txt +++ b/Library/CMakeLists.txt @@ -29,6 +29,7 @@ set( NYMPH_HEADERFILES ${DATA_DIR}/KTCutStatus.hh ${DATA_DIR}/KTData.hh ${PROC_DIR}/KTConnection.hh + ${PROC_DIR}/KTContext.hh ${PROC_DIR}/KTPrimaryProcessor.hh ${PROC_DIR}/KTProcessor.hh ${PROC_DIR}/KTSignal.hh @@ -62,6 +63,7 @@ set( NYMPH_SOURCEFILES ${DATA_DIR}/KTCutFilter.cc ${DATA_DIR}/KTCutStatus.cc ${DATA_DIR}/KTData.cc + ${PROC_DIR}/KTContext.cc ${PROC_DIR}/KTPrimaryProcessor.cc ${PROC_DIR}/KTProcessor.cc ${PROC_DIR}/KTSignal.cc diff --git a/Library/Processor/KTContext.cc b/Library/Processor/KTContext.cc new file mode 100644 index 00000000..cd83d080 --- /dev/null +++ b/Library/Processor/KTContext.cc @@ -0,0 +1,54 @@ +/* + * KTContext.cc + * + * Created on: Aug 7, 2024 + * Author: N.S. Oblath + */ + +#include "KTContext.hh" + +#include "KTLogger.hh" + +#include + +namespace Nymph +{ + + KTLOGGER(proclog, "KTContext.hh"); + + KTContext::KTContext() : + fData() + {} + + KTContext::~KTContext() + {} + + bool KTContext::Has(const std::string& name) const + { + return fData.has(name); + } + + scarab::param& KTContext::Get(const std::string& name) + { + return fData[name]; + } + + const scarab::param& KTContext::Get(const std::string& name) const + { + return fData[name]; + } + + void KTContext::Add(const std::string& name, const scarab::param& data) + { + fData.add(name, data); + return; + } + + + KTHasContext::KTHasContext() : + fContext(std::make_shared< KTContext >()) + {} + + KTHasContext::~KTHasContext() + {} +} /* namespace Nymph */ diff --git a/Library/Processor/KTContext.hh b/Library/Processor/KTContext.hh new file mode 100644 index 00000000..2d1ca311 --- /dev/null +++ b/Library/Processor/KTContext.hh @@ -0,0 +1,68 @@ +/** + @file KTContext.hh + @brief Contains KTContext + @details KTContext contains the run context that's available to all processors + @author: N. S. Oblath + @date: Aug 7, 2024 + */ + +#ifndef KTCONTEXT_HH_ +#define KTCONTEXT_HH_ + +#include "param.hh" + +namespace Nymph +{ + + class KTContext + { + public: + KTContext(); + virtual ~KTContext(); + + bool Has(const std::string& name) const; + + scarab::param& Get(const std::string& name); + const scarab::param& Get(const std::string& name) const; + + void Add(const std::string& name, const scarab::param& data); + + protected: + scarab::param_node fData; + }; + + class KTHasContext + { + public: + KTHasContext(); + virtual ~KTHasContext(); + + KTContext& Context(); + const KTContext& Context() const; + + void SetContext(const std::shared_ptr< KTContext > context); + + protected: + std::shared_ptr< KTContext > fContext; + + }; + + inline KTContext& KTHasContext::Context() + { + return *fContext; + } + + inline const KTContext& KTHasContext::Context() const + { + return *fContext; + } + + inline void KTHasContext::SetContext(std::shared_ptr< KTContext > context) + { + fContext = context; + return; + } + +} /* namespace Nymph */ + +#endif /* KTCONTEXT_HH_ */ diff --git a/Library/Processor/KTProcessor.cc b/Library/Processor/KTProcessor.cc index 72a717d6..66a6d868 100644 --- a/Library/Processor/KTProcessor.cc +++ b/Library/Processor/KTProcessor.cc @@ -7,6 +7,7 @@ #include "KTProcessor.hh" +#include "KTContext.hh" //#include "KTLogger.hh" #include @@ -26,10 +27,10 @@ namespace Nymph KTProcessor::KTProcessor(const string& name) : KTConfigurable(name), + KTHasContext(), fSignalMap(), fSlotMap() - { - } + {} KTProcessor::~KTProcessor() { diff --git a/Library/Processor/KTProcessor.hh b/Library/Processor/KTProcessor.hh index 7aa8e6c2..0ae1b0cc 100644 --- a/Library/Processor/KTProcessor.hh +++ b/Library/Processor/KTProcessor.hh @@ -10,6 +10,7 @@ #define KTPROCESSOR_HH_ #include "KTConfigurable.hh" +#include "KTContext.hh" #include "KTConnection.hh" #include "KTLogger.hh" @@ -24,6 +25,7 @@ #include #include +#include #include #include @@ -31,13 +33,14 @@ namespace Nymph { KTLOGGER(processorlog, "KTProcessor.hh"); + class ProcessorException : public std::logic_error { public: ProcessorException(std::string const& why); }; - class KTProcessor : public KTConfigurable + class KTProcessor : public KTConfigurable, public KTHasContext { protected: typedef std::map< std::string, KTSignalWrapper* > SignalMap; diff --git a/Library/Utility/KTLogger.cc b/Library/Utility/KTLogger.cc index 7cc87190..3cfff8ce 100644 --- a/Library/Utility/KTLogger.cc +++ b/Library/Utility/KTLogger.cc @@ -147,7 +147,7 @@ namespace Nymph fPrivate->fLogger = logName; } fPrivate->fColored = true; - sprintf(KTLogger::Private::sDateTimeFormat, "%%T"); + snprintf(KTLogger::Private::sDateTimeFormat, 16, "%%T"); SetLevel(eDebug); } @@ -155,7 +155,7 @@ namespace Nymph { fPrivate->fLogger = name.c_str(); fPrivate->fColored = true; - sprintf(KTLogger::Private::sDateTimeFormat, "%%T"); + snprintf(KTLogger::Private::sDateTimeFormat, 16, "%%T"); SetLevel(eDebug); } diff --git a/Scarab b/Scarab index ef2af248..f6b2c613 160000 --- a/Scarab +++ b/Scarab @@ -1 +1 @@ -Subproject commit ef2af248c5db7c92c84952a57759ba0f145f6cd4 +Subproject commit f6b2c613653584c9c5b0327bbe18302c2fef37a9