Skip to content

Commit 34a29fd

Browse files
author
Vitalii Arteev
committed
Add auxiliary API for Proxy
1 parent 63e4de6 commit 34a29fd

File tree

6 files changed

+174
-1
lines changed

6 files changed

+174
-1
lines changed

src/ipc/ipc-common/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ facelift_add_library(FaceliftIPCCommonLib
4848
SerializeParameterFunction.h
4949
StaticArrayReference.h
5050
ipc-common.h
51+
observer.h
5152
LINK_LIBRARIES FaceliftModelLib FaceliftCommonLib
5253
MONOLITHIC_SUPPORTED
5354
)

src/ipc/ipc-common/IPCProxyBase.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "ipc-common.h"
3434
#include "IPCProxyBaseBase.h"
3535
#include "IPCProxyBinderBase.h"
36-
36+
#include "observer.h"
3737

3838
#if defined(FaceliftIPCCommonLib_LIBRARY)
3939
# define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT
@@ -49,10 +49,12 @@ class IPCProxyBase : public AdapterType, protected IPCProxyBaseBase
4949

5050
public:
5151
using InterfaceType = AdapterType;
52+
IsReadyObserver m_readyObserver{};
5253

5354
public:
5455
IPCProxyBase(QObject *parent) : AdapterType(parent)
5556
{
57+
QObject::connect(this, &InterfaceBase::readyChanged, &m_readyObserver, &IsReadyObserver::onReadyChanged);
5658
}
5759

5860
template<typename BinderType>

