aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDavid Beck <david.beck@arm.com>2018-10-04 15:43:17 +0100
committerMatthew Bentham <matthew.bentham@arm.com>2018-10-10 16:16:58 +0100
commit5eec11db435a94ba5046ba74edc5c9c412a64e9d (patch)
tree6a3d9b9126ae449fc5e83838b2eb1765a67d6aad /include
parentd806792f7fbdae2cfa8dcb1eb59b9400b84741da (diff)
downloadarmnn-5eec11db435a94ba5046ba74edc5c9c412a64e9d.tar.gz
IVGCVSW-1964 : replace optional biases with home-grown Optional
!android-nn-driver:151788 Change-Id: Ibdc41d09b8df05e7a0360dcb8a060860dfb1bd99
Diffstat (limited to 'include')
-rw-r--r--include/armnn/Exceptions.hpp5
-rw-r--r--include/armnn/ILayerSupport.hpp8
-rw-r--r--include/armnn/LayerSupport.hpp7
-rw-r--r--include/armnn/Optional.hpp123
4 files changed, 135 insertions, 8 deletions
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 <typename ExceptionType>
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 <armnn/DescriptorsFwd.hpp>
-
-#include <boost/optional.hpp>
+#include <armnn/Optional.hpp>
#include <vector>
+#include <cctype>
namespace armnn
{
@@ -61,7 +61,7 @@ public:
const TensorInfo& output,
const Convolution2dDescriptor& descriptor,
const TensorInfo& weights,
- const boost::optional<TensorInfo>& biases,
+ const Optional<TensorInfo>& 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<TensorInfo>& biases,
+ const Optional<TensorInfo>& 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 <armnn/DescriptorsFwd.hpp>
#include <armnn/Types.hpp>
#include <armnn/Tensor.hpp>
-
-#include <boost/optional.hpp>
+#include <armnn/Optional.hpp>
namespace armnn
{
@@ -60,7 +59,7 @@ bool IsConvolution2dSupported(Compute compute,
const TensorInfo& output,
const Convolution2dDescriptor& descriptor,
const TensorInfo& weights,
- const boost::optional<TensorInfo>& biases,
+ const Optional<TensorInfo>& 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<TensorInfo>& biases,
+ const Optional<TensorInfo>& 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 <typename T>
+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<const T*>(m_Storage);
+ return *valuePtr;
+ }
+
+ T& value()
+ {
+ if (!has_value())
+ {
+ throw BadOptionalAccessException("Optional has no value");
+ }
+
+ auto valuePtr = reinterpret_cast<T*>(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;
+};
+
+}