/* Copyright 2014-2017 The MathWorks, Inc. */ #ifndef CHAR_ARRAY_HPP_ #define CHAR_ARRAY_HPP_ #include "matlab_data_array_defs.hpp" #include "TypedArray.hpp" #include "TypedArrayRef.hpp" #include "detail/StringHelpers.hpp" #include "detail/publish_util.hpp" #include "detail/FunctionType.hpp" #include #include #include namespace matlab { namespace data { namespace impl { class ArrayImpl; } namespace detail { class Access; } /** * CharArray class provides an API for accessing char16_t data. * It inherits most of its functionality from TypedArray class * and adds an API to get the contents of a Char array as a * string of char16_t. */ class CharArray : public TypedArray { public: static const ArrayType type = ArrayType::CHAR; /** * move assignment operator * * @param rhs - rvalue to be moved * @return CharArray& the updated instance * @throw none */ CharArray& operator=(CharArray&& rhs) MW_NOEXCEPT { TypedArray::operator=(std::move(rhs)); return *this; } /** * move constructor * * @param rhs - rvalue to be moved * @return CharArray the new instance * @throw none */ CharArray(CharArray&& rhs) MW_NOEXCEPT : TypedArray(std::move(rhs)) {} /** * assignment operator - creates a shared data copy * * @param rhs - rvalue to be copied * @return CharArray& the updated instance * @throw none */ CharArray& operator=(CharArray const& rhs) MW_NOEXCEPT { TypedArray::operator=(rhs); return *this; } /** * copy constructor - creates a shared data copy * * @param rhs - CharArray to be copied * @return CharArray the new instance * @throw none */ CharArray(const CharArray &rhs) MW_NOEXCEPT : TypedArray(rhs) {} /** * Construct a CharArray from an Array * * @param rhs - rvalue to be moved * @return CharArray the new instance * @throw InvalidArrayTypeException if type of rhs type is not CHAR */ CharArray(Array&& rhs) : TypedArray(std::move(rhs)) {} /** * move assignment operator from an Array * * @param rhs - rvalue to be moved * @return CharArray& the updated instance * @throw InvalidArrayTypeException if type of rhs type is not CHAR */ CharArray& operator=(Array&& rhs) { TypedArray::operator=(std::move(rhs)); return *this; } /** * copy constructor - creates a shared data copy from an Array * * @param rhs - CharArray to be copied * @return CharArray the new instance * @throw InvalidArrayTypeException if type of rhs type is not CHAR */ CharArray(const Array& rhs) : TypedArray(rhs) {} /** * assignment operator - creates a shared data copy fron an Array * * @param rhs - rvalue to be copied * @return CharArray& the updated instance * @throw InvalidArrayTypeException if type of rhs type is not CHAR */ CharArray& operator=(const Array& rhs) { TypedArray::operator=(rhs); return *this; } /** * Return contents of a CHAR array as a utf16 string * * @return std::basic_string string * @throws none */ String toUTF16() const MW_NOEXCEPT { const char16_t* str = nullptr; size_t strLen = 0; typedef void(*CharArrayGetStringFcnPtr)(impl::ArrayImpl*, const char16_t**, size_t*); static const CharArrayGetStringFcnPtr fcn = detail::resolveFunction (detail::FunctionType::CHAR_ARRAY_GET_STRING); fcn(detail::Access::getImpl(*this), &str, &strLen); return String(str, strLen); } /** * Return contents of a CHAR array as an ascii string * * @return std::string string * @throws NonAsciiCharInRequestedAsciiOutputException - if data contains non-ascii characters */ std::string toAscii() const { const char16_t* strVal = nullptr; size_t strLen = 0; typedef int(*CharArrayGetAsciiFcnPtr)(impl::ArrayImpl*, const char16_t**, size_t*); static const CharArrayGetAsciiFcnPtr fcn = detail::resolveFunction (detail::FunctionType::CHAR_ARRAY_GET_ASCII); detail::throwIfError(fcn(detail::Access::getImpl(*this), &strVal, &strLen)); return detail::toAsciiHelper(strVal, strLen); } private: friend class detail::Access; CharArray(impl::ArrayImpl* impl) MW_NOEXCEPT : TypedArray(impl) {} CharArray() = delete; }; using CharArrayRef = TypedArrayRef; } } #endif