creativity  v1.3.0
Agent-based model of creativity and piracy
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
creativity::gui::MemberStore< M > Class Template Referenceabstract

Base class for a flat list of simulation members. More...

#include <creativity/gui/MemberStore.hpp>

Public Member Functions

virtual void appendColumnsTo (Gtk::TreeView &v) const =0
 Takes a Gtk::TreeView and adds this object's columns to it. More...
 
const M & member (const Path &path) const
 Gets the M Member from a Path. More...
 
const M & member (const iterator &iter) const
 Gets the M Member from an iterator. More...
 
Path find (eris::eris_id_t id, size_t hint=0) const
 Returns the Path to the member with the given id. More...
 
Path find (eris::eris_id_t id, const iterator &iter) const
 Returns the Path to the member with the given id. More...
 
Path find (eris::eris_id_t id, const Path &hint) const
 Returns the Path to the member with the given id. More...
 

Protected Member Functions

 MemberStore (std::shared_ptr< const state::State > &&state)
 Protected constructor; this object should be constructed from a subclass, typically via the subclass's static create() method. More...
 
virtual Gtk::TreeModelFlags get_flags_vfunc () const override
 Returns Gtk::TreeModel flags (specifically, the LIST_ONLY flag). More...
 
virtual int get_n_columns_vfunc () const override=0
 Returns obj.columns.size(), the number of model columns. More...
 
virtual GType get_column_type_vfunc (int index) const override=0
 Returns the column type of the given position. More...
 
virtual bool get_iter_vfunc (const Path &path, iterator &iter) const override
 Converts a path to an iterator. More...
 
virtual bool iter_next_vfunc (const iterator &iter, iterator &iter_next) const override
 Takes an iterator, returns an iterator to the next item. More...
 
virtual bool iter_children_vfunc (const iterator &, iterator &) const override
 Returns false always: MemberStore elements cannot have children.
 
virtual bool iter_parent_vfunc (const iterator &, iterator &) const override
 Returns false always: MemberStore elemenets cannot have children/parents.
 
virtual bool iter_nth_child_vfunc (const iterator &, int, iterator &) const override
 Returns false always: MemberStore elements cannot have children.
 
virtual bool iter_has_child_vfunc (const iterator &) const override
 Returns false always: MemberStore elements cannot have children.
 
virtual int iter_n_children_vfunc (const iterator &) const override
 Returns 0 always: MemberStore elements have no children.
 
virtual bool iter_nth_root_child_vfunc (int n, iterator &iter) const override
 Obtains an iterator to the nth reader. More...
 
virtual int iter_n_root_children_vfunc () const override
 Returns the number of readers stored in this model.
 
virtual Path get_path_vfunc (const iterator &iter) const override
 Converts iterator iter into a Path.
 
virtual void get_value_vfunc (const iterator &iter, int column, Glib::ValueBase &value) const override=0
 Accesses a column value. More...
 
virtual bool get_sort_column_id_vfunc (int *sort_column_id, Gtk::SortType *order) const override
 Accesses the current sort column and order. More...
 
virtual void set_sort_column_id_vfunc (int sort_column_id, Gtk::SortType order) override=0
 Sets the model sort column and sort order. More...
 
virtual void sort_members (std::function< bool(const M &a, const M &b)> &compare, int sort_column_id, Gtk::SortType order)
 Called by subclasses, typically in set_sort_column_id_vfunc, to resort the current list of members using the given function. More...
 
template<typename T , typename = typename std::enable_if<std::is_base_of<Gtk::TreeModelColumnBase, T>::value>>
void appendCol (Gtk::TreeView &v, const std::string &label, T &col, int width, bool sortable=true) const
 Appends a single column to the given view using the given label, width, and sortability. More...
 

Static Protected Member Functions

template<typename T >
static void copy_value_ (Glib::ValueBase &valueobj, const T &val)
 Helper method for the crap needed in get_value_vfunc(): creates a Glib::Value of the right type and copies it into the given Glib::ValueBase. More...
 

Protected Attributes

const std::shared_ptr< const state::Statestate_
 The state this MemberStore represents.
 
std::vector< std::reference_wrapper< const M > > members_
 The vector of members. Subclasses need to add all member references to this vector.
 
int stamp_ = 1
 Tracks model changes by being incremented whenever such a change occurs.
 

Detailed Description

template<class M>
class creativity::gui::MemberStore< M >

Base class for a flat list of simulation members.

