<commit_before>/** @file
    @brief Header

    @date 2014

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

// Copyright 2014 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)

#ifndef INCLUDED_DeviceToken_h_GUID_428B015C_19A2_46B0_CFE6_CC100763D387
#define INCLUDED_DeviceToken_h_GUID_428B015C_19A2_46B0_CFE6_CC100763D387

// Internal Includes
#include <osvr/Connection/Export.h>
#include <osvr/Util/UniquePtr.h>
#include <osvr/Connection/ConnectionPtr.h>
#include <osvr/Connection/ConnectionDevicePtr.h>
#include <osvr/Connection/DeviceInitObject.h>
#include <osvr/Util/DeviceCallbackTypesC.h>
#include <osvr/Util/TimeValue.h>

// Library/third-party includes
#include <boost/noncopyable.hpp>

// Standard includes
#include <string>
#include <functional>

namespace osvr {
namespace connection {
    class MessageType;
    class AsyncDeviceToken;
    class SyncDeviceToken;
    class DeviceToken;
    typedef unique_ptr<DeviceToken> DeviceTokenPtr;

    typedef std::function<OSVR_ReturnCode()> AsyncDeviceWaitCallback;
    typedef std::function<OSVR_ReturnCode()> SyncDeviceUpdateCallback;

    /// @brief Class that blocks until the device is safe to send using request,
    /// then holds that permission until destruction
    class SendGuard : boost::noncopyable {
      public:
        SendGuard(DeviceToken &device);
        ~SendGuard();
        bool request();
        class Implementation : boost::noncopyable {
          public:
            static unique_ptr<Implementation> createDummyImplementation();
            virtual bool request() = 0;
            virtual ~Implementation();
        };

      private:
        unique_ptr<Implementation> m_impl;
    };

    /// @brief A DeviceToken connects the generic device interaction code in
    /// PluginKit's C API with the workings of an actual ConnectionDevice.
    class DeviceToken : boost::noncopyable {
      public:
        /// @name Factory functions
        /// @{
        /// @brief Creates a device token (and underlying ConnectionDevice) that
        /// has a wait callback that can block, that is called repeatedly in a
        /// thread of its own (managed by OSVR)
        OSVR_CONNECTION_EXPORT static DeviceTokenPtr
        createAsyncDevice(DeviceInitObject &init);
        /// @brief Creates a device token (and underlying ConnectionDevice) that
        /// has an update method that runs in the server mainloop.
        OSVR_CONNECTION_EXPORT static DeviceTokenPtr
        createSyncDevice(DeviceInitObject &init);
        /// @brief Creates a device token (and underlying ConnectionDevice)
        /// without a traditional, built-in update method - typically for
        /// server-internal usage.
        OSVR_CONNECTION_EXPORT static DeviceTokenPtr
        createVirtualDevice(std::string const &name, ConnectionPtr const &conn);
        /// @}

        /// @brief Destructor
        virtual ~DeviceToken();

        /// @brief Accessor for name property
        OSVR_CONNECTION_EXPORT std::string const &getName() const;

        /// @brief Sets the wait callback if this is an async device token.
        /// @throws std::logic_error if it isn't.
        OSVR_CONNECTION_EXPORT void
        setAsyncWaitCallback(AsyncDeviceWaitCallback const &cb);

        /// @brief Sets the update callback if this is a sync device token.
        /// @throws std::logic_error if it isn't.
        OSVR_CONNECTION_EXPORT void
        setSyncUpdateCallback(SyncDeviceUpdateCallback const &cb);

        /// @brief Send data.
        ///
        /// The timestamp for the data is assumed to be at the time this call is
        /// placed.
        ///
        /// This may block until the next connectionInteract call before
        /// forwarding on to ConnectionDevice::sendData, depending on the type
        /// of device token.
        OSVR_CONNECTION_EXPORT void
        sendData(MessageType *type, const char *bytestream, size_t len);

        /// @brief Send data.
        ///
        /// This may block until the next connectionInteract call before
        /// forwarding on to ConnectionDevice::sendData, depending on the type
        /// of device token.
        OSVR_CONNECTION_EXPORT void
        sendData(util::time::TimeValue const &timestamp, MessageType *type,
                 const char *bytestream, size_t len);

        /// @brief Waits until we have access to the connection, then calls your
        /// callback.
        ///
        /// This is effectively how to do multiple sendData at once.
        ///
        /// This may block until the next connectionInteract call before
        /// forwarding on to ConnectionDevice::sendData, depending on the type
        /// of device token.
        ///
        /// @returns true if your function was actually called.
        OSVR_CONNECTION_EXPORT bool
        callWhenSafeToSend(std::function<void()> callback);

        unique_ptr<SendGuard::Implementation> getSendGuardImplementation();

        /// @brief Interact with connection. Only legal to end up in
        /// ConnectionDevice::sendData from within here somehow.
        void connectionInteract();

        /// @brief Stop any threads spawned and owned by this DeviceToken
        void stopThreads();

      protected:
        DeviceToken(std::string const &name);
        ConnectionPtr m_getConnection();
        ConnectionDevicePtr m_getConnectionDevice();
        virtual void m_sendData(util::time::TimeValue const &timestamp,
                                MessageType *type, const char *bytestream,
                                size_t len) = 0;
        virtual virtual bool
        m_callWhenSafeToSend(std::function<void()> &callback) = 0;
        virtual unique_ptr<SendGuard::Implementation> m_getSendGuard() = 0;
        virtual void m_connectionInteract() = 0;
        virtual void m_stopThreads();

        virtual AsyncDeviceToken *asAsync();
        virtual SyncDeviceToken *asSync();

      private:
        void m_sharedInit(ConnectionPtr const &conn);
        std::string const m_name;
        ConnectionPtr m_conn;
        ConnectionDevicePtr m_dev;
    };

} // namespace connection
} // namespace osvr

#endif // INCLUDED_DeviceToken_h_GUID_428B015C_19A2_46B0_CFE6_CC100763D387
<commit_msg>Remove duplicated virtual<commit_after>/** @file
    @brief Header

    @date 2014

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

// Copyright 2014 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)

#ifndef INCLUDED_DeviceToken_h_GUID_428B015C_19A2_46B0_CFE6_CC100763D387
#define INCLUDED_DeviceToken_h_GUID_428B015C_19A2_46B0_CFE6_CC100763D387

// Internal Includes
#include <osvr/Connection/Export.h>
#include <osvr/Util/UniquePtr.h>
#include <osvr/Connection/ConnectionPtr.h>
#include <osvr/Connection/ConnectionDevicePtr.h>
#include <osvr/Connection/DeviceInitObject.h>
#include <osvr/Util/DeviceCallbackTypesC.h>
#include <osvr/Util/TimeValue.h>

// Library/third-party includes
#include <boost/noncopyable.hpp>

// Standard includes
#include <string>
#include <functional>

namespace osvr {
namespace connection {
    class MessageType;
    class AsyncDeviceToken;
    class SyncDeviceToken;
    class DeviceToken;
    typedef unique_ptr<DeviceToken> DeviceTokenPtr;

    typedef std::function<OSVR_ReturnCode()> AsyncDeviceWaitCallback;
    typedef std::function<OSVR_ReturnCode()> SyncDeviceUpdateCallback;

    /// @brief Class that blocks until the device is safe to send using request,
    /// then holds that permission until destruction
    class SendGuard : boost::noncopyable {
      public:
        SendGuard(DeviceToken &device);
        ~SendGuard();
        bool request();
        class Implementation : boost::noncopyable {
          public:
            static unique_ptr<Implementation> createDummyImplementation();
            virtual bool request() = 0;
            virtual ~Implementation();
        };

      private:
        unique_ptr<Implementation> m_impl;
    };

    /// @brief A DeviceToken connects the generic device interaction code in
    /// PluginKit's C API with the workings of an actual ConnectionDevice.
    class DeviceToken : boost::noncopyable {
      public:
        /// @name Factory functions
        /// @{
        /// @brief Creates a device token (and underlying ConnectionDevice) that
        /// has a wait callback that can block, that is called repeatedly in a
        /// thread of its own (managed by OSVR)
        OSVR_CONNECTION_EXPORT static DeviceTokenPtr
        createAsyncDevice(DeviceInitObject &init);
        /// @brief Creates a device token (and underlying ConnectionDevice) that
        /// has an update method that runs in the server mainloop.
        OSVR_CONNECTION_EXPORT static DeviceTokenPtr
        createSyncDevice(DeviceInitObject &init);
        /// @brief Creates a device token (and underlying ConnectionDevice)
        /// without a traditional, built-in update method - typically for
        /// server-internal usage.
        OSVR_CONNECTION_EXPORT static DeviceTokenPtr
        createVirtualDevice(std::string const &name, ConnectionPtr const &conn);
        /// @}

        /// @brief Destructor
        virtual ~DeviceToken();

        /// @brief Accessor for name property
        OSVR_CONNECTION_EXPORT std::string const &getName() const;

        /// @brief Sets the wait callback if this is an async device token.
        /// @throws std::logic_error if it isn't.
        OSVR_CONNECTION_EXPORT void
        setAsyncWaitCallback(AsyncDeviceWaitCallback const &cb);

        /// @brief Sets the update callback if this is a sync device token.
        /// @throws std::logic_error if it isn't.
        OSVR_CONNECTION_EXPORT void
        setSyncUpdateCallback(SyncDeviceUpdateCallback const &cb);

        /// @brief Send data.
        ///
        /// The timestamp for the data is assumed to be at the time this call is
        /// placed.
        ///
        /// This may block until the next connectionInteract call before
        /// forwarding on to ConnectionDevice::sendData, depending on the type
        /// of device token.
        OSVR_CONNECTION_EXPORT void
        sendData(MessageType *type, const char *bytestream, size_t len);

        /// @brief Send data.
        ///
        /// This may block until the next connectionInteract call before
        /// forwarding on to ConnectionDevice::sendData, depending on the type
        /// of device token.
        OSVR_CONNECTION_EXPORT void
        sendData(util::time::TimeValue const &timestamp, MessageType *type,
                 const char *bytestream, size_t len);

        /// @brief Waits until we have access to the connection, then calls your
        /// callback.
        ///
        /// This is effectively how to do multiple sendData at once.
        ///
        /// This may block until the next connectionInteract call before
        /// forwarding on to ConnectionDevice::sendData, depending on the type
        /// of device token.
        ///
        /// @returns true if your function was actually called.
        OSVR_CONNECTION_EXPORT bool
        callWhenSafeToSend(std::function<void()> callback);

        unique_ptr<SendGuard::Implementation> getSendGuardImplementation();

        /// @brief Interact with connection. Only legal to end up in
        /// ConnectionDevice::sendData from within here somehow.
        void connectionInteract();

        /// @brief Stop any threads spawned and owned by this DeviceToken
        void stopThreads();

      protected:
        DeviceToken(std::string const &name);
        ConnectionPtr m_getConnection();
        ConnectionDevicePtr m_getConnectionDevice();
        virtual void m_sendData(util::time::TimeValue const &timestamp,
                                MessageType *type, const char *bytestream,
                                size_t len) = 0;
        virtual bool m_callWhenSafeToSend(std::function<void()> &callback) = 0;
        virtual unique_ptr<SendGuard::Implementation> m_getSendGuard() = 0;
        virtual void m_connectionInteract() = 0;
        virtual void m_stopThreads();

        virtual AsyncDeviceToken *asAsync();
        virtual SyncDeviceToken *asSync();

      private:
        void m_sharedInit(ConnectionPtr const &conn);
        std::string const m_name;
        ConnectionPtr m_conn;
        ConnectionDevicePtr m_dev;
    };

} // namespace connection
} // namespace osvr

#endif // INCLUDED_DeviceToken_h_GUID_428B015C_19A2_46B0_CFE6_CC100763D387

{'fraction_non_alphanumeric': 0.08639822447685479, 'fraction_numerical': 0.012840837032339885, 'mean_word_length': 2.568156108597285, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2626758', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3255, 'n_words': 1051}