From c8c12d9c5431dc046251155aeebb05efa074d17d Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Wed, 15 Jul 2020 20:40:53 -0700 Subject: [PATCH] add allocArray() --- src/pvxs/sharedArray.h | 3 +++ src/sharedarray.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/pvxs/sharedArray.h b/src/pvxs/sharedArray.h index ee2195a..ea91a2c 100644 --- a/src/pvxs/sharedArray.h +++ b/src/pvxs/sharedArray.h @@ -47,6 +47,9 @@ std::ostream& operator<<(std::ostream& strm, ArrayType code); PVXS_API size_t elementSize(ArrayType type); +//! Return a void array usable for the given storage type +shared_array allocArray(ArrayType type, size_t count); + namespace detail { template struct CaptureCode; diff --git a/src/sharedarray.cpp b/src/sharedarray.cpp index 840b9c3..f7d8cfa 100644 --- a/src/sharedarray.cpp +++ b/src/sharedarray.cpp @@ -62,6 +62,31 @@ size_t elementSize(ArrayType type) throw std::logic_error("Invalid ArrayType"); } +shared_array allocArray(ArrayType type, size_t count) +{ + switch(type) { +#define CASE(CODE, TYPE) case ArrayType::CODE: return shared_array(count).castTo() + CASE(Bool, bool); + CASE(UInt8, uint8_t); + CASE(UInt16, uint16_t); + CASE(UInt32, uint32_t); + CASE(UInt64, uint64_t); + CASE(Int8, int8_t); + CASE(Int16, int16_t); + CASE(Int32, int32_t); + CASE(Int64, int64_t); + CASE(Float32, float); + CASE(Float64, double); + CASE(String, std::string); + CASE(Value, Value); +#undef CASE + case ArrayType::Null: + break; + } + + throw std::logic_error("Invalid ArrayType"); +} + namespace detail { namespace {