Mercurial > octave
changeset 22148:6772baacd71f
string_vector: add constructor that supports any container.
* str-vec.h: add new templated constructor that will accept any container
of std::string with same interface as the STL.
* str-vec.cc: remove the two constructors of repeated code which is
now needs to be declared the header.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Tue, 19 Jul 2016 18:04:12 +0100 |
parents | da413c03920c |
children | ba8a9d2934c7 |
files | liboctave/util/str-vec.cc liboctave/util/str-vec.h |
diffstat | 2 files changed, 20 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/util/str-vec.cc Tue Jul 19 11:27:56 2016 -0700 +++ b/liboctave/util/str-vec.cc Tue Jul 19 18:04:12 2016 +0100 @@ -39,39 +39,6 @@ #include "lo-utils.h" #include "str-vec.h" -// FIXME: isn't there some STL trick that could be used to make this -// work for all STL containers of std::string objects? - -string_vector::string_vector (const std::list<std::string>& lst) - : Array<std::string> () -{ - size_t n = lst.size (); - - resize (n); - - octave_idx_type i = 0; - - for (std::list<std::string>::const_iterator p = lst.begin (); - p != lst.end (); - p++) - elem (i++) = *p; -} - -string_vector::string_vector (const std::set<std::string>& lst) - : Array<std::string> () -{ - size_t n = lst.size (); - - resize (n); - - octave_idx_type i = 0; - - for (std::set<std::string>::const_iterator p = lst.begin (); - p != lst.end (); - p++) - elem (i++) = *p; -} - // Create a string vector from a NULL terminated list of C strings. string_vector::string_vector (const char * const *s)
--- a/liboctave/util/str-vec.h Tue Jul 19 11:27:56 2016 -0700 +++ b/liboctave/util/str-vec.h Tue Jul 19 18:04:12 2016 +0100 @@ -51,9 +51,14 @@ string_vector (const string_vector& s) : Array<std::string> (s) { } - string_vector (const std::list<std::string>& lst); - - string_vector (const std::set<std::string>& lst); + //! Constructor for STL containers of std::string + /*! + Templated constructor for any template class with std::string as the + first parameter, and begin, end, and size methods, i.e., a class with + similar interface as the STL containers. + */ + template<template <typename...> class String_Container> + string_vector (const String_Container<std::string>& lst); string_vector (const Array<std::string>& s) : Array<std::string> (s.as_column ()) { } @@ -122,4 +127,16 @@ const std::string& prefix = "") const; }; + +template<template <typename...> class String_Container> +string_vector::string_vector (const String_Container<std::string>& lst) + : Array<std::string> () +{ + resize (lst.size ()); + + octave_idx_type i = 0; + for (const std::string& s : lst) + elem(i++) = s; +} + #endif