Files

294 lines
8.8 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/UiContainerHelpers.h
*
* @brief U/i container helpers functions.
*/
#if (!defined ACTIV_FRAMEWORK_UI_CONTAINER_HELPERS_H)
#define ACTIV_FRAMEWORK_UI_CONTAINER_HELPERS_H
#include "ActivMiddleware/ActivFramework/UiIo.h"
#include ACTIV_BOOST_INCLUDE(type_traits/remove_const.hpp)
//! Activ namespace.
namespace Activ
{
//! UiContainerHelpers namespace.
namespace UiContainerHelpers
{
//! Functor to get the key from a map iterator.
template <typename MAP>
class GetMapKey
{
public:
typedef typename MAP::key_type key_type;
typedef typename MAP::const_iterator const_iterator;
const key_type &operator()(const const_iterator &iterator) const
{
return iterator->first;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
//! Functor to get the key from a set iterator.
template <typename SET>
class GetSetKey
{
public:
typedef typename SET::key_type key_type;
typedef typename SET::const_iterator const_iterator;
const key_type &operator()(const const_iterator &iterator) const
{
return *iterator;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
//! Functor to get the key from a multi index iterator.
template <typename INDEX>
class GetMultiIndexKey
{
public:
typedef typename INDEX::key_type key_type;
typedef typename INDEX::const_iterator const_iterator;
inline GetMultiIndexKey(const INDEX &index) :
m_keyExtractor(index.key_extractor())
{
};
const key_type &operator()(const const_iterator &iterator) const
{
return m_keyExtractor(*iterator);
}
private:
typename INDEX::key_from_value m_keyExtractor;
};
} // namespace UiContainerHelpers
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display an ordered container one page at a time, starting from startKey. The container can be modified between pages.
*
* @tparam CONTAINER the container type.
* @tparam F functor to convert the container entries to strings, with signature String F(const CONTAINER::value_type &)
* @tparam GET_KEY functor to get the key from an iterator.
*
* @param container
* @param uiIo
* @param function function to display the container entry.
* @param header
* @param startKey key to start displaying the container from.
* @param getKey function to get the key from an iterator.
*/
template <typename CONTAINER, typename F, typename GET_KEY>
inline void UiDisplayContainer(const CONTAINER &container, const UiIo &uiIo, const F &function, const String &header,
const typename CONTAINER::key_type &startKey, const GET_KEY &getKey)
{
typedef typename CONTAINER::key_type Key;
typedef typename Boost::remove_const<Key>::type NonConstKey;
typedef typename CONTAINER::const_iterator const_iterator;
static const size_t DEFAULT_NUMBER_OF_LINES = 22;
ConsoleWindow::Coord windowSize;
uiIo.GetWindowSize(windowSize);
const size_t linesToDisplay = ((windowSize.m_y > 2) ? windowSize.m_y - 2 : DEFAULT_NUMBER_OF_LINES);
if (container.empty())
{
uiIo.PutString(header + "\n");
return;
}
bool isFirst = true;
NonConstKey key = startKey;
const_iterator iterator = container.lower_bound(key);
do
{
uiIo.PutString(header + "\n");
if (!isFirst)
{
iterator = container.lower_bound(key);
if (container.end() != iterator)
++iterator;
}
isFirst = false;
for (size_t i = 0; (i < linesToDisplay) && (container.end() != iterator); ++iterator)
{
const String &line = function(*iterator);
if (!line.empty())
{
++i;
uiIo.PutString(function(*iterator) + "\n");
}
key = getKey(iterator);
}
}
while ((container.end() != iterator) && (STATUS_CODE_SUCCESS == uiIo.GetConfirmation("Continue", true)));
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display a map one page at a time. The map can be modified between pages.
*
* @tparam MAP the map type.
* @tparam F functor to convert the map entries to strings, with signature String F(const MAP::value_type &)
*
* @param map
* @param uiIo
* @param function function to display the map entry.
* @param header optional header.
*/
template <typename MAP, typename F>
inline void UiDisplayMap(const MAP &map, const UiIo &uiIo, const F &function, const String &header = String())
{
if (map.empty())
{
if (!header.empty())
uiIo.PutString(header + "\n");
return;
}
UiDisplayMap(map, uiIo, function, header, map.begin()->first);
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display a map one page at a time, starting from startKey. The map can be modified between pages.
*
* @tparam MAP the map type.
* @tparam F functor to convert the map entries to strings, with signature String F(const MAP::value_type &)
*
* @param map
* @param uiIo
* @param function function to display the map entry.
* @param header optional header.
* @param startKey the start key.
*/
template <typename MAP, typename F>
inline void UiDisplayMap(const MAP &map, const UiIo &uiIo, const F &function, const String &header,
const typename MAP::key_type &startKey)
{
UiDisplayContainer(map, uiIo, function, header, startKey, UiContainerHelpers::GetMapKey<MAP>());
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display a set one page at a time. The set can be modified between pages.
*
* @tparam SET the set type.
* @tparam F functor to convert the set entries to strings, with signature String F(const SET::value_type &)
*
* @param set
* @param uiIo
* @param function function to display the set entry.
* @param header optional header.
*/
template <typename SET, typename F>
inline void UiDisplaySet(const SET &set, const UiIo &uiIo, const F &function, const String &header = String())
{
if (set.empty())
{
if (!header.empty())
uiIo.PutString(header + "\n");
return;
}
UiDisplaySet(set, uiIo, function, header, *(set.begin()));
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display a set one page at a time, starting from startKey. The set can be modified between pages.
*
* @tparam SET the set type.
* @tparam F functor to convert the set entries to strings, with signature String F(const SET::value_type &)
*
* @param set
* @param uiIo
* @param function function to display the set entry.
* @param header optional header.
* @param startKey the start key.
*/
template <typename SET, typename F>
inline void UiDisplaySet(const SET &set, const UiIo &uiIo, const F &function, const String &header,
const typename SET::key_type &startKey)
{
UiDisplayContainer(set, uiIo, function, header, startKey, UiContainerHelpers::GetSetKey<SET>());
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display a multi index one page at a time. The index can be modified between pages.
*
* @tparam INDEX the index type.
* @tparam F functor to convert the index entries to strings, with signature String F(const INDEX::value_type &)
*
* @param index
* @param uiIo
* @param function function to display the index entry.
* @param header optional header.
*/
template <typename INDEX, typename F>
inline void UiDisplayMultiIndex(const INDEX &index, const UiIo &uiIo, const F &function, const String &header = String())
{
if (index.empty())
{
if (!header.empty())
uiIo.PutString(header + "\n");
return;
}
UiDisplayMultiIndex(index, uiIo, function, header, index.key_extractor()(*index.begin()));
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Display a multi index one page at a time, starting from startKey. The index can be modified between pages.
*
* @tparam INDEX the index type.
* @tparam F functor to convert the index entries to strings, with signature String F(const INDEX::value_type &)
*
* @param index
* @param uiIo
* @param function function to display the index entry.
* @param header optional header.
* @param startKey the start key.
*/
template <typename INDEX, typename F>
inline void UiDisplayMultiIndex(const INDEX &index, const UiIo &uiIo, const F &function, const String &header,
const typename INDEX::key_type &startKey)
{
UiDisplayContainer(index, uiIo, function, header, startKey, UiContainerHelpers::GetMultiIndexKey<INDEX>(index));
}
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_UI_CONTAINER_HELPERS_H)