ARAStdVectorUtilities.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //------------------------------------------------------------------------------
  2. //! \file ARAStdVectorUtilities.h
  3. //! convenience functions to ease casting, searching and modifying
  4. //! STL's std::vector, e.g. when searching derived class pointers
  5. //! in base class vectors and vice versa
  6. //! \project ARA SDK Library
  7. //! \copyright Copyright (c) 2018-2021, Celemony Software GmbH, All Rights Reserved.
  8. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  9. //! you may not use this file except in compliance with the License.
  10. //! You may obtain a copy of the License at
  11. //!
  12. //! http://www.apache.org/licenses/LICENSE-2.0
  13. //!
  14. //! Unless required by applicable law or agreed to in writing, software
  15. //! distributed under the License is distributed on an "AS IS" BASIS,
  16. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. //! See the License for the specific language governing permissions and
  18. //! limitations under the License.
  19. //------------------------------------------------------------------------------
  20. #ifndef ARAStdVectorUtilities_h
  21. #define ARAStdVectorUtilities_h
  22. #include <vector>
  23. #include <algorithm>
  24. namespace ARA {
  25. //! @addtogroup ARA_Library_Utility_StdVec
  26. //! @{
  27. /*******************************************************************************/
  28. //! @name std::vector casts
  29. //! Up-casting vectors of base class pointers to vectors of derived class pointers.
  30. //@{
  31. template <typename T, typename U, typename std::enable_if<(sizeof (T) == sizeof (U)) && (std::is_convertible<T, U>::value || std::is_convertible<U, T>::value), bool>::type = true>
  32. inline std::vector<T>& vector_cast (std::vector<U>& container) noexcept
  33. {
  34. return reinterpret_cast<std::vector<T>&> (container);
  35. }
  36. template <typename T, typename U, typename std::enable_if<(sizeof (T) == sizeof (U)) && (std::is_convertible<T, U>::value || std::is_convertible<U, T>::value), bool>::type = true>
  37. inline std::vector<T> const& vector_cast (std::vector<U> const& container) noexcept
  38. {
  39. return reinterpret_cast<std::vector<T> const&> (container);
  40. }
  41. //@}
  42. /*******************************************************************************/
  43. //! @name find_erase
  44. //! Find an element in the vector and, if found, erase it from the vector.
  45. //! Returns true if found & erased, otherwise false.
  46. //@{
  47. template <typename T, typename U, typename std::enable_if<std::is_convertible<T, U>::value || std::is_convertible<U, T>::value, bool>::type = true>
  48. inline bool find_erase (std::vector<T>& container, const U element) noexcept
  49. {
  50. const auto it { std::find (container.begin (), container.end (), element) };
  51. if (it == container.end ())
  52. return false;
  53. container.erase (it);
  54. return true;
  55. }
  56. //@}
  57. /*******************************************************************************/
  58. //! @name contains
  59. //! Determine if an element exists in a vector.
  60. //@{
  61. template <typename T, typename U, typename std::enable_if<std::is_convertible<T, U>::value || std::is_convertible<U, T>::value, bool>::type = true>
  62. inline bool contains (std::vector<T> const& container, const U element) noexcept
  63. {
  64. return std::find (container.begin (), container.end (), element) != container.end ();
  65. }
  66. //@}
  67. //! @} ARA_Library_Utility_StdVec
  68. } // namespace ARA
  69. #endif // ARAStdVectorUtilities_h