Introduce DispatcherIO 76/32176/5
authorAleksander Zdyb <a.zdyb@samsung.com>
Tue, 9 Dec 2014 12:42:33 +0000 (13:42 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 29 Dec 2014 09:10:08 +0000 (10:10 +0100)
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 [new file with mode: 0644]
src/cyad/CMakeLists.txt
src/cyad/DispatcherIO.cpp [new file with mode: 0644]
src/cyad/DispatcherIO.h [new file with mode: 0644]
test/cyad/FakeDispatcherIO.h [new file with mode: 0644]

diff --git a/src/cyad/BaseDispatcherIO.h b/src/cyad/BaseDispatcherIO.h
new file mode 100644 (file)
index 0000000..3cd8af5
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Aggregates I/O methods (base)
+ */
+
+#ifndef SRC_CYAD_BASEDISPATCHERIO_H_
+#define SRC_CYAD_BASEDISPATCHERIO_H_
+
+#include <istream>
+#include <memory>
+#include <ostream>
+#include <string>
+
+namespace Cynara {
+
+class BaseDispatcherIO {
+public:
+    typedef std::string Filename;
+    typedef std::shared_ptr<std::istream> 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_ */
index d221c75..d5f220c 100644 (file)
@@ -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 (file)
index 0000000..80837e8
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Aggregates I/O methods
+ */
+
+#include <fstream>
+
+#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 (file)
index 0000000..b29dfe1
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Aggregates I/O methods
+ */
+
+#ifndef SRC_CYAD_DISPATCHERIO_H_
+#define SRC_CYAD_DISPATCHERIO_H_
+
+#include <iostream>
+
+#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 (file)
index 0000000..eb6299b
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Aggregates I/O methods (stub)
+ */
+
+#ifndef TEST_CYAD_FAKEDISPATCHERIO_H_
+#define TEST_CYAD_FAKEDISPATCHERIO_H_
+
+#include <memory>
+#include <sstream>
+
+#include <cyad/BaseDispatcherIO.h>
+
+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<std::stringstream> m_inputFile;
+};
+
+#endif /* TEST_CYAD_FAKEDISPATCHERIO_H_ */