From 5eec11db435a94ba5046ba74edc5c9c412a64e9d Mon Sep 17 00:00:00 2001 From: David Beck Date: Thu, 4 Oct 2018 15:43:17 +0100 Subject: IVGCVSW-1964 : replace optional biases with home-grown Optional !android-nn-driver:151788 Change-Id: Ibdc41d09b8df05e7a0360dcb8a060860dfb1bd99 --- include/armnn/Exceptions.hpp | 5 ++ include/armnn/ILayerSupport.hpp | 8 +-- include/armnn/LayerSupport.hpp | 7 +-- include/armnn/Optional.hpp | 123 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 include/armnn/Optional.hpp (limited to 'include') diff --git a/include/armnn/Exceptions.hpp b/include/armnn/Exceptions.hpp index 4f3bea0e30..89b6f2cfbb 100644 --- a/include/armnn/Exceptions.hpp +++ b/include/armnn/Exceptions.hpp @@ -95,6 +95,11 @@ class GraphValidationException : public Exception using Exception::Exception; }; +class BadOptionalAccessException : public Exception +{ + using Exception::Exception; +}; + template void ConditionalThrow(bool condition, const std::string& message) { diff --git a/include/armnn/ILayerSupport.hpp b/include/armnn/ILayerSupport.hpp index 7962393f34..b9b41b7fcf 100644 --- a/include/armnn/ILayerSupport.hpp +++ b/include/armnn/ILayerSupport.hpp @@ -5,9 +5,9 @@ #pragma once #include - -#include +#include #include +#include namespace armnn { @@ -61,7 +61,7 @@ public: const TensorInfo& output, const Convolution2dDescriptor& descriptor, const TensorInfo& weights, - const boost::optional& biases, + const Optional& biases, char* reasonIfUnsupported = nullptr, size_t reasonIfUnsupportedMaxLength = 1024) const; @@ -69,7 +69,7 @@ public: const TensorInfo& output, const DepthwiseConvolution2dDescriptor& descriptor, const TensorInfo& weights, - const boost::optional& biases, + const Optional& biases, char* reasonIfUnsupported = nullptr, size_t reasonIfUnsupportedMaxLength = 1024) const; diff --git a/include/armnn/LayerSupport.hpp b/include/armnn/LayerSupport.hpp index 25e888e71e..31874fe944 100644 --- a/include/armnn/LayerSupport.hpp +++ b/include/armnn/LayerSupport.hpp @@ -7,8 +7,7 @@ #include #include #include - -#include +#include namespace armnn { @@ -60,7 +59,7 @@ bool IsConvolution2dSupported(Compute compute, const TensorInfo& output, const Convolution2dDescriptor& descriptor, const TensorInfo& weights, - const boost::optional& biases, + const Optional& biases, char* reasonIfUnsupported = nullptr, size_t reasonIfUnsupportedMaxLength = 1024); @@ -69,7 +68,7 @@ bool IsDepthwiseConvolutionSupported(Compute compute, const TensorInfo& output, const DepthwiseConvolution2dDescriptor& descriptor, const TensorInfo& weights, - const boost::optional& biases, + const Optional& biases, char* reasonIfUnsupported = nullptr, size_t reasonIfUnsupportedMaxLength = 1024); diff --git a/include/armnn/Optional.hpp b/include/armnn/Optional.hpp new file mode 100644 index 0000000000..6fc207f425 --- /dev/null +++ b/include/armnn/Optional.hpp @@ -0,0 +1,123 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// +#pragma once + +#include "Exceptions.hpp" + +namespace armnn +{ + +// NOTE: the members of the Optional class don't follow the ArmNN +// coding convention because the interface to be close to +// the C++-17 interface so we can easily migrate to std::optional +// later. + +template +class Optional final +{ +public: + Optional(T&& value) + : m_HasValue{true} + { + new (m_Storage) T(value); + } + + Optional(const T& value) + : m_HasValue{true} + { + new (m_Storage) T(value); + } + + Optional(const Optional& other) + : m_HasValue{false} + { + *this = other; + } + + Optional() noexcept + : m_HasValue{false} + { + } + + ~Optional() + { + reset(); + } + + operator bool() const noexcept + { + return has_value(); + } + + Optional& operator=(T&& value) + { + reset(); + new (m_Storage) T(value); + m_HasValue = true; + return *this; + } + + Optional& operator=(const T& value) + { + reset(); + new(m_Storage) T(value); + m_HasValue = true; + return *this; + } + + Optional& operator=(const Optional& other) + { + reset(); + if (other.has_value()) + { + new (m_Storage) T(other.value()); + m_HasValue = true; + } + + return *this; + } + + const T& value() const + { + if (!has_value()) + { + throw BadOptionalAccessException("Optional has no value"); + } + + auto valuePtr = reinterpret_cast(m_Storage); + return *valuePtr; + } + + T& value() + { + if (!has_value()) + { + throw BadOptionalAccessException("Optional has no value"); + } + + auto valuePtr = reinterpret_cast(m_Storage); + return *valuePtr; + } + + bool has_value() const noexcept + { + return m_HasValue; + } + + void reset() + { + if (has_value()) + { + value().T::~T(); + m_HasValue = false; + } + } + +private: + alignas(alignof(T)) unsigned char m_Storage[sizeof(T)]; + bool m_HasValue; +}; + +} -- cgit v1.2.1