/* * Copyright (C) 2014 Pavel Kirienko */ #pragma once namespace uavcan { /** * Usage: * StaticAssert::check(); */ template struct StaticAssert; template <> struct StaticAssert { static void check() { } }; /** * Usage: * ShowIntegerAsError::foobar(); */ template struct ShowIntegerAsError; /** * Prevents copying when inherited */ class Noncopyable { Noncopyable(const Noncopyable&); Noncopyable& operator=(const Noncopyable&); protected: Noncopyable() { } }; /** * Compile time conditions */ template struct EnableIf { }; template struct EnableIf { typedef T Type; }; template struct EnableIfType { typedef R Type; }; template struct Select; template struct Select { typedef TrueType Result; }; template struct Select { typedef FalseType Result; }; /** * Value types */ template struct BooleanType { }; typedef BooleanType TrueType; typedef BooleanType FalseType; /** * Relations */ template class 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 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 To try_implicit_cast(const From& from, const To& default_) { return TryImplicitCastImpl::impl(from, default_, BooleanType::Result>()); } template To try_implicit_cast(const From& from) { return TryImplicitCastImpl::impl(from, To(), BooleanType::Result>()); } }