src/ipc/ipc-common/observer.h

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**********************************************************************
2+
**
3+
** Copyright (C) 2020 Luxoft Sweden AB
4+
**
5+
** This file is part of the FaceLift project
6+
**
7+
** Permission is hereby granted, freIPCServiceAdapterBasee of charge, to any person
8+
** obtaining a copy of this software and associated documentation files
9+
** (the "Software"), to deal in the Software without restriction,
10+
** including without limitation the rights to use, copy, modify, merge,
11+
** publish, distribute, sublicense, and/or sell copies of the Software,
12+
** and to permit persons to whom the Software is furnished to do so,
13+
** subject to the following conditions:
14+
**
15+
** The above copyright notice and this permission notice shall be
16+
** included in all copies or substantial portions of the Software.
17+
**
18+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20+
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22+
** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23+
** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24+
** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
** SOFTWARE.
26+
**
27+
** SPDX-License-Identifier: MIT
28+
**
29+
**********************************************************************/
30+
#pragma once
31+
32+
#include <memory>
33+
#include <vector>
34+
#include <QObject>
35+
36+
namespace facelift {
37+
38+
class IObserver : public QObject
39+
{
40+
Q_OBJECT
41+
public:
42+
virtual void onReadyChanged(std::shared_ptr<QMetaObject::Connection> connection) = 0;
43+
};
44+
45+
class IsReadyObserver: public QObject
46+
{
47+
Q_OBJECT
48+
std::vector<IObserver *> m_observers;
49+
std::shared_ptr<QMetaObject::Connection> connection;
50+
public:
51+
IsReadyObserver() : connection{std::make_shared<QMetaObject::Connection>()}
52+
{
53+
*connection = QObject::connect(this, &IsReadyObserver::readyChanged, this, [ this ](){
54+
for (auto observer : m_observers) {
55+
observer->onReadyChanged( connection );
56+
}
57+
});
58+
}
59+
// Set observers
60+
void setObservers(const std::vector<IObserver *> &observers){
61+
m_observers = observers;
62+
}
63+
64+
// Get observers
65+
const std::vector<IObserver *>& getObservers() const {
66+
return m_observers;
67+
}
68+
69+
Q_SIGNAL void readyChanged();
70+
void onReadyChanged(){
71+
emit readyChanged();
72+
};
73+
};
74+
75+
// Single-time observer which will unregister itself when done
76+
template<typename Function>
77+
class SingleTimeObserver : public IObserver
78+
{
79+
Function m_func;
80+
public:
81+
explicit SingleTimeObserver(Function func) : m_func{func} {}
82+
~SingleTimeObserver() = default;
83+
84+
void onReadyChanged(std::shared_ptr<QMetaObject::Connection> connection) override {
85+
m_func();
86+
QObject::disconnect(*connection);
87+
}
88+
};
89+
90+
// Standard observer which will work for each signal
91+
template<typename Function>
92+
class StandartObserver : public IObserver
93+
{
94+
Function m_func;
95+
public:
96+
explicit StandartObserver(Function func) : m_func{func} {}
97+
~StandartObserver() = default;
98+
99+
void onReadyChanged(std::shared_ptr<QMetaObject::Connection> ) override {
100+
m_func();
101+
}
102+
};
103+
}

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ add_test(NAME benchmarking-inprocess.cpp COMMAND test-driver.sh benchmarking/tst
7676

7777
if(NOT ${FACELIFT_DISABLE_GTEST})
7878
add_subdirectory(unittest)
79+
add_subdirectory(unittest_observer)
7980
endif()
8081

8182
add_subdirectory(objectregistry)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
find_package(GTest)
3+
if(${GTEST_FOUND})
4+
find_library(GMOCK_LIBRARY gmock REQUIRED HINTS "${CMAKE_SYSTEM_PREFIX_PATH}")
5+
if(NOT ${GMOCK_LIBRARY} STREQUAL "GMOCK_LIBRARY-NOTFOUND")
6+
add_library(GTest::GMock UNKNOWN IMPORTED)
7+
set_target_properties(GTest::GMock PROPERTIES IMPORTED_LOCATION ${GMOCK_LIBRARY})
8+
else()
9+
message(WARNING "Google test/mock not found.")
10+
endif()
11+
12+
find_package(Threads REQUIRED)
13+
set(FACELIFT_GTEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} GTest::GMock Threads::Threads)
14+
include_directories(${GTEST_INCLUDE_DIRS})
15+
16+
facelift_add_test(UnitTestsObserver
17+
SOURCES FaceliftObserverTest.cpp
18+
LINK_LIBRARIES ${FACELIFT_GTEST_LIBRARIES} FaceliftIPCCommonLib)
19+
20+
else()
21+
message(WARNING "Required package google test not found!")
22+
endif()
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <gtest/gtest.h>
2+
#include "IPCProxyBase.h"
3+
#include "InterfaceBase.h"
4+
#include <QObject>
5+
6+
using namespace facelift;
7+
8+
class Counter
9+
{
10+
public:
11+
Counter() { m_value = 0; }
12+
int value() const { return m_value; }
13+
14+
void setValue1(){
15+
qDebug() << "Counter setValue1() ";
16+
}
17+
void setValue2(){
18+
qDebug() << "Counter setValue2() ";
19+
}
20+
private:
21+
int m_value;
22+
};
23+
24+
class IPCProxyBaseTest : public testing::Test
25+
{
26+
public:
27+
Counter c1;
28+
Counter c2;
29+
StandartObserver < std::function<void()> > * obs1 = new StandartObserver < std::function<void()> >(std::bind(&Counter::setValue1, &c1) );
30+
SingleTimeObserver< std::function<void()> > * obs2 = new SingleTimeObserver< std::function<void()> >(std::bind(&Counter::setValue2, &c2) );
31+
32+
IPCProxyBase<InterfaceBase> proxyBase{nullptr};
33+
};
34+
35+
TEST_F(IPCProxyBaseTest, setObservers)
36+
{
37+
const auto expected = std::vector<IObserver*>{obs1, obs2,};
38+
proxyBase.m_readyObserver.setObservers(expected);
39+
const auto actual = proxyBase.m_readyObserver.getObservers();
40+
EXPECT_EQ(expected.size(), actual.size());
41+
EXPECT_EQ(expected[0], actual[0]);
42+
EXPECT_EQ(expected[1], actual[1]);
43+
}

0 commit comments

Comments
 (0)