Note: the implementing class must inherit from Glib::ObjectBase and call the Glib::ObjectBase(typeid(CLASS)) constructor to register the type with Glib.

Parameters
Mthe state type being represented by this MemberStore such as BookState or ReaderState.

Constructor & Destructor Documentation

§ MemberStore()

template<class M >
creativity::gui::MemberStore< M >::MemberStore ( std::shared_ptr< const state::State > &&  state)
protected

Protected constructor; this object should be constructed from a subclass, typically via the subclass's static create() method.

Subclasses must inherit from Glib::ObjectBase and call the Glib::ObjectBase(typeid(CLASS)) constructor to register the type with Glib. See gui/ReaderStore.hpp for an example.

Parameters
statea reference to a simulation state object. The object's lifespan should exceed the lifespan of the MemberStore-derived object.
See also
ReaderStore
BookStore

Member Function Documentation

§ appendCol()

template<class M>
template<typename T , typename = typename std::enable_if<std::is_base_of<Gtk::TreeModelColumnBase, T>::value>>
void creativity::gui::MemberStore< M >::appendCol ( Gtk::TreeView &  v,
const std::string &  label,
T &  col,
int  width,
bool  sortable = true 
) const
inlineprotected

Appends a single column to the given view using the given label, width, and sortability.

Parameters
vthe TreeView to add the column to
labelthe string for the column header
colthe model column object, typically a member of store.columns such as readerstore.columns.id.
widththe fixed width of the column
sortableif omitted (or true) the column will be user-sortable by clicking on the column header; if specified explicitly as false, user sorting will be disabled.

§ appendColumnsTo()

template<class M>
virtual void creativity::gui::MemberStore< M >::appendColumnsTo ( Gtk::TreeView &  v) const
pure virtual

Takes a Gtk::TreeView and adds this object's columns to it.

Implemented in creativity::gui::BookStore, creativity::gui::ReaderStore, and creativity::gui::LibraryStore.

§ copy_value_()

template<class M >
template<class T >
void creativity::gui::MemberStore< M >::copy_value_ ( Glib::ValueBase &  valueobj,
const T &  val 
)
staticprotected

Helper method for the crap needed in get_value_vfunc(): creates a Glib::Value of the right type and copies it into the given Glib::ValueBase.

§ find() [1/3]

template<class M >
Gtk::TreeModel::Path creativity::gui::MemberStore< M >::find ( eris::eris_id_t  id,
size_t  hint = 0 
) const

Returns the Path to the member with the given id.

Returns an empty path if the id was not found.

This does a linear search of the current member list. If the optional hint is given, locations near hint are checked first; otherwise this does a linear search beginning from 0.

Parameters
idthe id to compare against members_[i].id to find the element's position.
hintif provided as a non-zero, this starts looking at and near the given position, iterating outward in both directions. This is a useful optimization when calling find() on a model that has a reasonable chance of having the elements in the same (or nearby) positions.

§ find() [2/3]

template<class M >
Gtk::TreeModel::Path creativity::gui::MemberStore< M >::find ( eris::eris_id_t  id,
const iterator &  iter 
) const

Returns the Path to the member with the given id.

The hint iterator is used to determine the position to check first; if it doesn't match the requested ID, a linear search of all members is performed.

See also
find(eris::eris_id_t, size_t)

§ find() [3/3]

template<class M >
Gtk::TreeModel::Path creativity::gui::MemberStore< M >::find ( eris::eris_id_t  id,
const Path &  hint 
) const

Returns the Path to the member with the given id.

The hint path is used to determine the position to check first; if it doesn't match the requested ID, a linear search of all members is performed.

See also
find(eris::eris_id_t, size_t)

§ get_column_type_vfunc()

template<class M>
virtual GType creativity::gui::MemberStore< M >::get_column_type_vfunc ( int  index) const
overrideprotectedpure virtual

Returns the column type of the given position.

This is typically invoked via get_column_type, itself given a column member of the .columns ColRec object.

See also
MemberStore::ColRec
BookStore::ColRec

Implemented in creativity::gui::BookStore, and creativity::gui::ReaderStore.

§ get_flags_vfunc()

template<class M >
Gtk::TreeModelFlags creativity::gui::MemberStore< M >::get_flags_vfunc ( ) const
overrideprotectedvirtual

Returns Gtk::TreeModel flags (specifically, the LIST_ONLY flag).

§ get_iter_vfunc()

