Program Listing for File InstanceHandle.h

Return to documentation for file (/tmp/ws/src/fastrtps/include/fastdds/rtps/common/InstanceHandle.h)

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _FASTDDS_RTPS_INSTANCEHANDLE_H_
#define _FASTDDS_RTPS_INSTANCEHANDLE_H_

#include <array>

#include <fastrtps/fastrtps_dll.h>
#include <fastdds/rtps/common/Types.h>
#include <fastdds/rtps/common/Guid.h>

namespace eprosima {
namespace fastrtps {
namespace rtps {

using KeyHash_t = std::array<octet, 16>;

struct RTPS_DllAPI InstanceHandleValue_t
{
    template<typename T>
    octet& operator [] (
            T i) noexcept
    {
        has_been_set_ = true;
        return value_[i];
    }

    template<typename T>
    octet operator [] (
            T i) const noexcept
    {
        return value_[i];
    }

    operator octet* () noexcept
    {
        has_been_set_ = true;
        return value_.data();
    }

    operator const octet* () const noexcept
    {
        return value_.data();
    }

    bool has_been_set() const noexcept
    {
        return has_been_set_;
    }

    void clear() noexcept
    {
        value_.fill(0);
        has_been_set_ = false;
    }

    bool operator == (
            const InstanceHandleValue_t& other) const noexcept
    {
        return (has_been_set_ == other.has_been_set_) && (value_ == other.value_);
    }

    bool operator < (
            const InstanceHandleValue_t& other) const noexcept
    {
        if (has_been_set_)
        {
            return other.has_been_set_ && value_ < other.value_;
        }

        return other.has_been_set_;
    }

private:

    KeyHash_t value_ {};
    bool has_been_set_ = false;
};

struct RTPS_DllAPI InstanceHandle_t
{
    InstanceHandleValue_t value;

    InstanceHandle_t() noexcept = default;

    InstanceHandle_t(
            const InstanceHandle_t& ihandle) noexcept = default;

    InstanceHandle_t(
            const GUID_t& guid) noexcept
    {
        *this = guid;
    }

    InstanceHandle_t& operator =(
            const InstanceHandle_t& ihandle) noexcept = default;

    InstanceHandle_t& operator =(
            const GUID_t& guid) noexcept
    {
        octet* dst = value;
        memcpy(dst, guid.guidPrefix.value, 12);
        memcpy(&dst[12], guid.entityId.value, 4);
        return *this;
    }

    bool isDefined() const noexcept
    {
        return value.has_been_set();
    }

    void clear() noexcept
    {
        value.clear();
    }

    // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
    explicit operator const GUID_t&() const noexcept
    {
        return *reinterpret_cast<const GUID_t*>(this);
    }

};

const InstanceHandle_t c_InstanceHandle_Unknown;

#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC

inline bool operator ==(
        const InstanceHandle_t& ihandle1,
        const InstanceHandle_t& ihandle2) noexcept
{
    return ihandle1.value == ihandle2.value;
}

inline bool operator !=(
        const InstanceHandle_t& ihandle1,
        const InstanceHandle_t& ihandle2) noexcept
{
    return !(ihandle1 == ihandle2);
}

#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC

inline void iHandle2GUID(
        GUID_t& guid,
        const InstanceHandle_t& ihandle) noexcept
{
    const octet* value = ihandle.value;
    memcpy(guid.guidPrefix.value, value, 12);
    memcpy(guid.entityId.value, &value[12], 4);
}

inline GUID_t iHandle2GUID(
        const InstanceHandle_t& ihandle) noexcept
{
    GUID_t guid;
    iHandle2GUID(guid, ihandle);
    return guid;
}

inline bool operator <(
        const InstanceHandle_t& h1,
        const InstanceHandle_t& h2) noexcept
{
    return h1.value < h2.value;
}

#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC

inline std::ostream& operator <<(
        std::ostream& output,
        const InstanceHandle_t& iHandle)
{
    std::stringstream ss;
    ss << std::hex;
    for (uint8_t i = 0; i < 15; ++i)
    {
        ss << (int)iHandle.value[i] << ".";
    }
    ss << (int)iHandle.value[15] << std::dec;
    return output << ss.str();
}

inline std::istream& operator >>(
        std::istream& input,
        InstanceHandle_t& iHandle)
{
    std::istream::sentry s(input);

    if (s)
    {
        char point;
        unsigned short hex;
        std::ios_base::iostate excp_mask = input.exceptions();

        try
        {
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
            input >> std::hex >> hex;

            if (hex > 255)
            {
                input.setstate(std::ios_base::failbit);
            }

            iHandle.value[0] = static_cast<octet>(hex);

            for (int i = 1; i < 16; ++i)
            {
                input >> point >> hex;
                if ( point != '.' || hex > 255 )
                {
                    input.setstate(std::ios_base::failbit);
                }
                iHandle.value[i] = static_cast<octet>(hex);
            }

            input >> std::dec;
        }
        catch (std::ios_base::failure& )
        {
        }

        input.exceptions(excp_mask);
    }

    return input;
}

#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC

} // namespace rtps
} // namespace fastrtps
} // namespace eprosima

#endif /* _FASTDDS_RTPS_INSTANCEHANDLE_H_ */