Program Listing for File Types.h

Return to documentation for file (/tmp/ws/src/sick_safetyscanners_base/include/sick_safetyscanners_base/Types.h)

// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-

// -- BEGIN LICENSE BLOCK ----------------------------------------------

// -- END LICENSE BLOCK ------------------------------------------------

//----------------------------------------------------------------------
//----------------------------------------------------------------------

#ifndef SICK_SAFETYSCANNERS_BASE_TYPES_H
#define SICK_SAFETYSCANNERS_BASE_TYPES_H

#include "sick_safetyscanners_base/datastructure/Data.h"
#include "sick_safetyscanners_base/datastructure/PacketBuffer.h"
#include <boost/asio/ip/address_v4.hpp>
#include <functional>
#include <memory>

namespace sick {
namespace types {

using ScanDataCb = std::function<void(const sick::datastructure::Data&)>;

using PacketHandler = std::function<void(const sick::datastructure::PacketBuffer&)>;

using ip_address_t = boost::asio::ip::address_v4;

using port_t = uint16_t;

using time_duration_t = boost::posix_time::time_duration;

using SensorFeatures = uint16_t;
} // namespace types

// Namespace containing sensor feature flags as constants.
namespace SensorDataFeatures {
constexpr uint16_t ALL                  = 0b11111;
constexpr uint16_t NONE                 = 0;
constexpr uint16_t GENERAL_SYSTEM_STATE = 1 << 0;
constexpr uint16_t DERIVED_SETTINGS     = 1 << 1;
constexpr uint16_t MEASUREMENT_DATA     = 1 << 2;
constexpr uint16_t INTRUSION_DATA       = 1 << 3;
constexpr uint16_t APPLICATION_DATA     = 1 << 4;

constexpr bool isFlagSet(uint16_t bitset, uint16_t flag)
{
  return (bitset & flag) == flag;
};

constexpr uint16_t toFeatureFlags(bool general_system_state,
                                  bool derived_settings,
                                  bool measurement_data,
                                  bool intursion_data,
                                  bool application_data)
{
  // In C++11 constexpr functions are restricted to a function-body that contains a single
  // return-statement only. bool * int gets promoted to either 1 * int or 0 * int, so this is works
  // to avoid if-statements.
  return (general_system_state * sick::SensorDataFeatures::GENERAL_SYSTEM_STATE) +
         (derived_settings * sick::SensorDataFeatures::DERIVED_SETTINGS) +
         (measurement_data * sick::SensorDataFeatures::MEASUREMENT_DATA) +
         (intursion_data * sick::SensorDataFeatures::INTRUSION_DATA) +
         (application_data * sick::SensorDataFeatures::APPLICATION_DATA);
}

} // namespace SensorDataFeatures
} // namespace sick

#endif // SICK_SAFETYSCANNERS_TYPES_H