#ifndef __JMAPELEMENT__ #define __JMAPELEMENT__ #include #include "JIO/JSerialisable.hh" #include "JTools/JCollection.hh" namespace JTOOLS { namespace { using JIO::JReader; using JIO::JWriter; } /** * Element of a map. * This class extends the std::pair<> class and implements the JComparator<>::JComparable interface. * * Note that this template class is a simple wrapper around the STL pair<> data structure. * It is primarily used to implement a template map for the JAbstractCollection<> interface. * * The constantness of the key that is defined explicitly inside the STL map class is * imposed through the member method getKey() of the JComparator<>::JComparable class. */ template class JMapElement : public std::pair, public JComparator::JComparable { public: typedef JKey_t key_type; typedef JValue_t mapped_type; typedef JMapElement value_type; typedef std::pair pair_type; typedef typename JComparator::JComparable JComparable_t; /** * Default constructor. */ JMapElement() : pair_type(), JComparable_t() {} /** * Constructor. * * \param key JKey_t */ JMapElement(const key_type& key) : pair_type(key, mapped_type()), JComparable_t() {} /** * Constructor. * * \param key JKey_t * \param value JValue_t */ JMapElement(const key_type& key, const mapped_type& value) : pair_type(key, value), JComparable_t() {} /** * Constructor. * * \param data pair */ JMapElement(const pair_type& data) : pair_type(data), JComparable_t() {} /** * Assignment operator. * * \param data pair * \return this object */ value_type& operator=(const pair_type& data) { this->first = data.first; this->second = const_cast(data.second); return *this; } const key_type& getKey() const { return this->first; } const mapped_type& getValue() const { return this->second; } mapped_type& getValue() { return this->second; } /** * Read template JMapElement from input. * * \param in JReader * \param element JMapElement * \return JReader */ friend inline JReader& operator>>(JReader& in, JMapElement& element) { in >> element.first; in >> element.second; return in; } /** * Write template JMapElement to output. * * \param out JWriter * \param element JMapElement * \return JWriter */ friend inline JWriter& operator<<(JWriter& out, const JMapElement& element) { out << element.first; out << element.second; return out; } }; } #endif