first re-commit.
This commit is contained in:
20
util/xqexception.cpp
Normal file
20
util/xqexception.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include <xqexception.h>
|
||||
|
||||
XQException::XQException(const QString& what, const QString& param )
|
||||
: std::runtime_error( param.isEmpty() ? what.toStdString() : QString( "%1: %2" ).arg(what,param).toStdString( ) )
|
||||
{}
|
||||
|
34
util/xqexception.h
Normal file
34
util/xqexception.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef XQEXCEPTION_H
|
||||
#define XQEXCEPTION_H
|
||||
|
||||
#include <QString>
|
||||
#include <stdexcept>
|
||||
|
||||
/**
|
||||
* @brief Simple exception class
|
||||
*/
|
||||
|
||||
class XQException : public std::runtime_error
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
XQException( const QString& what, const QString& param="" );
|
||||
|
||||
};
|
||||
|
||||
#endif // XQEXCEPTION_H
|
76
util/xqmapindex.h
Normal file
76
util/xqmapindex.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef XQMAPINDEX_H
|
||||
#define XQMAPINDEX_H
|
||||
|
||||
#include <QMap>
|
||||
|
||||
/**
|
||||
* @brief Holds the string to index mapping for the QXMaptor classes
|
||||
*/
|
||||
|
||||
class XQMapIndex : public QMap<QString, int>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
virtual ~XQMapIndex() = default;
|
||||
|
||||
void addKey(const QString& key, int index)
|
||||
{
|
||||
(*this)[key] = index;
|
||||
}
|
||||
|
||||
|
||||
int indexOf(const QString& key) const
|
||||
{
|
||||
if (contains(key))
|
||||
return (*this)[key];
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void update(int index)
|
||||
{
|
||||
|
||||
XQMapIndex newindex;
|
||||
QMapIterator<QString, int> iter(*this);
|
||||
|
||||
while (iter.hasNext())
|
||||
{
|
||||
iter.next();
|
||||
|
||||
// item idx == kill-index: continue
|
||||
// item idx < kill-index: store item
|
||||
// item idx > kill-index: decrement & store item
|
||||
|
||||
int idx = iter.value();
|
||||
if (idx == index)
|
||||
continue;
|
||||
if (idx > index)
|
||||
idx--;
|
||||
// schlüssel auch sichern
|
||||
newindex[(iter.key())] = idx;
|
||||
|
||||
}
|
||||
|
||||
swap(newindex);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // XQMAPINDEX_H
|
305
util/xqmaptor.h
Normal file
305
util/xqmaptor.h
Normal file
@@ -0,0 +1,305 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef XQMAPTOR_H
|
||||
#define XQMAPTOR_H
|
||||
|
||||
#include <xqmapindex.h>
|
||||
#include <xqexception.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief map + vector = XQMaptor, a template storage class whose data
|
||||
* items can be accessed via string keys and int indices.
|
||||
*/
|
||||
|
||||
template<class T>
|
||||
class XQMaptor
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
XQMaptor()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
XQMaptor( int itemsize )
|
||||
{
|
||||
if( itemsize )
|
||||
_data.resize( itemsize );
|
||||
}
|
||||
|
||||
|
||||
XQMaptor( const XQMaptor& src )
|
||||
{
|
||||
*this=src;
|
||||
}
|
||||
|
||||
|
||||
virtual ~XQMaptor()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
XQMaptor& operator=( const XQMaptor& src )
|
||||
{
|
||||
if( this == &src )
|
||||
return *this;
|
||||
_data = src._data;
|
||||
_index = src._index;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// STL-like iterators
|
||||
auto begin()
|
||||
{
|
||||
return _data.begin();
|
||||
}
|
||||
|
||||
auto end()
|
||||
{
|
||||
return _data.end();
|
||||
}
|
||||
|
||||
inline int size() const
|
||||
{
|
||||
return (int) _data.size();
|
||||
}
|
||||
|
||||
|
||||
inline bool isEmpty() const
|
||||
{
|
||||
return (_data.size()==0);
|
||||
}
|
||||
|
||||
inline bool contains( int index ) const
|
||||
{
|
||||
return index < size() && index > -1;
|
||||
}
|
||||
|
||||
inline bool contains( const QString& key ) const
|
||||
{
|
||||
return mapIndex().contains(key);
|
||||
}
|
||||
|
||||
|
||||
inline const XQMapIndex& mapIndex() const
|
||||
{
|
||||
return _index;
|
||||
}
|
||||
|
||||
|
||||
int indexOf( const QString& key ) const
|
||||
{
|
||||
return mapIndex().indexOf(key);
|
||||
}
|
||||
|
||||
int indexOf(const T& entry) const
|
||||
{
|
||||
for (int i=0; i<_data.size(); ++i)
|
||||
{
|
||||
if (_data[i] == entry)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
virtual QString keyOf( int index ) const
|
||||
{
|
||||
return mapIndex().key( index );
|
||||
}
|
||||
|
||||
|
||||
T& operator[]( int index )
|
||||
{
|
||||
if( contains(index) )
|
||||
return _data[index];
|
||||
throw XQException("XQMaptor operator[ int index ]: out of range");
|
||||
}
|
||||
|
||||
|
||||
const T& operator[]( int index ) const
|
||||
{
|
||||
if ( contains(index) )
|
||||
return _data[index];
|
||||
throw XQException("XQMaptor const operator[ int index ]: out of range");
|
||||
}
|
||||
|
||||
T& at( int index )
|
||||
{
|
||||
return (*this)[index];
|
||||
}
|
||||
|
||||
const T& at( int index ) const
|
||||
{
|
||||
return (*this)[index];
|
||||
}
|
||||
|
||||
T& operator[]( const QString& key )
|
||||
{
|
||||
if( key.isEmpty() || !contains(key) )
|
||||
throw XQException("maprow operator[]: key empty || not found: " + key);
|
||||
return _data[ _index[key] ];
|
||||
}
|
||||
|
||||
|
||||
const T& operator[]( const QString& key ) const
|
||||
{
|
||||
if (key.isEmpty() || !contains(key))
|
||||
throw XQException("maprow operator[]: key empty || not found: " + key);
|
||||
return _data[_index[key]];
|
||||
}
|
||||
|
||||
|
||||
T& at( const QString& key )
|
||||
{
|
||||
return (*this)[key];
|
||||
}
|
||||
|
||||
const T& at( const QString& key ) const
|
||||
{
|
||||
return (*this)[key];
|
||||
}
|
||||
|
||||
virtual int add( const T& item )
|
||||
{
|
||||
_data.push_back( item );
|
||||
return _data.size()-1;
|
||||
}
|
||||
|
||||
|
||||
virtual void addAtIndex( int index, const T& item )
|
||||
{
|
||||
if(contains(index))
|
||||
throw XQException( "QStringrow::add: index out of range!" );
|
||||
_data[index] = item;
|
||||
}
|
||||
|
||||
|
||||
// convenience method to mimic QMap<T,QString>
|
||||
virtual void insert( const T& item, const QString& key )
|
||||
{
|
||||
addAtKey(key, item );
|
||||
}
|
||||
|
||||
|
||||
virtual void addAtKey( const QString& key, const T& item )
|
||||
{
|
||||
XQMapIndex::iterator pos = _index.find( key );
|
||||
if( pos == _index.end() )
|
||||
{
|
||||
_data.push_back( item );
|
||||
_index[key] = _data.size()-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_data[pos.value()] = item;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool addAlias( const QString& key, const QString& alias )
|
||||
{
|
||||
// look for 'original' key
|
||||
int key_idx = indexOf(key);
|
||||
// quit if not found
|
||||
if( key_idx < 0 )
|
||||
return false;
|
||||
// look for alias
|
||||
int alias_idx = indexOf(alias);
|
||||
// quit if found: don't overwrite anything
|
||||
if( alias_idx > -1 )
|
||||
return false;
|
||||
// store alias
|
||||
_index[ alias ] = key_idx;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void addKey( const QString& key, int index )
|
||||
{
|
||||
_index.addKey( key, index );
|
||||
}
|
||||
|
||||
|
||||
virtual void clear()
|
||||
{
|
||||
_data.clear();
|
||||
_index.clear();
|
||||
}
|
||||
|
||||
|
||||
virtual bool killEntry( const QString& key )
|
||||
{
|
||||
int idx = indexOf( key );
|
||||
if( idx<0 )
|
||||
return false;
|
||||
return killEntry( (int) idx );
|
||||
}
|
||||
|
||||
|
||||
virtual bool killEntry( int index )
|
||||
{
|
||||
if( index >= this->_data.size() )
|
||||
return false;
|
||||
// eintrag lschen
|
||||
this->_data.erase( this->_data.begin()+index );
|
||||
// index updaten
|
||||
_index.update( index );
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
virtual QString toString() const
|
||||
{
|
||||
return join( ";" );
|
||||
}
|
||||
|
||||
|
||||
virtual void dump() const
|
||||
{
|
||||
throw XQException("XQMaptor: dump not implemented!" );
|
||||
}
|
||||
|
||||
|
||||
virtual QString join( const QString& sep, int from=0, int to=-1) const
|
||||
{
|
||||
Q_UNUSED(sep)
|
||||
Q_UNUSED(from)
|
||||
Q_UNUSED(to)
|
||||
throw XQException("XQMaptor: join not implemented!" );
|
||||
return "--";
|
||||
}
|
||||
|
||||
|
||||
int replaceKey( const QString& oldkey, const QString& newkey )
|
||||
{
|
||||
int idx = indexOf( oldkey );
|
||||
if( idx<0 || oldkey == newkey )
|
||||
return idx;
|
||||
_index.remove( oldkey );
|
||||
_index[ newkey ] = idx;
|
||||
return idx;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
QVector<T> _data;
|
||||
XQMapIndex _index;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // XQMAPTOR_H
|
128
util/xqptrmaptor.h
Normal file
128
util/xqptrmaptor.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef XQPTRMAPTOR_H
|
||||
#define XQPTRMAPTOR_H
|
||||
|
||||
#include <xqmaptor.h>
|
||||
|
||||
/**
|
||||
* @brief A XQMaptor implementation for pointers to to data entries.
|
||||
*/
|
||||
|
||||
template<class T>
|
||||
class XQPtrMaptor : public XQMaptor<T*>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
virtual ~XQPtrMaptor()
|
||||
{
|
||||
for (int i = 0; i < this->_data.size(); ++i)
|
||||
delete this->_data[i];
|
||||
}
|
||||
|
||||
T& entry(const QString& key)
|
||||
{
|
||||
int i = this->indexOf(key);
|
||||
if (i < 0)
|
||||
throw XQException("XQPtrMaptor: entry: not found: " + key);
|
||||
return *(this->_data[i]);
|
||||
}
|
||||
|
||||
const T& entry(const QString& key) const
|
||||
{
|
||||
int i = this->indexOf(key);
|
||||
if (i < 0)
|
||||
throw XQException("XQPtrMaptor: entry: not found: " + key);
|
||||
return *(this->_data[i]);
|
||||
}
|
||||
|
||||
T& entry(int index)
|
||||
{
|
||||
if ((int)index < this->_data.size())
|
||||
return *(this->_data[index]);
|
||||
throw XQException("ddmapptr entry( int index ): out of range");
|
||||
}
|
||||
|
||||
|
||||
const T& entry(int index) const
|
||||
{
|
||||
if (index > 0 && index < this->_data.size())
|
||||
return *(this->_data[index]);
|
||||
throw XQException("ddmapptr const entry( int index ): out of range");
|
||||
}
|
||||
|
||||
|
||||
bool killEntry(const QString& key) override
|
||||
{
|
||||
int idx = this->indexOf(key);
|
||||
// warum keine exception?
|
||||
if (idx < 0)
|
||||
return false;
|
||||
return killEntry((int)idx);
|
||||
}
|
||||
|
||||
bool killEntry(int index) override
|
||||
{
|
||||
T* item = this->_data[index];
|
||||
if (XQMaptor<T*>::killEntry(index))
|
||||
{
|
||||
delete item;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool removeEntry(const QString& key)
|
||||
{
|
||||
int idx = this->indexOf(key);
|
||||
// warum keine exception?
|
||||
if (idx < 0)
|
||||
return false;
|
||||
return removeEntry((int)idx);
|
||||
}
|
||||
|
||||
|
||||
bool removeEntry(int index)
|
||||
{
|
||||
if (index >= this->_data.size())
|
||||
return false;
|
||||
|
||||
// eintrag löschen
|
||||
this->_data.erase(this->_data.begin() + index);
|
||||
// index updaten
|
||||
this->_index.update(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void replace(int index, T* entry)
|
||||
{
|
||||
if ((int)index >= this->_data.size())
|
||||
throw XQException("ddmapptr replace( int index ): out of range");
|
||||
delete(this->_data[index]);
|
||||
this->_data[index] = entry;
|
||||
}
|
||||
|
||||
void replace(const QString& key, T* entry)
|
||||
{
|
||||
replace(this->indexOf(key), entry);
|
||||
}
|
||||
|
||||
};
|
||||
ö
|
||||
#endif // XQPTRMAPTOR_H
|
48
util/xsingleton.h
Normal file
48
util/xsingleton.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef XSINGLETON_H
|
||||
#define XSINGLETON_H
|
||||
|
||||
/**
|
||||
* @brief The classic singleton template interface.
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
class xsingleton
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//! Get the singleton instance.
|
||||
static T& instance()
|
||||
{
|
||||
static T Instance;
|
||||
return Instance;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
xsingleton() {}
|
||||
virtual ~xsingleton() {}
|
||||
|
||||
private:
|
||||
|
||||
xsingleton(xsingleton const &) = delete;
|
||||
xsingleton& operator=(xsingleton const &) = delete;
|
||||
|
||||
};
|
||||
|
||||
#endif // XSINGLETON_H
|
||||
|
47
util/xtreewalker.h
Normal file
47
util/xtreewalker.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx xtree
|
||||
Copyright © 2024-2025 c.holzheuer
|
||||
christoph.holzheuer@gmail.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef XTREEWALKER_H
|
||||
#define XTREEWALKER_H
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <functional>
|
||||
|
||||
/**
|
||||
* @brief A implentation of the pugi::xml treewalker class.
|
||||
*/
|
||||
|
||||
template <typename O,typename M>
|
||||
class xtreewalker : public pugi::xml_tree_walker
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
xtreewalker( O* object, M member )
|
||||
{
|
||||
_call = std::bind(member, object, std::placeholders::_1, std::placeholders::_2);
|
||||
}
|
||||
|
||||
virtual bool for_each(pugi::xml_node& node) override
|
||||
{
|
||||
return _call(node,depth());
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
std::function<bool(pugi::xml_node& node,int)> _call;
|
||||
|
||||
};
|
||||
|
||||
#endif // XTREEWALKER_H
|
Reference in New Issue
Block a user