From 5d268488b2861ce73db233d71bdb527f89001a4e Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 25 Jan 2025 12:57:24 +0100 Subject: Add PointerList class for working with, and propagating, argc/argv --- src/pointerlist.cc | 28 ++++++++++++++++++++++++++++ src/pointerlist.h | 26 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/pointerlist.cc create mode 100644 src/pointerlist.h (limited to 'src') diff --git a/src/pointerlist.cc b/src/pointerlist.cc new file mode 100644 index 0000000..e642075 --- /dev/null +++ b/src/pointerlist.cc @@ -0,0 +1,28 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include "pointerlist.h" + +PointerList::PointerList(int argc, const char* argv[]) +{ + for(int i = 0; i < argc; ++i) + { + push_back(argv[i]); + } +} + +std::pair PointerList::get() +{ + argptrs.clear(); + for(const auto& arg : *this) + { + argptrs.push_back(arg.data()); + } + + if(argptrs.size() == 0) + { + return {0, nullptr}; + } + + return {argptrs.size(), argptrs.data()}; +} diff --git a/src/pointerlist.h b/src/pointerlist.h new file mode 100644 index 0000000..7c51ca6 --- /dev/null +++ b/src/pointerlist.h @@ -0,0 +1,26 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. + +#include +#include +#include +#include + +// Maintains an (owning) list of string args and converts them to argc/argv +// compatible arguments on request. +// The returned pointers are guaranteed to be valid as long as the PointerList +// object lifetime is not exceeded. +class PointerList : + public std::deque +{ +public: + PointerList() = default; + PointerList(int argc, const char* argv[]); + + //! Returns argc/argv pair from the current list of args + std::pair get(); + +private: + std::vector argptrs; +}; -- cgit v1.2.3