LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SerializableObjectList< T, P > Class Template Reference

The SerializableObjectList class implements a list of librepcb::SerializableObject. More...

#include <serializableobjectlist.h>

+ Inheritance diagram for SerializableObjectList< T, P >:
+ Collaboration diagram for SerializableObjectList< T, P >:

Classes

class  IF_Observer
 
class  Iterator
 

Public Types

using iterator = Iterator< typename QVector< std::shared_ptr< T >>::iterator, T >
 
using const_iterator = Iterator< typename QVector< std::shared_ptr< T >>::const_iterator, const T >
 

Public Member Functions

 SerializableObjectList (IF_Observer *observer=nullptr) noexcept
 
 SerializableObjectList (const SerializableObjectList< T, P > &other, IF_Observer *observer=nullptr) noexcept
 
 SerializableObjectList (SerializableObjectList< T, P > &&other, IF_Observer *observer=nullptr) noexcept
 
 SerializableObjectList (std::initializer_list< std::shared_ptr< T >> elements, IF_Observer *observer=nullptr) noexcept
 
 SerializableObjectList (std::initializer_list< T > elements, IF_Observer *observer=nullptr) noexcept
 
 SerializableObjectList (const SExpression &node, IF_Observer *observer=nullptr)
 
virtual ~SerializableObjectList () noexcept
 
bool isEmpty () const noexcept
 
int count () const noexcept
 
std::vector< UuidgetUuids () const noexcept
 
QSet< UuidgetUuidSet () const noexcept
 
int indexOf (const T *obj) const noexcept
 
int indexOf (const Uuid &key) const noexcept
 
int indexOf (const QString &name) const noexcept
 
bool contains (int index) const noexcept
 
bool contains (const T *obj) const noexcept
 
bool contains (const Uuid &key) const noexcept
 
bool contains (const QString &name) const noexcept
 
std::shared_ptr< T > value (int index) noexcept
 
std::shared_ptr< const T > value (int index) const noexcept
 
std::shared_ptr< T > find (const T *obj) noexcept
 
std::shared_ptr< T > find (const Uuid &key) noexcept
 
std::shared_ptr< const T > find (const Uuid &key) const noexcept
 
std::shared_ptr< T > find (const QString &name) noexcept
 
std::shared_ptr< const T > find (const QString &name) const noexcept
 
std::shared_ptr< const T > at (int index) const noexcept
 
std::shared_ptr< T > & first () noexcept
 
std::shared_ptr< const T > first () const noexcept
 
std::shared_ptr< T > & last () noexcept
 
std::shared_ptr< const T > last () const noexcept
 
std::shared_ptr< T > get (const T *obj)
 
std::shared_ptr< T > get (const Uuid &key)
 
std::shared_ptr< const T > get (const Uuid &key) const
 
std::shared_ptr< T > get (const QString &name)
 
std::shared_ptr< const T > get (const QString &name) const
 
const_iterator begin () const noexcept
 
const_iterator end () const noexcept
 
const_iterator cbegin () noexcept
 
const_iterator cend () noexcept
 
iterator begin () noexcept
 
iterator end () noexcept
 
int loadFromDomElement (const SExpression &node)
 
void swap (int i, int j) noexcept
 
int insert (int index, const std::shared_ptr< T > &obj) noexcept
 
int append (const std::shared_ptr< T > &obj) noexcept
 
std::shared_ptr< T > take (int index) noexcept
 
std::shared_ptr< T > take (const T *obj) noexcept
 
std::shared_ptr< T > take (const Uuid &uuid) noexcept
 
std::shared_ptr< T > take (const QString &name) noexcept
 
void remove (int index) noexcept
 
void remove (const T *obj) noexcept
 
void remove (const Uuid &uuid) noexcept
 
void remove (const QString &name) noexcept
 
void clear () noexcept
 
void serialize (SExpression &root) const override
 Serialize the object into an existing S-Expression node. More...
 
SerializableObjectList< T, P > sortedByUuid () const noexcept
 
SerializableObjectList< T, P > sortedByName () const noexcept
 
void registerObserver (IF_Observer *o) noexcept
 
void unregisterObserver (IF_Observer *o) noexcept
 
std::shared_ptr< T > operator[] (int i) noexcept
 
std::shared_ptr< const T > operator[] (int i) const noexcept
 
bool operator== (const SerializableObjectList< T, P > &rhs) const noexcept
 
bool operator!= (const SerializableObjectList< T, P > &rhs) const noexcept
 
SerializableObjectList< T, P > & operator= (const SerializableObjectList< T, P > &rhs) noexcept
 
SerializableObjectList< T, P > & operator= (SerializableObjectList< T, P > &&rhs) noexcept
 
- Public Member Functions inherited from SerializableObject
 SerializableObject () noexcept
 