template<class M >
bool creativity::gui::MemberStore< M >::get_iter_vfunc ( const Path &  path,
iterator &  iter 
) const
overrideprotectedvirtual

Converts a path to an iterator.

Parameters
paththe path (in)
iterthe iterator to set (out)
Returns
true and sets iter if the path refers to a valid element, returns false otherwise.

§ get_n_columns_vfunc()

template<class M>
virtual int creativity::gui::MemberStore< M >::get_n_columns_vfunc ( ) const
overrideprotectedpure virtual

Returns obj.columns.size(), the number of model columns.

Implemented in creativity::gui::BookStore, and creativity::gui::ReaderStore.

§ get_sort_column_id_vfunc()

template<class M >
bool creativity::gui::MemberStore< M >::get_sort_column_id_vfunc ( int *  sort_column_id,
Gtk::SortType *  order 
) const
overrideprotectedvirtual

Accesses the current sort column and order.

Returns true if accessed sort_column_id refers to a specific column (instead of the Gtk magic unsorted and default order constants).

Parameters
sort_column_ida pointer in which to store the sort column. May be a nullptr to skip accessing the sort column.
ordera pointer in which to store the sort order. May be a nullptr to skip accessing the sort order.

§ get_value_vfunc()

template<class M>
virtual void creativity::gui::MemberStore< M >::get_value_vfunc ( const iterator &  iter,
int  column,
Glib::ValueBase &  value 
) const
overrideprotectedpure virtual

Accesses a column value.

Parameters
itera valid iterator referencing the row to access
columnthe index of the column to access
valuea Glib::Value<TYPE> object (where TYPE is the appropriate type for the requested column) in which to store the value.

Implemented in creativity::gui::BookStore, creativity::gui::ReaderStore, and creativity::gui::LibraryStore.

§ iter_next_vfunc()

template<class M >
bool creativity::gui::MemberStore< M >::iter_next_vfunc ( const iterator &  iter,
iterator &  iter_next 
) const
overrideprotectedvirtual

Takes an iterator, returns an iterator to the next item.

Parameters
iterthe current element (in)
iter_nextthe next element (out)
Returns
true and sets iter_next if iter is valid (i.e. the model hasn't changed since the iterator was created) and there is a next element; returns false otherwise.

§ iter_nth_root_child_vfunc()

template<class M >
bool creativity::gui::MemberStore< M >::iter_nth_root_child_vfunc ( int  n,
iterator &  iter 
) const
overrideprotectedvirtual

Obtains an iterator to the nth reader.

Parameters
nthe index of the reader to access
iteran iterator to set to the requested reader
Returns
true and sets iter if n is valid (i.e. there are at least n+1 readers); false otherwise.

§ member() [1/2]

template<class M >
const M & creativity::gui::MemberStore< M >::member ( const Path &  path) const

Gets the M Member from a Path.

Throws an exception if the Path is invalid.

§ member() [2/2]

template<class M >
const M & creativity::gui::MemberStore< M >::member ( const iterator &  iter) const

Gets the M Member from an iterator.

Throws an exception if the iterator is invalid.

§ set_sort_column_id_vfunc()

template<class M>
virtual void creativity::gui::MemberStore< M >::set_sort_column_id_vfunc ( int  sort_column_id,
Gtk::SortType  order 
)
overrideprotectedpure virtual

Sets the model sort column and sort order.

If the sort_column and order differ from the current values, the model data is resorted. If resorting occurs, the sort_column_changed and rows_reordered signals will fire.

Implemented in creativity::gui::BookStore, creativity::gui::ReaderStore, and creativity::gui::LibraryStore.

§ sort_members()

template<class M>
void creativity::gui::MemberStore< M >::sort_members ( std::function< bool(const M &a, const M &b)> &  compare,
int  sort_column_id,
Gtk::SortType  order 
)
protectedvirtual

Called by subclasses, typically in set_sort_column_id_vfunc, to resort the current list of members using the given function.

This uses std::stable_sort, which means the current order is maintained for any members that are equal according to the given comparison function.

This fires sort_column_changed if the sort column and/or sort order are changing from their current values, and rows_reordered if the sorting actually changes any row orders.

If compare is not callable, this method does nothing.

Parameters
comparea function (or callable object) that returns true if its first arguments should (strictly) come before its second argument, and false otherwise.
sort_column_idthe new sort column
orderthe new sort order
Inheritance diagram for creativity::gui::MemberStore< M >:
[legend]
Collaboration diagram for creativity::gui::MemberStore< M >:
[legend]

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