/* * Copyright (C) 2014 Pavel Kirienko */ #pragma once #include namespace uavcan { /** * Usage: * StaticAssert::check(); */ template struct UAVCAN_EXPORT StaticAssert; template <> struct UAVCAN_EXPORT StaticAssert { static void check() { } }; /** * Usage: * ShowIntegerAsError::foobar(); */ template struct ShowIntegerAsError; /** * Prevents copying when inherited */ class UAVCAN_EXPORT Noncopyable { Noncopyable(const Noncopyable&); Noncopyable& operator=(const Noncopyable&); protected: Noncopyable() { } }; /** * Compile time conditions */ template struct UAVCAN_EXPORT EnableIf { }; template struct UAVCAN_EXPORT EnableIf { typedef T Type; }; template struct UAVCAN_EXPORT EnableIfType { typedef R Type; }; template struct UAVCAN_EXPORT Select; template struct UAVCAN_EXPORT Select { typedef TrueType Result; }; template struct UAVCAN_EXPORT Select { typedef FalseType Result; }; /** * Value types */ template struct UAVCAN_EXPORT BooleanType { }; typedef BooleanType TrueType; typedef BooleanType FalseType; /** * Relations */ template class UAVCAN_EXPORT IsImplicitlyConvertibleFromTo { template static U returner(); struct True_ { char x[2]; }; struct False_ { }; static True_ test(const T2 &); static False_ test(...); public: enum { Result = sizeof(True_) == sizeof(IsImplicitlyConvertibleFromTo::test(returner())) }; }; template struct UAVCAN_EXPORT TryImplicitCastImpl { static To impl(const From& from, const To&, TrueType) { return To(from); } static To impl(const From&, const To& default_, FalseType) { return default_; } }; template UAVCAN_EXPORT To try_implicit_cast(const From& from, const To& default_) { return TryImplicitCastImpl::impl(from, default_, BooleanType::Result>()); } template UAVCAN_EXPORT To try_implicit_cast(const From& from) { return TryImplicitCastImpl::impl(from, To(), BooleanType::Result>()); } /** * Some arithmetics */ template struct UAVCAN_EXPORT CompileTimeIntSqrt; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<4> { enum { Result = 2 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<9> { enum { Result = 3 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<16> { enum { Result = 4 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<25> { enum { Result = 5 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<36> { enum { Result = 6 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<49> { enum { Result = 7 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<64> { enum { Result = 8 }; }; template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<81> { enum { Result = 9 }; }; }