virtual ~SerializableObject () noexcept
 
SExpression serializeToDomElement (const QString &name) const
 Serialize the object to a new S-Expression node. More...
 

Protected Member Functions

void notifyObjectAdded (int index, const std::shared_ptr< T > &obj) noexcept
 
void notifyObjectRemoved (int index, const std::shared_ptr< T > &obj) noexcept
 
void throwKeyNotFoundException (const Uuid &key) const
 
void throwNameNotFoundException (const QString &name) const
 

Protected Attributes

QVector< std::shared_ptr< T > > mObjects
 
QList< IF_Observer * > mObservers
 

Additional Inherited Members

- Static Public Member Functions inherited from SerializableObject
template<typename T >
static void serializeObjectContainer (SExpression &root, const T &container, const QString &itemName)
 
template<typename T >
static void serializePointerContainer (SExpression &root, const T &container, const QString &itemName)
 
template<typename T >
static void serializePointerContainerUuidSorted (SExpression &root, const T &container, const QString &itemName)
 

Detailed Description

template<typename T, typename P>
class librepcb::SerializableObjectList< T, P >

The SerializableObjectList class implements a list of librepcb::SerializableObject.

This template class lets you hold a list of serializable objects and provides some useful features:

  • The method loadFromDomElement() to deserialize from a librepcb::DomElement.
  • The method serialize() to serialize the whole list into a librepcb::DomElement.
  • Iterators (for example to use in C++11 range based for loops).
  • Methods to find elements by UUID and/or name (if supported by template type T).
  • Method sortedByUuid() to create a copy of the list with elements sorted by UUID.
  • Observer pattern to get notified about added and removed elements.
  • Undo commands librepcb::CmdListElementInsert, librepcb::CmdListElementRemove and ibrepcb::CmdListElementsSwap.
  • Const correctness: A const list always returns pointers/references to const elements.
Template Parameters
TThe type of the list items. The type must provide following functionality:
  • Optional: A nothrow copy constructor (to make the list copyable)
  • Optional: A constructor with one parameter of type const DomElement&
  • Optional: A method serialize() according to librepcb::SerializableObject
  • Optional: Comparison operator overloadings
  • Optional: A method Uuid getUuid() const noexcept
  • Optional: A method QString getName() const noexcept
PA class which provides the S-Expression node tag name of the list items. Example: struct MyNameProvider {static constexpr const char* tagname = "item";};
Note
Instead of directly storing elements of type T, elements are always wrapped into a std::shared_ptr<T> before adding them to the list. This is done to ensure that elements never have to be copied or moved for adding or removing them to/from the list. Otherwise it would not be possible to use this list in undo commands as references/pointers to elements would become invalid. Using pointers ensures that the objects are located at the same address over the whole lifetime. To still minimize the risk of memory leaks, std::shared_ptr is used instead of raw pointers.
Warning
Using Qt's foreach keyword on a SerializableObjectList is not recommended because it always creates a deep copy of the list! You should use range based for loops (since C++11) instead.

Member Typedef Documentation

using iterator = Iterator<typename QVector<std::shared_ptr<T>>::iterator, T>
using const_iterator = Iterator<typename QVector<std::shared_ptr<T>>::const_iterator, const T>

Constructor & Destructor Documentation

SerializableObjectList ( IF_Observer observer = nullptr)
inlineexplicitnoexcept
SerializableObjectList ( const SerializableObjectList< T, P > &  other,
IF_Observer observer = nullptr 
)
inlinenoexcept
SerializableObjectList ( SerializableObjectList< T, P > &&  other,
IF_Observer observer = nullptr 
)
inlinenoexcept
SerializableObjectList ( std::initializer_list< std::shared_ptr< T >>  elements,
IF_Observer observer = nullptr 
)
inlinenoexcept
SerializableObjectList ( std::initializer_list< T >  elements,
IF_Observer observer = nullptr 
)
inlinenoexcept
SerializableObjectList ( const SExpression node,
IF_Observer observer = nullptr 
)
inlineexplicit
virtual ~SerializableObjectList ( )
inlinevirtualnoexcept

Member Function Documentation

bool isEmpty ( ) const
inlinenoexcept

+ Here is the caller graph for this function:

int count ( ) const
inlinenoexcept

+ Here is the caller graph for this function:

std::vector<Uuid> getUuids ( ) const
inlinenoexcept
QSet<Uuid> getUuidSet ( ) const
inlinenoexcept

+ Here is the caller graph for this function:

int indexOf ( const T *  obj) const
inlinenoexcept

+ Here is the caller graph for this function:

int indexOf ( const Uuid key) const
inlinenoexcept
int indexOf ( const QString &  name) const
inlinenoexcept
bool contains ( int  index) const
inlinenoexcept

+ Here is the caller graph for this function:

