插入一序列的key-value的map

MapAssign is used to insert a series of key-value pair into a map. it will help to statically initialize a constant map.

template
class MapAssign
{
public:
    typedef std::map   Map;
    typedef std::pair  Pair;
    typedef std::deque Deque;

    MapAssign(void)
    try : m_qPairs()
    {
    }
    catch (...)
    {
        throw std::exception();
    }

    ~MapAssign(void)
    {
    }

    /// \brief overloading of operator ().
    ///        it enables you to use MapAssign in this
    ///        style:
    ///        MapAssign()(key1, value1)(key2, value2);
    ///
    /// \param[in] kKey   key of a single map element.
    /// \param[in] kValue value of a single map element.
    ///
    /// \return reference of current MapAssign object.
    MapAssign& operator()(const K &kKey, const V &kValue)
    {
        try
        {
            m_qPairs.push_back(Pair(kKey, kValue));

            return *this;
        }
        catch (...)
        {
            throw std::exception();
        }
    }

    /// \brief overloading of operator std::map
    ///
    /// \return an object of std::map.
    operator Map() const
    {
        try
        {
            return Map(m_qPairs.begin(), m_qPairs.end());
        }
        catch (...)
        {
            throw std::exception();
        }
    }

private:
    DISALLOW_COPY_AND_ASSIGN(MapAssign);

    Deque m_qPairs;
};

你可能感兴趣的:(C++)