From 94f5031bbcba5ca3391ced88b13cd7630cb75009 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 6 Nov 2018 17:31:17 +0900 Subject: [PATCH] [tflchef] Extract "to_number" helper (#2131) This commit extracts "to_number" helper (currently in Data/Constant.h) to a seperate module. Signed-off-by: Jonghyun Park --- contrib/tflchef/core/src/Data/Constant.h | 8 +++----- contrib/tflchef/core/src/LexicalCast.cpp | 19 +++++++++++++++++++ contrib/tflchef/core/src/LexicalCast.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 contrib/tflchef/core/src/LexicalCast.cpp create mode 100644 contrib/tflchef/core/src/LexicalCast.h diff --git a/contrib/tflchef/core/src/Data/Constant.h b/contrib/tflchef/core/src/Data/Constant.h index da87c81..ebe1f3d 100644 --- a/contrib/tflchef/core/src/Data/Constant.h +++ b/contrib/tflchef/core/src/Data/Constant.h @@ -18,6 +18,7 @@ #define __CONSTANT_FILLER_H__ #include "DataChef.h" +#include "LexicalCast.h" template class ConstantDataChef final : public DataChef { @@ -49,15 +50,12 @@ private: T _value; }; -template T to_number(const std::string &str); - -template <> float to_number(const std::string &str) { return std::stof(str); } - template struct ConstantDataChefFactory : public DataChefFactory { std::unique_ptr create(const Arguments &args) const { - return std::unique_ptr{new ConstantDataChef(to_number(args.value(0)))}; + auto const value = to_number(args.value(0)); + return std::unique_ptr{new ConstantDataChef{value}}; } }; diff --git a/contrib/tflchef/core/src/LexicalCast.cpp b/contrib/tflchef/core/src/LexicalCast.cpp new file mode 100644 index 0000000..4709088 --- /dev/null +++ b/contrib/tflchef/core/src/LexicalCast.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "LexicalCast.h" + +template <> float to_number(const std::string &s) { return std::stof(s); } diff --git a/contrib/tflchef/core/src/LexicalCast.h b/contrib/tflchef/core/src/LexicalCast.h new file mode 100644 index 0000000..4aeccb4 --- /dev/null +++ b/contrib/tflchef/core/src/LexicalCast.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @brief This file provides string <-> number cast helpers + */ +#ifndef __LEXICAL_CAST_H__ +#define __LEXICAL_CAST_H__ + +#include + +/** + * @brief Return a numeric value that corresponds to a given string + * + * @note This function will throw an exception on casting failure + */ +template Number to_number(const std::string &s); + +#endif // __LEXICAL_CAST_H__ -- 2.7.4