Skip to content

Commit acd86a1

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

File tree

5 files changed

+486
-10
lines changed

5 files changed

+486
-10
lines changed

src/ipc/ipc-common/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ facelift_add_library(FaceliftIPCCommonLib
2323
IPCServiceAdapterBase.h
2424
NewIPCServiceAdapterBase.h
2525
IPCAttachedPropertyFactory.h
26+
observer.h
2627
HEADERS_NO_MOC
2728
AppendDBUSSignatureFunction.h
2829
ipc-serialization.h

src/ipc/ipc-common/IPCProxyBase.h

-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@
3535
#include "IPCProxyBinderBase.h"
3636

3737

38-
#if defined(FaceliftIPCCommonLib_LIBRARY)
39-
# define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT
40-
#else
41-
# define FaceliftIPCCommonLib_EXPORT Q_DECL_IMPORT
42-
#endif
43-
4438
namespace facelift {
4539

4640
template<typename AdapterType>

src/ipc/ipc-common/observer.h

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
31+
#pragma once
32+
33+
#include <memory>
34+
#include <QVector>
35+
#include <QObject>
36+
37+
namespace facelift {
38+
39+
class IObserver : public QObject
40+
{
41+
Q_OBJECT
42+
public:
43+
virtual void IsReadyObserver(const std::shared_ptr<QMetaObject::Connection> &connection) = 0;
44+
};
45+
46+
class IsReadyObserver: public QObject
47+
{
48+
Q_OBJECT
49+
QVector< QPointer<IObserver> > m_observers{};
50+
bool isReady{};
51+
52+
public:
53+
IsReadyObserver() {}
54+
55+
template<typename T, typename S, typename F>
56+
void watch(T const *object, S signal, F function)
57+
{
58+
QObject::connect(object, std::move(signal), object, [this, function, object](){
59+
std::function<bool()> functor { std::bind(function, object) };
60+
if(functor != nullptr) {
61+
isReady = functor();
62+
}
63+
});
64+
QObject::connect(object, std::move(signal), this, &IsReadyObserver::onReadyChanged );
65+
}
66+
67+
// Set observers
68+
void setObservers(const QVector< QPointer<IObserver> > &observers) {
69+
m_observers.clear();
70+
for (auto observer: observers) {
71+
Q_ASSERT (observer != nullptr);
72+
m_observers.push_back(observer);
73+
auto connection = std::make_shared<QMetaObject::Connection>();
74+
*connection = QObject::connect(this, &IsReadyObserver::readyChanged, observer, [observer, connection](){
75+
observer->IsReadyObserver( connection );
76+
});
77+
}
78+
}
79+
80+
// Get observers
81+
const QVector< QPointer<IObserver> > &getObservers() const {
82+
return m_observers;
83+
}
84+
85+
Q_SIGNAL void readyChanged();
86+
87+
void onReadyChanged() {
88+
if(isReady) {
89+
emit readyChanged();
90+
}
91+
}
92+
};
93+
94+
// Single-time observer which will unregister itself when done
95+
template<typename T>
96+
class SingleTimeObserver : public IObserver
97+
{
98+
T m_function;
99+
100+
public:
101+
explicit SingleTimeObserver(T function) : m_function{function} {}
102+
~SingleTimeObserver() = default;
103+
104+
void IsReadyObserver(const std::shared_ptr<QMetaObject::Connection> &connection) override {
105+
if(m_function != nullptr) {
106+
m_function();
107+
}
108+
QObject::disconnect(*connection);
109+
}
110+
};
111+
112+
// Standard observer which will work for each signal
113+
template<typename T>
114+
class StandartObserver : public IObserver
115+
{
116+
T m_function;
117+
118+
public:
119+
explicit StandartObserver(T function) : m_function{function} {}
120+
~StandartObserver() = default;
121+
122+
void IsReadyObserver(const std::shared_ptr<QMetaObject::Connection> &) override {
123+
if(m_function != nullptr) {
124+
m_function();
125+
}
126+
}
127+
};
128+
129+
}

tests/unittest/CMakeLists.txt

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@ if(${GTEST_FOUND})
66
add_library(GTest::GMock UNKNOWN IMPORTED)
77
set_target_properties(GTest::GMock PROPERTIES IMPORTED_LOCATION ${GMOCK_LIBRARY})
88
else()
9-
message(WARNING "Google test/mock not found.")
9+
message(ERROR "Google test/mock not found.")
1010
endif()
1111

12+
find_package(Qt5 COMPONENTS Test REQUIRED)
13+
if(${QT5TEST_NOTFOUND})
14+
message(ERROR "Required package Qt5Test not found.")
15+
endif()
1216
find_package(Threads REQUIRED)
13-
set(FACELIFT_GTEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} GTest::GMock Threads::Threads)
17+
if(${THREADS_NOTFOUND})
18+
message(ERROR "Required package Threads not found.")
19+
endif()
20+
set(FACELIFT_GTEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} GTest::GMock Qt5::Test Threads::Threads)
1421
include_directories(${GTEST_INCLUDE_DIRS})
1522

1623
facelift_add_test(UnitTests
1724
SOURCES FaceliftUtilsTest.cpp
18-
LINK_LIBRARIES ${FACELIFT_GTEST_LIBRARIES} FaceliftCommonLib)
25+
LINK_LIBRARIES
26+
${FACELIFT_GTEST_LIBRARIES}
27+
FaceliftCommonLib
28+
)
1929

30+
facelift_add_test(UnitTestsObserver
31+
SOURCES FaceliftObserverTest.cpp
32+
LINK_LIBRARIES
33+
${FACELIFT_GTEST_LIBRARIES}
34+
FaceliftIPCCommonLib
35+
)
2036
else()
21-
message(WARNING "Required package google test not found!")
37+
message(ERROR "Required package google test not found!")
2238
endif()
2339

0 commit comments

Comments
 (0)