41#include <drain/UniTuple.h>
48#include <drain/Sprinter.h>
80 PathSeparatorPolicy(
char separator=
'/',
bool lead=
true,
bool repeated=
false,
bool trail=
true) :
82 set(lead, repeated, trail);
84 throw std::runtime_error(
"PathSeparatorPolict (char separator): separator=0, did you mean empty init (\"\")");
93DRAIN_TYPENAME(PathSeparatorPolicy);
114std::ostream & operator<<(std::ostream & ostr,
const PathSeparatorPolicy & policy) {
115 ostr <<
"PathSeparatorPolicy";
116 if (policy.character){
117 ostr <<
'[' << policy.character <<
']';
119 ostr <<
'(' << policy.tuple() <<
')';
134template <
class T,
char SEP=
'/',
bool ALEAD=
true,
bool AREPEAT=
false,
bool ATRAIL=
true>
135class Path :
public std::list<T> {
143 typedef std::list< path_t > list_t;
162 Path(
typename path_t::const_iterator it,
typename path_t::const_iterator it2){
171 template <
typename ... TT>
179 template <
typename ... TT>
181 Path(
const std::string & arg,
const TT &... args){
187 template <
typename ... TT>
189 Path(
const char * arg,
const TT &... args){
214 template <
typename ... TT>
215 void set(
const TT &... args) {
222 template <
typename T2,
typename ... TT>
223 void append(
const T2 & arg,
const TT &... args) {
229 template <
typename ... TT>
230 void append(
const path_t &p,
const TT &... args) {
231 this->insert(this->end(), p.begin(), p.end());
236 template <
typename ... TT>
237 void append(
char c,
const TT &... args) {
239 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
"::" <<
" null separator char \n";
241 else if (c==separator.character){
251 template <
typename ... TT>
252 void append(
const char * arg,
const TT &... args) {
258 template <
typename ... TT>
259 void append(
const std::string &arg,
const TT &... args) {
290 this->push_back(elem);
293 else if (this->empty()){
295 this->push_back(elem);
298 else if (this->back().empty()){
301 this->push_back(elem);
306 this->push_back(elem);
309 std::cerr << __FUNCTION__ <<
"? failed in appending: " << sprinter(*
this) <<
'\n';
315 template <
typename T2>
333 return ((this->size()==1) && this->front().empty());
346 return this->front().empty();
362 this->push_front(elem_t());
374 const size_t minLength = COMPLETE ? 0 : 1;
375 while (this->size() > minLength){
377 if (!this->front().empty()){
388 const size_t minLength = COMPLETE ? 0 : 1;
389 while (this->size() > minLength){
391 if (!this->back().empty()){
428 if (this->front().empty()){
429 typename path_t::const_iterator it0 = ++this->begin();
430 typename path_t::const_iterator it = it0;
431 while (it != this->end()) {
452 if (this->back().empty()){
453 typename path_t::const_iterator it0 = --this->end();
454 typename path_t::const_iterator it = it0;
456 while (it != this->begin()) {
501 path_t &
operator=(
const std::string & p){
543 this->insert(this->end(), path.begin(), path.end());
570 std::ostream &
toStream(std::ostream & ostr)
const {
573 ostr << separator.character;
576 layout.arrayChars.separator = separator.character;
588 std::string str()
const {
589 std::stringstream sstr;
595 operator std::string()
const {
599 std::ostream & debug(std::ostream & ostr = std::cout)
const {
601 ostr <<
"Path elems:" << this->size() <<
" sep:" << separator <<
" typeid: " <<
typeid(T).name() <<
"\n";
602 ostr << *
this <<
'\n';
604 for (
typename path_t::const_iterator it = this->begin(); it != this->end(); ++it) {
607 ostr <<
" " << i <<
'\t' << *it <<
'\n';
624 template <typename T2>
625 void _append(const T2 & p){
633 if (start == p.length()){
642 size_t nextSep = p.find(separator.character, start);
645 if (nextSep==std::string::npos){
677template <
class T,
char SEP=
'/',
bool ALEAD=
true,
bool AREPEAT=
false,
bool ATRAIL=
true>
679std::ostream & operator<<(std::ostream & ostr,
const Path<T,SEP,ALEAD,AREPEAT,ATRAIL> & p) {
680 return p.toStream(ostr);
684template <
class T,
char SEP=
'/',
bool ALEAD=
true,
bool AREPEAT=
false,
bool ATRAIL=
true>
686std::istream & operator>>(std::istream &istr, Path<T,SEP,ALEAD,AREPEAT,ATRAIL> & p) {
703template <
class T,
char SEP,
bool ALEAD,
bool AREPEAT,
bool ATRAIL>
704const PathSeparatorPolicy Path<T,SEP,ALEAD,AREPEAT,ATRAIL>::separator(SEP, ALEAD, AREPEAT, ATRAIL);
707template <
class T,
char SEP,
bool ALEAD,
bool AREPEAT,
bool ATRAIL>
713 const std::string & str(){
716 "Path<",
TypeName<T>::str(),
">[", SEP?SEP:
'?',
"](", ALEAD, AREPEAT, ATRAIL,
')');
virtual ~Path()
Why the three above instead of this?
Definition Path.h:208
void simplify()
ORIGINAL. Removes trailing empty elements, except for the root itself.
Definition Path.h:415
void simplifyTail()
Remove duplicates of empty elements from the end.
Definition Path.h:447
virtual std::ostream & toStream(std::ostream &ostr) const
Path is written like a list, adding the separator between items. Exception: root is displayed as sepa...
Definition Path.h:570
void appendElem(const elem_t &elem)
Main method for adding elements.
Definition Path.h:279
void appendSubStr(const std::string &p, size_t start=0)
"Default" append function.
Definition Path.h:631
void appendPath(const char *p)
Add path. Specialized handler for strings (note, possibly: elem_t==std:string)
Definition Path.h:267
void simplifyHead()
Remove duplicates of empty elements from the start.
Definition Path.h:423
void append(const std::string &arg, const TT &... args)
Add elements. Any string argument handled as a path.
Definition Path.h:259
path_t & operator=(const path_t &p)
Assigns a path directly with std::list assignment.
Definition Path.h:477
Path(const char *arg, const TT &... args)
Initialize with a path.
Definition Path.h:189
bool isRoot() const
Returns true, if the path as only one element which is empty. An empty path is not a root.
Definition Path.h:332
Path(const path_t &arg, const TT &... args)
Initialize with a path.
Definition Path.h:173
Path(const path_t &p)
Copy constructor.
Definition Path.h:157
void appendElem(const T2 &elem)
To be specialized in subclasses.
Definition Path.h:317
path_t & operator<<(const elem_t &elem)
Append an element, unless empty string.
Definition Path.h:536
Path(typename path_t::const_iterator it, typename path_t::const_iterator it2)
Secondary copy constructor. Handy for creating a parent path, for example.
Definition Path.h:162
void append(const T2 &arg, const TT &... args)
Append path with element(s), path(s) or string(s).
Definition Path.h:223
Path(const std::string &arg, const TT &... args)
Initialize with a path.
Definition Path.h:181
path_t & operator=(const Path< T2 > &p)
Conversion from str path type.
Definition Path.h:494
void append(char c, const TT &... args)
Add elements, first of which is a character. Any string argument is handled as a path.
Definition Path.h:237
path_t & ensureRoot()
Returns true, if the path is not empty and the first element is empty.
Definition Path.h:360
path_t & operator>>(elem_t &e)
Extract last element.
Definition Path.h:557
void append(const char *arg, const TT &... args)
Add elements. Any string argument handled as a path.
Definition Path.h:252
void trimTail(bool COMPLETE=false)
Removes trailing empty elements. The resulting string presentation will not end with the separator.
Definition Path.h:387
void append()
Terminal function for variadic templates.
Definition Path.h:662
void trimHead(bool COMPLETE=false)
Removes leading empty elements. The resulting string presentation will not start with the separator.
Definition Path.h:373
bool hasRoot() const
Returns true, if the path is not empty and the first element is empty.
Definition Path.h:344
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition Sprinter.h:324
Definition StringBuilder.h:58
Tuple of N elements of type T.
Definition UniTuple.h:65
Definition DataSelector.cpp:1277
Determines how separator char (often / ) will be handled in the path.
Definition Path.h:66
bool & acceptRepeated
If true, allow empty elements in the middle (appearing as repeated separators)
Definition Path.h:75
bool & acceptTrailing
If true, allow trailing empty elements (appearing as repeated separators)
Definition Path.h:78
bool & acceptLeading
If true, allow leading empties (ie leading separators) // Accepts one. to start with.
Definition Path.h:72
Definition Sprinter.h:136
Default implementation.
Definition TypeName.h:54