summaryrefslogtreecommitdiff
path: root/src/libcamera/include
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-03-13 19:59:57 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-04-15 01:07:24 +0300
commit79ce121b6bd33cc90e0a6645ef7391291792986f (patch)
tree0fb9e1f297eb62a5ca41f754c0041767c8e9f0bb /src/libcamera/include
parentebd0cae455ef1605a39b00dec79770f3d52de88f (diff)
libcamera: utils: Add string join function
Add a utils::join() function to join elements of a container into a string, with a separator and an optional conversion function if the elements are not implicitly convertible to std::string. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'src/libcamera/include')
-rw-r--r--src/libcamera/include/utils.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
index cfa620f2..242eeded 100644
--- a/src/libcamera/include/utils.h
+++ b/src/libcamera/include/utils.h
@@ -11,6 +11,7 @@
#include <chrono>
#include <memory>
#include <ostream>
+#include <sstream>
#include <string>
#include <string.h>
#include <sys/time.h>
@@ -109,6 +110,49 @@ inline _hex hex<uint64_t>(uint64_t value, unsigned int width)
size_t strlcpy(char *dst, const char *src, size_t size);
+#ifndef __DOXYGEN__
+template<typename Container, typename UnaryOp>
+std::string join(const Container &items, const std::string &sep, UnaryOp op)
+{
+ std::ostringstream ss;
+ bool first = true;
+
+ for (typename Container::const_iterator it = std::begin(items);
+ it != std::end(items); ++it) {
+ if (!first)
+ ss << sep;
+ else
+ first = false;
+
+ ss << op(*it);
+ }
+
+ return ss.str();
+}
+
+template<typename Container>
+std::string join(const Container &items, const std::string &sep)
+{
+ std::ostringstream ss;
+ bool first = true;
+
+ for (typename Container::const_iterator it = std::begin(items);
+ it != std::end(items); ++it) {
+ if (!first)
+ ss << sep;
+ else
+ first = false;
+
+ ss << *it;
+ }
+
+ return ss.str();
+}
+#else
+template<typename Container, typename UnaryOp>
+std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
+#endif
+
namespace details {
class StringSplitter