bool contains ( const T *  obj) const
inlinenoexcept
bool contains ( const Uuid key) const
inlinenoexcept
bool contains ( const QString &  name) const
inlinenoexcept
std::shared_ptr<T> value ( int  index)
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<const T> value ( int  index) const
inlinenoexcept
std::shared_ptr<T> find ( const T *  obj)
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<T> find ( const Uuid key)
inlinenoexcept
std::shared_ptr<const T> find ( const Uuid key) const
inlinenoexcept
std::shared_ptr<T> find ( const QString &  name)
inlinenoexcept
std::shared_ptr<const T> find ( const QString &  name) const
inlinenoexcept
std::shared_ptr<const T> at ( int  index) const
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<T>& first ( )
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<const T> first ( ) const
inlinenoexcept
std::shared_ptr<T>& last ( )
inlinenoexcept
std::shared_ptr<const T> last ( ) const
inlinenoexcept
std::shared_ptr<T> get ( const T *  obj)
inline

+ Here is the caller graph for this function:

std::shared_ptr<T> get ( const Uuid key)
inline
std::shared_ptr<const T> get ( const Uuid key) const
inline
std::shared_ptr<T> get ( const QString &  name)
inline
std::shared_ptr<const T> get ( const QString &  name) const
inline
const_iterator begin ( ) const
inlinenoexcept

+ Here is the caller graph for this function:

const_iterator end ( ) const
inlinenoexcept

+ Here is the caller graph for this function:

const_iterator cbegin ( )
inlinenoexcept
const_iterator cend ( )
inlinenoexcept
iterator begin ( )
inlinenoexcept
iterator end ( )
inlinenoexcept
int loadFromDomElement ( const SExpression node)
inline

+ Here is the caller graph for this function:

void swap ( int  i,
int  j 
)
inlinenoexcept
int insert ( int  index,
const std::shared_ptr< T > &  obj 
)
inlinenoexcept

+ Here is the caller graph for this function:

int append ( const std::shared_ptr< T > &  obj)
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<T> take ( int  index)
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<T> take ( const T *  obj)
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<T> take ( const Uuid uuid)
inlinenoexcept
std::shared_ptr<T> take ( const QString &  name)
inlinenoexcept
void remove ( int  index)
inlinenoexcept
void remove ( const T *  obj)
inlinenoexcept
void remove ( const Uuid uuid)
inlinenoexcept
void remove ( const QString &  name)
inlinenoexcept
void clear ( )
inlinenoexcept

+ Here is the caller graph for this function:

void serialize ( SExpression root) const
inlineoverridevirtual

Serialize the object into an existing S-Expression node.

This method inserts/appends all attributes and childs of the object to an existing S-Expression node. The content which already exists in the given S-Expression node will not be removed.

Note
The generated S-Expression node has always the format of the application's major version (it's not possible to generate DOMs of older versions).
Parameters
rootThe target DOM root node
Exceptions
ExceptionThis method throws an exception if an error occurs.

Implements SerializableObject.

+ Here is the caller graph for this function:

SerializableObjectList<T, P> sortedByUuid ( ) const
inlinenoexcept

+ Here is the caller graph for this function:

SerializableObjectList<T, P> sortedByName ( ) const
inlinenoexcept
void registerObserver ( IF_Observer o)
inlinenoexcept

+ Here is the caller graph for this function:

void unregisterObserver ( IF_Observer o)
inlinenoexcept

+ Here is the caller graph for this function:

std::shared_ptr<T> operator[] ( int  i)
inlinenoexcept
std::shared_ptr<const T> operator[] ( int  i) const
inlinenoexcept
bool operator== ( const SerializableObjectList< T, P > &  rhs) const
inlinenoexcept
bool operator!= ( const SerializableObjectList< T, P > &  rhs) const
inlinenoexcept
SerializableObjectList<T, P>& operator= ( const SerializableObjectList< T, P > &  rhs)
inlinenoexcept
SerializableObjectList<T, P>& operator= ( SerializableObjectList< T, P > &&  rhs)
inlinenoexcept
void notifyObjectAdded ( int  index,
const std::shared_ptr< T > &  obj 
)
inlineprotectednoexcept

+ Here is the caller graph for this function:

void notifyObjectRemoved ( int  index,
const std::shared_ptr< T > &  obj 
)
inlineprotectednoexcept

+ Here is the caller graph for this function:

void throwKeyNotFoundException ( const Uuid key) const
inlineprotected

+ Here is the caller graph for this function:

void throwNameNotFoundException ( const QString &  name) const
inlineprotected

+ Here is the caller graph for this function:

Member Data Documentation

QVector<std::shared_ptr<T> > mObjects
protected
QList<IF_Observer*> mObservers
protected

The documentation for this class was generated from the following file: