344 lines
8.5 KiB
C++
344 lines
8.5 KiB
C++
/**
|
|
* @file include/ActivOneApi/MapKey.h
|
|
*
|
|
* @brief Header for the MapKey class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_ONE_API_MAP_KEY_H)
|
|
#define ACTIV_ONE_API_MAP_KEY_H
|
|
|
|
#include "ActivOneApi/External.h"
|
|
|
|
#include "ActivOneApi/TypeDefinitions.h"
|
|
|
|
#include "ActivMiddleware/CxxYZ/functional.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! OneApi namespace.
|
|
namespace OneApi
|
|
{
|
|
|
|
//! MapKey class.
|
|
class MapKey
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
MapKey();
|
|
|
|
/**
|
|
* @brief Construct from buffer.
|
|
*
|
|
* @param pBuffer
|
|
* @param length
|
|
*/
|
|
MapKey(const void *pBuffer, const uint8_t length);
|
|
|
|
/**
|
|
* @brief Copy constructor.
|
|
*
|
|
* @param rhs
|
|
*/
|
|
MapKey(const MapKey &rhs);
|
|
|
|
//! Destructor.
|
|
~MapKey();
|
|
|
|
/**
|
|
* @brief Copy assignment operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return a reference to this object.
|
|
*/
|
|
MapKey &operator=(const MapKey &rhs);
|
|
|
|
/**
|
|
* @brief Return buffer.
|
|
*
|
|
* @return A pointer to the buffer containing the key.
|
|
*/
|
|
const void *GetBuffer() const;
|
|
|
|
/**
|
|
* @brief Return buffer length.
|
|
*
|
|
* @return The length of the buffer returned by GetBuffer().
|
|
*/
|
|
uint8_t GetLength() const;
|
|
|
|
/**
|
|
* @brief Is the MapKey initialized.
|
|
*
|
|
* @return Whether this object is initialized.
|
|
*/
|
|
bool IsInitialized() const;
|
|
|
|
/**
|
|
* @brief Equality operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return Boolean
|
|
*/
|
|
bool operator==(const MapKey &rhs) const;
|
|
|
|
/**
|
|
* @brief Inequality operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return Boolean
|
|
*/
|
|
bool operator!=(const MapKey &rhs) const;
|
|
|
|
/**
|
|
* @brief Less than operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return Boolean
|
|
*/
|
|
bool operator<(const MapKey &rhs) const;
|
|
|
|
/**
|
|
* @brief Convert map key to a string representation.
|
|
*
|
|
* @return A string representation of this object.
|
|
*/
|
|
ACTIV_ONE_API String ToString() const;
|
|
|
|
private:
|
|
//! Reset.
|
|
void Reset();
|
|
|
|
//! Indicates whether use dynamically allocated memory.
|
|
static bool IsDynamic(const uint8_t length);
|
|
|
|
//! Static storage struct.
|
|
struct StaticStorage
|
|
{
|
|
static const size_t MAX_LENGTH = 14; ///< The buffer length.
|
|
uint8_t m_length; ///< The length.
|
|
uint8_t m_isExternal; ///< Is the buffer owned externally.
|
|
uint8_t m_buffer[MAX_LENGTH]; ///< The buffer.
|
|
};
|
|
|
|
//! Dynamic storage struct.
|
|
struct DynamicStorage
|
|
{
|
|
uint8_t m_length; ///< The length.
|
|
uint8_t m_isExternal; ///< Is the buffer owned externally.
|
|
const void * m_pBuffer; ///< The dynamic storage.
|
|
};
|
|
|
|
//! Storage union.
|
|
union Storage
|
|
{
|
|
StaticStorage m_staticStorage; ///< The static storage.
|
|
DynamicStorage m_dynamicStorage; ///< The dynamic storage.
|
|
};
|
|
|
|
Storage m_storage; ///< The storage.
|
|
|
|
friend class MapKeyDetail;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MapKey::MapKey()
|
|
{
|
|
m_storage.m_staticStorage.m_length = 0;
|
|
m_storage.m_staticStorage.m_isExternal = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MapKey::MapKey(const void *pBuffer, const uint8_t length)
|
|
{
|
|
m_storage.m_staticStorage.m_isExternal = 0;
|
|
m_storage.m_staticStorage.m_length = length;
|
|
|
|
if (!IsDynamic(length))
|
|
{
|
|
std::memcpy(m_storage.m_staticStorage.m_buffer, pBuffer, length);
|
|
}
|
|
else
|
|
{
|
|
void *pInternalBuffer = new uint8_t[length]();
|
|
m_storage.m_dynamicStorage.m_pBuffer = pInternalBuffer;
|
|
|
|
std::memcpy(pInternalBuffer, pBuffer, length);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MapKey::MapKey(const MapKey &rhs)
|
|
{
|
|
const uint8_t length = rhs.m_storage.m_staticStorage.m_length;
|
|
|
|
m_storage.m_staticStorage.m_isExternal = 0;
|
|
m_storage.m_staticStorage.m_length = length;
|
|
|
|
if (!IsDynamic(length))
|
|
{
|
|
std::memcpy(m_storage.m_staticStorage.m_buffer,
|
|
((rhs.m_storage.m_staticStorage.m_isExternal) ?
|
|
rhs.m_storage.m_dynamicStorage.m_pBuffer :
|
|
rhs.m_storage.m_staticStorage.m_buffer),
|
|
length);
|
|
}
|
|
else
|
|
{
|
|
void *pBuffer = new uint8_t[length]();
|
|
m_storage.m_dynamicStorage.m_pBuffer = pBuffer;
|
|
|
|
std::memcpy(pBuffer, rhs.m_storage.m_dynamicStorage.m_pBuffer, length);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MapKey::~MapKey()
|
|
{
|
|
Reset();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MapKey &MapKey::operator=(const MapKey &rhs)
|
|
{
|
|
Reset();
|
|
|
|
const uint8_t length = rhs.m_storage.m_staticStorage.m_length;
|
|
|
|
m_storage.m_staticStorage.m_isExternal = 0;
|
|
m_storage.m_staticStorage.m_length = length;
|
|
|
|
if (!IsDynamic(length))
|
|
{
|
|
std::memcpy(m_storage.m_staticStorage.m_buffer,
|
|
((rhs.m_storage.m_staticStorage.m_isExternal) ?
|
|
rhs.m_storage.m_dynamicStorage.m_pBuffer :
|
|
rhs.m_storage.m_staticStorage.m_buffer),
|
|
length);
|
|
}
|
|
else
|
|
{
|
|
void *pBuffer = new uint8_t[length]();
|
|
m_storage.m_dynamicStorage.m_pBuffer = pBuffer;
|
|
|
|
std::memcpy(pBuffer, rhs.m_storage.m_dynamicStorage.m_pBuffer, length);
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const void *MapKey::GetBuffer() const
|
|
{
|
|
if (m_storage.m_dynamicStorage.m_isExternal || IsDynamic(m_storage.m_staticStorage.m_length))
|
|
return m_storage.m_dynamicStorage.m_pBuffer;
|
|
|
|
return m_storage.m_staticStorage.m_buffer;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline uint8_t MapKey::GetLength() const
|
|
{
|
|
return m_storage.m_staticStorage.m_length;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MapKey::IsInitialized() const
|
|
{
|
|
return (m_storage.m_staticStorage.m_length != 0);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MapKey::operator==(const MapKey &rhs) const
|
|
{
|
|
return (GetLength() == rhs.GetLength()) && (0 == ::memcmp(GetBuffer(), rhs.GetBuffer(), GetLength()));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MapKey::operator!=(const MapKey &rhs) const
|
|
{
|
|
return !(*this == rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MapKey::operator<(const MapKey &rhs) const
|
|
{
|
|
if (ACTIV_LIKELY(GetLength() == rhs.GetLength()))
|
|
return (::memcmp(GetBuffer(), rhs.GetBuffer(), GetLength()) < 0);
|
|
|
|
return (GetLength() < rhs.GetLength());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void MapKey::Reset()
|
|
{
|
|
if (!m_storage.m_staticStorage.m_isExternal && IsDynamic(m_storage.m_staticStorage.m_length))
|
|
delete[] const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(m_storage.m_dynamicStorage.m_pBuffer));
|
|
|
|
m_storage.m_staticStorage.m_length = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MapKey::IsDynamic(const uint8_t length)
|
|
{
|
|
return (length > MapKey::StaticStorage::MAX_LENGTH);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief hash_value
|
|
*
|
|
* @param mapKey
|
|
*
|
|
* @return The hashed value of @a mapKey
|
|
*/
|
|
inline std::size_t hash_value(const MapKey &mapKey)
|
|
{
|
|
const uint8_t *pBuffer = static_cast<const uint8_t *>(mapKey.GetBuffer());
|
|
|
|
return Boost::hash_range(pBuffer, (pBuffer + mapKey.GetLength()));
|
|
}
|
|
|
|
} // namespace OneApi
|
|
|
|
} // namespace Activ
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ACTIV_CXXYZ_HASH_NAMESPACE
|
|
{
|
|
|
|
template<>
|
|
struct hash<Activ::OneApi::MapKey>
|
|
{
|
|
inline size_t operator()(const Activ::OneApi::MapKey &mapKey) const
|
|
{
|
|
return hash_value(mapKey);
|
|
}
|
|
};
|
|
|
|
} // namespace ACTIV_CXXYZ_HASH_NAMESPACE
|
|
|
|
#endif // (!defined ACTIV_ONE_API_MAP_KEY_H)
|