From b09fa00cca1a9baa0327a33d865fc970f77d4db0 Mon Sep 17 00:00:00 2001 From: couriersud Date: Fri, 18 Oct 2019 17:57:55 +0200 Subject: Netlist: code maintenance and improvements. [Couriersud] - Added support for line markers to the preprocessor and parser. - Added support for include processing to the preprocessor. - Moved sources base type to plib to be used for preprocessor includes. This enables to include e.g. from rom memory regions. - Renamed some defines --- src/lib/netlist/plib/putil.h | 112 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'src/lib/netlist/plib/putil.h') diff --git a/src/lib/netlist/plib/putil.h b/src/lib/netlist/plib/putil.h index 5ff70d941a2..227648b172b 100644 --- a/src/lib/netlist/plib/putil.h +++ b/src/lib/netlist/plib/putil.h @@ -10,6 +10,8 @@ #include "pexception.h" #include "pstring.h" +#include "palloc.h" + #include #include #include @@ -24,8 +26,118 @@ namespace plib { + // ---------------------------------------------------------------------------------------- + // A Generic netlist sources implementation + // ---------------------------------------------------------------------------------------- + + class psource_t + { + public: + + using stream_ptr = plib::unique_ptr; + + psource_t() + {} + + COPYASSIGNMOVE(psource_t, delete) + + virtual ~psource_t() noexcept = default; + + virtual stream_ptr stream(const pstring &name) = 0; + private: + }; + + /**! Generic string source + * + * Will return the given string when name matches. + * Is used in preprocessor code to eliminate inclusion of certain files. + * + * @tparam TS base stream class. Default is psource_t + */ + template + class psource_str_t : public TS + { + public: + psource_str_t(pstring name, pstring str) + : m_name(name), m_str(str) + {} + + COPYASSIGNMOVE(psource_str_t, delete) + virtual ~psource_str_t() noexcept = default; + + typename TS::stream_ptr stream(const pstring &name) override + { + if (name == m_name) + return plib::make_unique(m_str); + else + return typename TS::stream_ptr(nullptr); + } + private: + pstring m_name; + pstring m_str; + }; + + /**! Generic sources collection + * + * @tparam TS base stream class. Default is psource_t + */ + template + class psource_collection_t + { + public: + using source_type = plib::unique_ptr; + using list_t = std::vector; + + psource_collection_t() + {} + + COPYASSIGNMOVE(psource_collection_t, delete) + virtual ~psource_collection_t() noexcept = default; + + void add_source(source_type &&src) + { + m_collection.push_back(std::move(src)); + } + + template + typename S::stream_ptr get_stream(pstring name) + { + for (auto &s : m_collection) + { + auto source(dynamic_cast(s.get())); + if (source) + { + auto strm = source->stream(name); + if (strm) + return strm; + } + } + return typename S::stream_ptr(nullptr); + } + + template + bool for_all(pstring name, F lambda) + { + for (auto &s : m_collection) + { + auto source(dynamic_cast(s.get())); + if (source) + { + if (lambda(source)) + return true; + } + } + return false; + } + + private: + list_t m_collection; + }; + namespace util { + pstring basename(pstring filename); + pstring path(pstring filename); pstring buildpath(std::initializer_list list ); pstring environment(const pstring &var, const pstring &default_val); } // namespace util -- cgit v1.2.3-70-g09d2