Commit be821f3e authored by Pascal Palenda's avatar Pascal Palenda
Browse files

Move enum utils to data type utils

parent 26679a28
......@@ -12,7 +12,7 @@
#include <ITA/SimulationScheduler/definitions.h>
// simulation scheduler includes
#include "../src/ITA/SimulationScheduler/utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
// RAVEN includes
#include "RG_Vector.h"
......
......@@ -6,6 +6,7 @@
#include <complex>
#include <fstream>
#include <array>
#include <type_traits>
// API includes
#include <ITA/SimulationScheduler/definitions.h>
......@@ -262,4 +263,187 @@ namespace ITA
} // namespace SimulationScheduler
} // namespace ITA
template <typename Enumeration>
auto AsInteger ( Enumeration const value )
-> typename std::underlying_type<Enumeration>::type
{
return static_cast< typename std::underlying_type<Enumeration>::type >( value );
}
///
/// The use of scoped enums for bit flags requires some additional overloads.
/// The ideas for this implementation were taken from
/// - http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/
/// - https://github.com//grisumbras/enum-flags
/// To work correctly this need C++17!! Update the compiler?
///
template<class Enum>
struct EnableBitMaskOperators : public std::false_type
{
};
/*struct empty_t
{
explicit empty_t ( ) = default;
};
template <class Enum> class Flag
{
public:
static_assert( EnableBitMaskOperators<Enum>::value,
"Flags are not allowed for this type; "
"use ENABLE_BITMASK_OPERATORS macro." );
using enum_t = typename std::decay<Enum>::type;
using underlying_t = typename std::underlying_type_t<enum_t>;
using impl_t = typename std::make_unsigned<underlying_t>::type;
Flag ( ) = default;
Flag ( const Flag& fl ) = default;
Flag& operator=( const Flag& fl ) = default;
explicit Flag ( empty_t t ) : underlyingVal ( 0 )
{ }
Flag ( enum_t e )
: underlyingVal ( static_cast< impl_t >( e ) )
{ }
Flag& operator=( enum_t e )
{
underlyingVal = static_cast< impl_t >( e );
return *this;
}
explicit operator bool ( ) const
{
return underlyingVal != 0;
}
bool operator!( ) const
{
return !underlyingVal;
}
friend bool operator==( Flag fl1, Flag fl2 )
{
return fl1.underlyingVal == fl2.underlyingVal;
}
friend bool operator!=( Flag fl1, Flag fl2 )
{
return fl1.underlyingVal != fl2.underlyingVal;
}
Flag operator~( )
{
return Flag ( ~underlyingVal );
}
Flag& operator|=( const Flag& fl )
{
underlyingVal |= fl.underlyingVal;
return *this;
}
Flag& operator&=( const Flag& fl )
{
underlyingVal &= fl.underlyingVal;
return *this;
}
Flag& operator^=( const Flag& fl )
{
underlyingVal ^= fl.underlyingVal;
return *this;
}
Flag& operator|=( enum_t e )
{
underlyingVal |= static_cast< underlying_t >( e );
return *this;
}
Flag& operator&=( enum_t e )
{
underlyingVal &= static_cast< underlying_t >( e );
return *this;
}
Flag& operator^=( enum_t e )
{
underlyingVal ^= static_cast< underlying_t >( e );
return *this;
}
friend Flag operator|( Flag f1, Flag f2 )
{
return static_cast< enum_t > ( static_cast< underlying_t >( f1.underlyingVal | f2.underlyingVal ) );
}
friend Flag operator&( Flag f1, Flag f2 )
{
return static_cast< enum_t > ( static_cast< underlying_t >( f1.underlyingVal & f2.underlyingVal ) );
}
friend Flag operator^( Flag f1, Flag f2 )
{
return static_cast< enum_t > ( static_cast< underlying_t >( f1.underlyingVal ^ f2.underlyingVal ) );
}
private:
explicit Flag ( impl_t val ) : underlyingVal ( val )
{ }
impl_t underlyingVal;
};*/
template<typename Enum>
auto operator |( Enum lhs, Enum rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type
{
using Underlying = typename std::underlying_type<Enum>::type;
return static_cast< Enum > (
static_cast< Underlying >( lhs ) |
static_cast< Underlying >( rhs )
);
}
template<typename Enum>
auto operator &( Enum lhs, Enum rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type
{
using Underlying = typename std::underlying_type<Enum>::type;
return static_cast< Enum > (
static_cast< Underlying >( lhs ) &
static_cast< Underlying >( rhs )
);
}
template<typename Enum>
auto operator |=( Enum& lhs, const Enum& rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type&
{
using Underlying = typename std::underlying_type<Enum>::type;
auto lhsTmp = static_cast< Underlying >( lhs ) |
static_cast< Underlying >( rhs );
return lhs = static_cast< Enum > ( lhsTmp );
}
template<typename Enum>
auto operator &=( Enum& lhs, const Enum& rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type&
{
using Underlying = typename std::underlying_type<Enum>::type;
auto lhsTmp = static_cast< Underlying >( lhs ) &
static_cast< Underlying >( rhs );
return lhs = static_cast< Enum > ( lhsTmp );
}
template<typename Enum>
auto IsSet ( const Enum& enumToTest, const Enum& enumForTest ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value,
bool >::type
{
return ( enumToTest & enumForTest ) == enumForTest;
}
#define ENABLE_BITMASK_OPERATORS(x) template<> struct EnableBitMaskOperators<x> : std::true_type {};
#endif // INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS_DATA_TYPE_UTILS
\ No newline at end of file
......@@ -5,7 +5,7 @@
#include <cmath>
// simulation scheduler include
#include "../src/ITA/SimulationScheduler/utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
// Vista include
#include <VistaAspects/VistaSerializer.h>
......
......@@ -9,13 +9,12 @@
#include <ITA/SimulationScheduler/AudibilityFilter/translation_filter.h>
#include <ITA/SimulationScheduler/AudibilityFilter/zone_filter.h>
#include <ITA/SimulationScheduler/AudibilityFilter/perceptive_rotation_filter.h>
#include <ITA/SimulationScheduler/Utils/utils.h>
// ITA includes
#include <ITAException.h>
#include <ITAStringUtils.h>
#include "../utils.h"
namespace ITA
{
namespace SimulationScheduler
......
......@@ -4,7 +4,7 @@
#include <ITA/SimulationScheduler/update_scene.h>
#include <ITA/SimulationScheduler/Profiler/profiler.h>
#include "../configuration_keys.h"
#include "../utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
#include "ITAException.h"
......
......@@ -2,7 +2,7 @@
#include <ITA/SimulationScheduler/RoomAcoustics/Raven/simulator.h>
#include <ITA/SimulationScheduler/Profiler/profiler.h>
#include "../src/ITA/SimulationScheduler/utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
#include <RD_DebugPrintf.h>
......
......@@ -2,7 +2,7 @@
#include <ITA/SimulationScheduler/RoomAcoustics/rir_simulation_result.h>
// simulation scheduler include
#include "../src/ITA/SimulationScheduler/utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
// Vista include
#include <VistaAspects/VistaSerializer.h>
......
......@@ -3,6 +3,7 @@
// simulation scheduler include
#include <ITA/SimulationScheduler/RoomAcoustics/Raven/worker_thread.h>
#include <ITA/SimulationScheduler/Utils/utils.h>
#include "../configuration_keys.h"
namespace ITA
......
......@@ -14,7 +14,6 @@ set( DirFiles
update_scene.cpp
serialization_helper.h
serialization_helper.cpp
utils.h
simulation_result.cpp
ir_simulation_result.cpp
scheduler.cpp
......
......@@ -2,7 +2,7 @@
#include <ITA/SimulationScheduler/update_config.h>
// simulation scheduler include
#include "../src/ITA/SimulationScheduler/utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
// Vista include
#include <VistaAspects/VistaSerializer.h>
......
#ifndef INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS
#define INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS
#include <type_traits>
template <typename Enumeration>
auto AsInteger ( Enumeration const value )
-> typename std::underlying_type<Enumeration>::type
{
return static_cast< typename std::underlying_type<Enumeration>::type >( value );
}
///
/// The use of scoped enums for bit flags requires some additional overloads.
/// The ideas for this implementation were taken from
/// - http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/
/// - https://github.com//grisumbras/enum-flags
/// To work correctly this need C++17!! Update the compiler?
///
template<class Enum>
struct EnableBitMaskOperators : public std::false_type
{
};
/*struct empty_t
{
explicit empty_t ( ) = default;
};
template <class Enum> class Flag
{
public:
static_assert( EnableBitMaskOperators<Enum>::value,
"Flags are not allowed for this type; "
"use ENABLE_BITMASK_OPERATORS macro." );
using enum_t = typename std::decay<Enum>::type;
using underlying_t = typename std::underlying_type_t<enum_t>;
using impl_t = typename std::make_unsigned<underlying_t>::type;
Flag ( ) = default;
Flag ( const Flag& fl ) = default;
Flag& operator=( const Flag& fl ) = default;
explicit Flag ( empty_t t ) : underlyingVal ( 0 )
{ }
Flag ( enum_t e )
: underlyingVal ( static_cast< impl_t >( e ) )
{ }
Flag& operator=( enum_t e )
{
underlyingVal = static_cast< impl_t >( e );
return *this;
}
explicit operator bool ( ) const
{
return underlyingVal != 0;
}
bool operator!( ) const
{
return !underlyingVal;
}
friend bool operator==( Flag fl1, Flag fl2 )
{
return fl1.underlyingVal == fl2.underlyingVal;
}
friend bool operator!=( Flag fl1, Flag fl2 )
{
return fl1.underlyingVal != fl2.underlyingVal;
}
Flag operator~( )
{
return Flag ( ~underlyingVal );
}
Flag& operator|=( const Flag& fl )
{
underlyingVal |= fl.underlyingVal;
return *this;
}
Flag& operator&=( const Flag& fl )
{
underlyingVal &= fl.underlyingVal;
return *this;
}
Flag& operator^=( const Flag& fl )
{
underlyingVal ^= fl.underlyingVal;
return *this;
}
Flag& operator|=( enum_t e )
{
underlyingVal |= static_cast< underlying_t >( e );
return *this;
}
Flag& operator&=( enum_t e )
{
underlyingVal &= static_cast< underlying_t >( e );
return *this;
}
Flag& operator^=( enum_t e )
{
underlyingVal ^= static_cast< underlying_t >( e );
return *this;
}
friend Flag operator|( Flag f1, Flag f2 )
{
return static_cast< enum_t > ( static_cast< underlying_t >( f1.underlyingVal | f2.underlyingVal ) );
}
friend Flag operator&( Flag f1, Flag f2 )
{
return static_cast< enum_t > ( static_cast< underlying_t >( f1.underlyingVal & f2.underlyingVal ) );
}
friend Flag operator^( Flag f1, Flag f2 )
{
return static_cast< enum_t > ( static_cast< underlying_t >( f1.underlyingVal ^ f2.underlyingVal ) );
}
private:
explicit Flag ( impl_t val ) : underlyingVal ( val )
{ }
impl_t underlyingVal;
};*/
template<typename Enum>
auto operator |( Enum lhs, Enum rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type
{
using Underlying = typename std::underlying_type<Enum>::type;
return static_cast< Enum > (
static_cast< Underlying >( lhs ) |
static_cast< Underlying >( rhs )
);
}
template<typename Enum>
auto operator &( Enum lhs, Enum rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type
{
using Underlying = typename std::underlying_type<Enum>::type;
return static_cast< Enum > (
static_cast< Underlying >( lhs ) &
static_cast< Underlying >( rhs )
);
}
template<typename Enum>
auto operator |=( Enum& lhs, const Enum& rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type&
{
using Underlying = typename std::underlying_type<Enum>::type;
auto lhsTmp = static_cast< Underlying >( lhs ) |
static_cast< Underlying >( rhs );
return lhs = static_cast< Enum > ( lhsTmp );
}
template<typename Enum>
auto operator &=( Enum& lhs, const Enum& rhs ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value, Enum>::type&
{
using Underlying = typename std::underlying_type<Enum>::type;
auto lhsTmp = static_cast< Underlying >( lhs ) &
static_cast< Underlying >( rhs );
return lhs = static_cast< Enum > ( lhsTmp );
}
template<typename Enum>
auto IsSet ( const Enum& enumToTest, const Enum& enumForTest ) -> typename std::enable_if<EnableBitMaskOperators<Enum>::value,
bool >::type
{
return ( enumToTest & enumForTest ) == enumForTest;
}
#define ENABLE_BITMASK_OPERATORS(x) template<> struct EnableBitMaskOperators<x> : std::true_type {};
#endif // INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS
\ No newline at end of file
......@@ -14,7 +14,7 @@
#include <ostream>
#include "../../src/ITA/SimulationScheduler/utils.h"
#include <ITA/SimulationScheduler/Utils/utils.h>
#include "gtest/gtest.h"
#include "ITAConstants.h"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment