Files
pip/libs/main/containers/picontainers.h

199 lines
7.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*! \addtogroup Containers
* \{
* \file picontainers.h
* \~\brief
* \~english Base macros for generic containers
* \~russian Базовые макросы для контейнеров
* \~\authors
* \~english
* Ivan Pelipenko peri4ko@yandex.ru;
* Andrey Bychkov work.a.b@yandex.ru;
* \~russian
* Иван Пелипенко peri4ko@yandex.ru;
* Андрей Бычков work.a.b@yandex.ru;
* \~\} */
/*
PIP - Platform Independent Primitives
Base macros for generic containers
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PICONTAINERS_H
#define PICONTAINERS_H
#include "picout.h"
#include "piintrospection_containers.h"
#ifdef MAC_OS
# include <stdlib.h>
#else
# include <malloc.h>
#endif
#include <initializer_list>
#include <type_traits>
#include <string.h>
#include <new>
#if 0
/*!\brief Macro for iterate any container
* \details Use this macros instead of standard "for"
* to get read/write access to each element of container.
* Pass direction is direct \n
* Example: \snippet picontainers.cpp foreach
*/
# define piForeach(i,c)
/*!\brief Macro for iterate any container only for read
* \details Use this macros instead of standard "for"
* to get read access to each element of container.
* Pass direction is direct \n
* Example: \snippet picontainers.cpp foreachC
*/
# define piForeachC(i,c)
/*!\brief Macro for iterate any container with reverse direction
* \details Use this macros instead of standard "for"
* to get read/write access to each element of container.
* Pass direction is reverse \n
* Example: \snippet picontainers.cpp foreachR
*/
# define piForeachR(i,c)
/*!\brief Macro for iterate any container only for read with reverse direction
* \details Use this macros instead of standard "for"
* to get read access to each element of container.
* Pass direction is reverse \n
* Example: \snippet picontainers.cpp foreachCR
*/
# define piForeachCR(i,c)
#endif
template <typename C>
class _PIReverseWrapper {
public:
_PIReverseWrapper(C & c): c_(c) {}
_PIReverseWrapper(const C & c): c_(const_cast<C&>(c)) {}
typename C::reverse_iterator begin() {return c_.rbegin();}
typename C::reverse_iterator end() {return c_.rend(); }
typename C::const_reverse_iterator begin() const {return c_.rbegin();}
typename C::const_reverse_iterator end() const {return c_.rend(); }
private:
C & c_;
};
template <typename C> _PIReverseWrapper<C> PIReverseWrap(C & c) {return _PIReverseWrapper<C>(c);}
template <typename C> _PIReverseWrapper<C> PIReverseWrap(const C & c) {return _PIReverseWrapper<C>(c);}
/*! \brief
* \~english Macro for short replacement of standart "for"
* \~russian Макрос для короткой записи стандартного цикла "for"
* \~\param c
* \~english Iteration times in loop
* \~russian Количество итераций цикла
*/
#define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c)
/*! \brief
* \~english Macro for iterate any container
* \~russian Макрос для перебора любых контейнеров
* \~\deprecated
* \~english Deprecated, using only for backward compatibility. Use
* [C++ Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for).
* \~russian Устарело, используется только для обратной совместимости. Используйте
* [C++ Range-based for loop](https://ru.cppreference.com/w/cpp/language/range-for).
* \~\details
* \~english Get read/write access to each element of container.
* Iterating in forward direction.
* Example of using:
* \~russian Перебор всех элементов контейнера с доступом на чтение и запись.
* Перебор осуществляется в прямом порядке.
* Пример использования:
* \~\code
* PIVector<int> vec;
* vec << 1 << 2 << 3;
* piForeach(int & i, vec) piCout << i;
* // 1
* // 2
* // 3
* piForeach(int & i, vec) i++;
* piForeach(int & i, vec) piCout << i;
* // 2
* // 3
* // 4
* \endcode
* \sa \a piForeachC, \a piForeachR, \a piForeachRC
*/
#define piForeach(i, c) for(i : c)
/*! \brief
* \~english Macro for iterate any container
* \~russian Макрос для перебора любых контейнеров
* \~\deprecated
* \~english Deprecated, using only for backward compatibility. Use
* [C++ Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for).
* \~russian Устарело, используется только для обратной совместимости. Используйте
* [C++ Range-based for loop](https://ru.cppreference.com/w/cpp/language/range-for).
* \~\details
* \~english Get read only access to each element of container.
* Iterating in forward direction.
* \~russian Перебор всех элементов контейнера с доступом только на чтение.
* Перебор осуществляется в прямом порядке.
* \~ \sa \a piForeach, \a piForeachR, \a piForeachRC
*/
#define piForeachC(i, c) for(const i : c)
/*! \brief
* \~english Macro for iterate any container
* \~russian Макрос для перебора любых контейнеров
* \~\deprecated
* \~english Deprecated, using only for backward compatibility. Use
* [C++ Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for).
* \~russian Устарело, используется только для обратной совместимости. Используйте
* [C++ Range-based for loop](https://ru.cppreference.com/w/cpp/language/range-for).
* \~\details
* \~english Get read/write access to each element of container.
* Iterating in backward direction.
* \~russian Перебор всех элементов контейнера с доступом на чтение и запись.
* Перебор осуществляется в обратном порядке.
* \~ \sa \a piForeach, \a piForeachC, \a piForeachRC
*/
#define piForeachR(i, c) for(i : PIReverseWrap(c))
/*! \brief
* \~english Macro for iterate any container
* \~russian Макрос для перебора любых контейнеров
* \~\deprecated
* \~english Deprecated, using only for backward compatibility. Use
* [C++ Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for).
* \~russian Устарело, используется только для обратной совместимости. Используйте
* [C++ Range-based for loop](https://ru.cppreference.com/w/cpp/language/range-for).
* \~\details
* \~english Get read only access to each element of container.
* Iterating in backward direction. Also has alias **piForeachCR**
* \~russian Перебор всех элементов контейнера с доступом только на чтение.
* Перебор осуществляется в обратном порядке. Также можно писать **piForeachCR**
* \~ \sa \a piForeach, \a piForeachC, \a piForeachR
*/
#define piForeachRC(i, c) for(const i : PIReverseWrap(c))
#define piForeachCR piForeachRC
#endif // PICONTAINERS_H