From 8069773e3b5568c07c4ea8b9e1412fc909e641fc Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Tue, 9 Dec 2014 13:42:33 +0100 Subject: [PATCH] Introduce DispatcherIO BaseDispatcherIO provides interface for wrapping and aggregating I/O streams. The wrapping is needed mainly for testing purposes, but in near future will be used for printing messages, errors, etc. DispatcherIO is a target implementation, while FakeDispatcherIO is a stub for testing. Change-Id: I1ce231c3bbaf5f7f483358478cdf1eb5ff618589 --- src/cyad/BaseDispatcherIO.h | 49 ++++++++++++++++++++++++++++ src/cyad/CMakeLists.txt | 1 + src/cyad/DispatcherIO.cpp | 40 +++++++++++++++++++++++ src/cyad/DispatcherIO.h | 54 +++++++++++++++++++++++++++++++ test/cyad/FakeDispatcherIO.h | 76 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 220 insertions(+) create mode 100644 src/cyad/BaseDispatcherIO.h create mode 100644 src/cyad/DispatcherIO.cpp create mode 100644 src/cyad/DispatcherIO.h create mode 100644 test/cyad/FakeDispatcherIO.h diff --git a/src/cyad/BaseDispatcherIO.h b/src/cyad/BaseDispatcherIO.h new file mode 100644 index 0000000..3cd8af5 --- /dev/null +++ b/src/cyad/BaseDispatcherIO.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/BaseDispatcherIO.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Aggregates I/O methods (base) + */ + +#ifndef SRC_CYAD_BASEDISPATCHERIO_H_ +#define SRC_CYAD_BASEDISPATCHERIO_H_ + +#include +#include +#include +#include + +namespace Cynara { + +class BaseDispatcherIO { +public: + typedef std::string Filename; + typedef std::shared_ptr InputStreamPtr; + + BaseDispatcherIO() = default; + virtual ~BaseDispatcherIO() {} + + virtual InputStreamPtr openFile(const Filename &filename) = 0; + virtual std::ostream &cout(void) = 0; + virtual std::istream &cin(void) = 0; + virtual std::ostream &cerr(void) = 0; +}; + +} /* namespace Cynara */ + +#endif /* SRC_CYAD_BASEDISPATCHERIO_H_ */ diff --git a/src/cyad/CMakeLists.txt b/src/cyad/CMakeLists.txt index d221c75..d5f220c 100644 --- a/src/cyad/CMakeLists.txt +++ b/src/cyad/CMakeLists.txt @@ -21,6 +21,7 @@ SET(CYAD_PATH ${CYNARA_PATH}/cyad) SET(CYAD_SOURCES ${CYAD_PATH}/AdminApiWrapper.cpp ${CYAD_PATH}/CynaraAdminPolicies.cpp + ${CYAD_PATH}/DispatcherIO.cpp ${CYAD_PATH}/main.cpp ) diff --git a/src/cyad/DispatcherIO.cpp b/src/cyad/DispatcherIO.cpp new file mode 100644 index 0000000..80837e8 --- /dev/null +++ b/src/cyad/DispatcherIO.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/DispatcherIO.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Aggregates I/O methods + */ + +#include + +#include "DispatcherIO.h" + +namespace Cynara { + +BaseDispatcherIO::InputStreamPtr DispatcherIO::openFile(const BaseDispatcherIO::Filename &filename) +{ + BaseDispatcherIO::InputStreamPtr streamPtr; + if (filename == "-") { + streamPtr.reset(&std::cin, [] (BaseDispatcherIO::InputStreamPtr::element_type *) {}); + } else { + streamPtr.reset(new std::ifstream(filename)); + } + return streamPtr; +} + +} /* namespace Cynara */ diff --git a/src/cyad/DispatcherIO.h b/src/cyad/DispatcherIO.h new file mode 100644 index 0000000..b29dfe1 --- /dev/null +++ b/src/cyad/DispatcherIO.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/DispatcherIO.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Aggregates I/O methods + */ + +#ifndef SRC_CYAD_DISPATCHERIO_H_ +#define SRC_CYAD_DISPATCHERIO_H_ + +#include + +#include "BaseDispatcherIO.h" + +namespace Cynara { + +class DispatcherIO : public BaseDispatcherIO { +public: + + using BaseDispatcherIO::BaseDispatcherIO; + + BaseDispatcherIO::InputStreamPtr openFile(const BaseDispatcherIO::Filename &filename); + + std::ostream &cout(void) { + return std::cout; + } + + std::istream &cin(void) { + return std::cin; + } + + std::ostream &cerr(void) { + return std::cerr; + } +}; + +} /* namespace Cynara */ + +#endif /* SRC_CYAD_DISPATCHERIO_H_ */ diff --git a/test/cyad/FakeDispatcherIO.h b/test/cyad/FakeDispatcherIO.h new file mode 100644 index 0000000..eb6299b --- /dev/null +++ b/test/cyad/FakeDispatcherIO.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file test/cyad/FakeDispatcherIO.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Aggregates I/O methods (stub) + */ + +#ifndef TEST_CYAD_FAKEDISPATCHERIO_H_ +#define TEST_CYAD_FAKEDISPATCHERIO_H_ + +#include +#include + +#include + +class FakeDispatcherIO : public Cynara::BaseDispatcherIO { +public: + + FakeDispatcherIO() : m_inputFile(new std::stringstream()) {} + ~FakeDispatcherIO() = default; + + BaseDispatcherIO::InputStreamPtr openFile(const BaseDispatcherIO::Filename &) { + return m_inputFile; + } + + std::ostream &cout(void) { + return m_out; + } + + std::istream &cin(void) { + return m_cin; + } + + std::ostream &cerr(void) { + return m_err; + } + + std::ostream &file(void) { + return *m_inputFile; + } + + std::stringstream &cerrRaw(void) { + return m_err; + } + + std::stringstream &coutRaw(void) { + return m_out; + } + + std::stringstream &cinRaw(void) { + return m_cin; + } + +private: + std::stringstream m_out; + std::stringstream m_err; + std::stringstream m_cin; + std::shared_ptr m_inputFile; +}; + +#endif /* TEST_CYAD_FAKEDISPATCHERIO_H_ */ -- 2.7.4