/** * @file include/ActivOneApi/FieldListValidator.h * * @brief Header file for the FieldListValidator class. */ #if (!defined ACTIV_ONE_API_FIELD_LIST_VALIDATOR_H) #define ACTIV_ONE_API_FIELD_LIST_VALIDATOR_H #include "ActivOneApi/External.h" #include "ActivOneApi/TypeDefinitions.h" #include "ActivOneApi/Constants/DataSourceIds.h" #include "ActivOneApi/Constants/FieldIds.h" #include "ActivOneApi/Detail/FieldListValidatorDetail.h" #include "ActivMiddleware/ActivBase/FieldTypes/All.h" //! Activ namespace. namespace Activ { //! OneApi namespace. namespace OneApi { // Forward declarations. class FieldFilter; class FieldListBuilder; class RefreshMessage; class SnapshotMessage; class StreamMessage; class UpdateMessage; /** * @brief FieldListValidator class. * * Note this class is not thread safe. Do not make concurrent calls to its methods from multiple threads. * * @tparam FIELD_STORAGE the underlying storage for fields. Currently available: * OneApi::FlvTypes::SplitVectorFieldStorage - optimized for access speed at the expense of memory. * OneApi::FlvTypes::HashFieldStorage - hash map storage for fields. Slower but more memory efficient. */ template class FieldListValidatorT ACTIV_FINAL { public: //! The storage type for this object. typedef FIELD_STORAGE FieldStorage; //! The Field iterator type. typedef FlvDetail::ConstIterator ConstIterator; //! The Field definition. typedef FlvDetail::Field Field; //! Constructor. ACTIV_ONE_API FieldListValidatorT(); /** * @brief Copy constructor. * * @param rhs a FieldListValidator to copy fields from. */ ACTIV_ONE_API explicit FieldListValidatorT(const FieldListValidatorT &rhs); /** * @brief Copy constructor. * * @param rhs a FieldListValidator to copy fields from. */ ACTIV_ONE_API explicit FieldListValidatorT(const FieldListValidatorT &rhs); //! Destructor. ACTIV_ONE_API ~FieldListValidatorT(); //! Get DataSourceId. DataSourceId GetDataSourceId() const; /** * @brief Assignment operator. Performs a deep copy of rhs. * * @param rhs * * @return *this */ ACTIV_ONE_API FieldListValidatorT &operator=(const FieldListValidatorT &rhs); /** * @brief Assignment operator. Performs a deep copy of rhs. * * @tparam RHS_FIELD_STORAGE the FieldStorage used for rhs. * * @param rhs * * @return *this */ template ACTIV_ONE_API FieldListValidatorT &operator=(const FieldListValidatorT &rhs); /** * @brief Reset. * * Resets the list of fields present in message. */ ACTIV_ONE_API void Reset(); /** * @brief Clear. * * Resets the list of fields present in message and frees storage for all cached fields. */ ACTIV_ONE_API void Clear(); /** * @brief Is a field present? * * @param fieldId * * @retval true if the field is present. */ bool IsFieldPresent(const FieldId fieldId) const; /** * @brief Is a field defined? * * @param fieldId * * @retval true if the field is present and defined. */ bool IsFieldDefined(const FieldId fieldId) const; /** * @brief Is empty? * * @retval true if empty. */ bool IsEmpty() const; /** * @brief Get the number of fields. * * @return The number of fields present. */ size_t GetNumberOfFields() const; /** * @brief Get a field. * * @param fieldId the field id. * @param pField a pointer to the field. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_NOT_FOUND */ StatusCode GetField(const FieldId fieldId, const Field* &pField) const; /** * @brief Get ACTIV field type. * * @deprecated This method will be removed in future release. * * @param fieldId the field id. * @param pFieldType reference to a pointer to a concrete ACTIV field type object. * * @retval STATUS_CODE_SUCCESS field is defined and pFieldType is set * @retval STATUS_CODE_UNDEFINED field is undefined * @retval STATUS_CODE_NOT_FOUND field not present * @retval STATUS_CODE_INVALID_FIELD_TYPE template type T does not match the type of the field */ template StatusCode GetActivFieldType(const FieldId fieldId, const T* &pFieldType) const; /** * @brief Get ACTIV field type. * * @deprecated This method will be removed in future release. * * @param fieldId the field id. * * @return Const pointer to a concrete ACTIV field type object. * * @throw StatusCodeException, where m_errorCode is: * STATUS_CODE_UNDEFINED field is undefined * STATUS_CODE_NOT_FOUND field not present in record * STATUS_CODE_INVALID_FIELD_TYPE template type @a T does not match the type of the field */ template const T *GetActivFieldType(const FieldId fieldId) const; /** * @brief Get field value base on ACTIV field. * * @tparam T concrete ACTIV field type. * * @param fieldId id of the field to get. * @param pFieldValue reference to a pointer to a concrete ACTIV field type object. * * @retval STATUS_CODE_SUCCESS field is defined and pFieldValue is set * @retval STATUS_CODE_NOT_FOUND field not present in record * @retval STATUS_CODE_UNDEFINED_FIELD field is undefined * @retval STATUS_CODE_INVALID_FIELD template type @a T does not match the type of the field */ template typename Boost::enable_if, StatusCode>::type GetValue(const FieldId fieldId, const T* &pFieldValue) const; /** * @brief Get field value base on ACTIV field. * * @tparam T the ACTIV field type. * * @param fieldId id of the field to get. * @param fieldValue reference to basic type. * * @retval STATUS_CODE_SUCCESS field is defined and fieldValue is set * @retval STATUS_CODE_NOT_FOUND field not present in record * @retval STATUS_CODE_UNDEFINED_FIELD field is undefined * @retval STATUS_CODE_INVALID_FIELD template type @a T does not match the type of the field * @retval STATUS_CODE_OUT_OF_RANGE template type @a T too short to store the value */ template typename Boost::enable_if_c::value, StatusCode>::type GetValue(const FieldId fieldId, T &fieldValue) const; /** * @brief Get a const reference to ACTIV type field value from the map. * * @tparam T concrete ACTIV field type. * * @param fieldId id of the field to get. * * @return field value of the defined field. * * @throw Exception */ template typename Boost::enable_if, const T&>::type GetValue(const FieldId fieldId) const; /** * @brief Get a field value as basic type from the map. * * @tparam T the basic type (uint*_t, int*_t, bool, double, String, ...). * * @param fieldId id of the field to get. * * @return field value of the defined field. * * @throw Exception */ template typename Boost::enable_if_c::value, T>::type GetValue(const FieldId fieldId) const; /** * @brief Get an iterator to the first field in the message. * * @return an iterator object. */ ConstIterator begin() const; /** * @brief Get an iterator that points to beyond the last field in the message. * * @return an iterator object. */ ConstIterator end() const; /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a snapshotMessage. * * @param snapshotMessage reference to a SnapshotMessage object. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const SnapshotMessage &snapshotMessage); /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a refreshMessage. * * @param refreshMessage reference to a RefreshMessage object. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const RefreshMessage &refreshMessage); /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a refreshMessage if they exist in * @a fieldFilter. If @a fieldFilter does not have any fields, then all new fields are added. Use the * @see ApplyUpdateToExistingFieldsOnly method for any updates to preserve the filter. * * @param refreshMessage reference to a RefreshMessage object. * @param fieldFilter the field filter. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const RefreshMessage &refreshMessage, const FieldFilter &fieldFilter); /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a updateMessage. * * @param updateMessage reference to an UpdateMessage object. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const UpdateMessage &updateMessage); /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a updateMessage if they exist in * @a fieldFilter. If @a fieldFilter does not have any fields, then all new fields are added. Use the * @see ApplyUpdateToExistingFieldsOnly method for any updates to preserve the filter. * * @param updateMessage reference to a UpdateMessage object. * @param fieldFilter the field filter. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const UpdateMessage &updateMessage, const FieldFilter &fieldFilter); /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a streamMessage. * * @param streamMessage reference to a StreamMessage object. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const StreamMessage &streamMessage); /** * @brief Initialize. * * This call clears any fields currently in this object. New fields are then added from @a streamMessage. * * @param dataSourceId the DataSourceId of the field data in @a fieldListBuilder. * @param fieldListBuilder reference to a FieldListBuilder object. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval ... */ ACTIV_ONE_API StatusCode Initialize(const DataSourceId dataSourceId, const FieldListBuilder &fieldListBuilder); /** * @brief Initialize this with the fields from @a fieldListValidator * * This call clears any fields currently in this object. New fields are then added from @a fieldListValidator. * * @param fieldListValidator reference to an FieldListValidator object. * * @retval STATUS_CODE_SUCCESS * @retval ... */ template ACTIV_ONE_API StatusCode Initialize(const FieldListValidatorT &fieldListValidator); /** * @brief Apply fields to this. * * This call updates any existing fields add news fields from @ updateMessage. Pre-existing fields are not cleared. * Note fields in @a updateMessage that have the rule RULE_DOES_NOT_UPDATE_LAST_VALUE will be ignored. * * @param updateMessage the updateMessage to apply. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval STATUS_CODE_HEAP_EMPTY * @retval ... */ ACTIV_ONE_API StatusCode ApplyUpdate(const UpdateMessage &updateMessage); /** * @brief Apply only the fields from an UpdateMessage to this that already exist in this. * * This call updates any existing fields from @ updateMessage. Pre-existing fields are not cleared. * Note fields in @a updateMessage that have the rule RULE_DOES_NOT_UPDATE_LAST_VALUE will not be applied to this. * * @param updateMessage the updateMessage to apply. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval STATUS_CODE_HEAP_EMPTY * @retval ... */ ACTIV_ONE_API StatusCode ApplyUpdateToExistingFieldsOnly(const UpdateMessage &updateMessage); /** * @brief Apply all fields in another FieldListValidator to this. * * This call updates any existing fields add news fields from @ fieldListValidator. Pre-existing fields are not cleared. * Note fields in @a fieldListValidator that have the rule RULE_DOES_NOT_UPDATE_LAST_VALUE will not be applied to this. * * @param fieldListValidator the fields to apply. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval STATUS_CODE_HEAP_EMPTY * @retval ... */ template ACTIV_ONE_API StatusCode ApplyUpdate(const FieldListValidatorT &fieldListValidator); /** * @brief Apply only the fields from another FieldListValidator to this that already exist in this. * * This call updates any existing fields from @ fieldListValidator. Pre-existing fields are not cleared. * Note fields in @a fieldListValidator that have the rule RULE_DOES_NOT_UPDATE_LAST_VALUE will not be applied to this. * * @param fieldListValidator the fields to apply. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_FORMAT * @retval STATUS_CODE_HEAP_EMPTY * @retval ... */ template ACTIV_ONE_API StatusCode ApplyUpdateToExistingFieldsOnly(const FieldListValidatorT &fieldListValidator); /** * @brief Set a field to a defined value. * * If the field already exists then its value will be updated. * * If the field does not exist it will be added. * * @param fieldId id of the field to set. * @param fieldValue the field value to set. * @param shouldUpdate does this field update the last cached value. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_NOT_INITIALIZED * @retval ... */ ACTIV_ONE_API StatusCode SetField(const FieldId fieldId, const IFieldType &fieldValue, const bool shouldUpdate = true); /** * @brief Set a field to an undefined value. * * If the field already exists then its value will be updated. * * If the field does not exist it will be added. * * @param fieldId id of the field to set. * @param shouldUpdate does this field update the last cached value. */ ACTIV_ONE_API void SetUndefinedField(const FieldId fieldId, const bool shouldUpdate = true); /** * @brief Return a string representation of the field contents. * * @return String representation of fields. Empty string if not initialized. */ ACTIV_ONE_API String ToString() const; /** * @brief Get the approximate memory usage of this object. * * Note this can be a slow operation. * * @return Approximate memory usage in bytes. */ ACTIV_ONE_API size_t GetMemoryUsage() const; //! Sort the field list to provide consistent iteration ordering. ACTIV_ONE_API void Sort(); private: //! Get field storage. FIELD_STORAGE &GetFieldStorage(); const FIELD_STORAGE &GetFieldStorage() const; typedef FlvDetail::PresentFieldList PresentFieldList; MessageValidator m_messageValidator; ///< To validate serialized data. DataSourceId m_dataSourceId; ///< Data source for the current message. PresentFieldList m_presentFieldList; ///< Vector of fields in message. size_t m_numberOfFields; FIELD_STORAGE m_fieldStorage; template friend class FieldListValidatorT; friend class FieldListValidatorImpl; friend class FieldListValidatorImplTemplateUpdate; }; // --------------------------------------------------------------------------------------------------------------------------------- template inline DataSourceId FieldListValidatorT::GetDataSourceId() const { return m_dataSourceId; } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool FieldListValidatorT::IsFieldPresent(const FieldId fieldId) const { return GetFieldStorage().IsFieldPresent(fieldId); } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool FieldListValidatorT::IsFieldDefined(const FieldId fieldId) const { return GetFieldStorage().IsFieldDefined(fieldId); } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool FieldListValidatorT::IsEmpty() const { return (0 == m_numberOfFields); } // --------------------------------------------------------------------------------------------------------------------------------- template size_t FieldListValidatorT::GetNumberOfFields() const { return m_numberOfFields; } // --------------------------------------------------------------------------------------------------------------------------------- template inline StatusCode FieldListValidatorT::GetField(const FieldId fieldId, const Field* &pField) const { if (!IsFieldPresent(fieldId)) return STATUS_CODE_NOT_FOUND; const StatusCode statusCode = GetFieldStorage().GetField(fieldId, pField); assert(!pField->IsDefined() || ((m_dataSourceId != DATA_SOURCE_PLATFORM) && (m_dataSourceId != DATA_SOURCE_LOCAL)) || (GetPlatformFieldType(fieldId) == FIELD_TYPE_UNKNOWN) || (GetPlatformFieldType(fieldId) == pField->GetType())); return statusCode; } // --------------------------------------------------------------------------------------------------------------------------------- template template inline StatusCode FieldListValidatorT::GetActivFieldType(const FieldId fieldId, const T* &pFieldType) const { assert(((m_dataSourceId != DATA_SOURCE_PLATFORM) && (m_dataSourceId != DATA_SOURCE_LOCAL)) || (GetPlatformFieldType(fieldId) == FIELD_TYPE_UNKNOWN) || (GetPlatformFieldType(fieldId) == T::FIELD_TYPE)); const Field *pField; ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, GetField(fieldId, pField)); return pField->GetActivFieldType(pFieldType); } // --------------------------------------------------------------------------------------------------------------------------------- template template inline const T *FieldListValidatorT::GetActivFieldType(const FieldId fieldId) const { const T *pFieldType; ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, GetActivFieldType(fieldId, pFieldType)); return pFieldType; } // --------------------------------------------------------------------------------------------------------------------------------- template template inline typename Boost::enable_if, StatusCode>::type FieldListValidatorT::GetValue(const FieldId fieldId, const T* &pFieldValue) const { assert(((m_dataSourceId != DATA_SOURCE_PLATFORM) && (m_dataSourceId != DATA_SOURCE_LOCAL)) || (GetPlatformFieldType(fieldId) == FIELD_TYPE_UNKNOWN) || (GetPlatformFieldType(fieldId) == T::FIELD_TYPE)); const Field *pField; ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, GetField(fieldId, pField)); return pField->GetValue(pFieldValue); } // --------------------------------------------------------------------------------------------------------------------------------- template template inline typename Boost::enable_if_c::value, StatusCode>::type FieldListValidatorT::GetValue(const FieldId fieldId, T &fieldValue) const { const Field *pField; ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, GetField(fieldId, pField)); return pField->GetValue(fieldValue); } // --------------------------------------------------------------------------------------------------------------------------------- template template inline typename Boost::enable_if, const T&>::type FieldListValidatorT::GetValue(const FieldId fieldId) const { const T *pFieldValue; ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, GetValue(fieldId, pFieldValue)); return *pFieldValue; } // --------------------------------------------------------------------------------------------------------------------------------- template template inline typename Boost::enable_if_c::value, T>::type FieldListValidatorT::GetValue(const FieldId fieldId) const { T fieldValue; ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, GetValue(fieldId, fieldValue)); return fieldValue; } // --------------------------------------------------------------------------------------------------------------------------------- template inline typename FieldListValidatorT::ConstIterator FieldListValidatorT::begin() const { return ConstIterator(m_presentFieldList.begin()); } // --------------------------------------------------------------------------------------------------------------------------------- template inline typename FieldListValidatorT::ConstIterator FieldListValidatorT::end() const { return ConstIterator(m_presentFieldList.end()); } // --------------------------------------------------------------------------------------------------------------------------------- template inline FIELD_STORAGE &FieldListValidatorT::GetFieldStorage() { return m_fieldStorage; } // --------------------------------------------------------------------------------------------------------------------------------- template inline const FIELD_STORAGE &FieldListValidatorT::GetFieldStorage() const { return m_fieldStorage; } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- // For BOOST_FOREACH support, mainly. template inline FlvDetail::ConstIterator range_begin(const FieldListValidatorT &fieldListValidator) { return fieldListValidator.begin(); } // For BOOST_FOREACH support, mainly. template inline FlvDetail::ConstIterator range_end(const FieldListValidatorT &fieldListValidator) { return fieldListValidator.end(); } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- // Typedefs for FieldListValidator types. //! FlatVectorFieldListValidator is optimized for speed efficiency at the expense of increase memory usage. typedef FieldListValidatorT FlatVectorFieldListValidator; //! HashFieldListValidator is optimized for memory efficiency at the expense of access speed. typedef FieldListValidatorT HashFieldListValidator; //! The default FieldListValidator definition. typedef FlatVectorFieldListValidator FieldListValidator; } // namespace OneApi } // namespace Activ // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- namespace ACTIV_BOOST_NAMESPACE { // For BOOST_FOREACH support, mainly. template struct range_const_iterator > { typedef typename Activ::OneApi::FlvDetail::ConstIterator type; }; template struct range_mutable_iterator > { // There is no mutable iterator; use const. typedef typename Activ::OneApi::FlvDetail::ConstIterator type; }; namespace iterators { template<> struct iterator_reference { typedef const Activ::OneApi::FlvDetail::Field &type; }; } // namespace iterators } // namespace ACTIV_BOOST_NAMESPACE #endif // (!defined ACTIV_ONE_API_FIELD_LIST_VALIDATOR_H)