.
This commit is contained in:
@@ -78,7 +78,7 @@ find_package(catkin REQUIRED COMPONENTS
|
||||
## CATKIN_DEPENDS: catkin_packages dependent projects also need
|
||||
## DEPENDS: system dependencies of this project that dependent projects also need
|
||||
catkin_package(
|
||||
INCLUDE_DIRS include
|
||||
INCLUDE_DIRS include/${PROJECT_NAME}/
|
||||
# LIBRARIES hos_cosgi_common
|
||||
CATKIN_DEPENDS hos_celix
|
||||
# DEPENDS system_lib
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* cosgi_common.h
|
||||
*
|
||||
* Created on: 19.04.2015
|
||||
* Author: matzman
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_HOS_COSGI_COMMON_COSGI_CORE_H_
|
||||
#define INCLUDE_HOS_COSGI_COMMON_COSGI_CORE_H_
|
||||
|
||||
|
||||
namespace hos {
|
||||
namespace cosgi{
|
||||
|
||||
|
||||
class COSGI_Object_Interface {
|
||||
public:
|
||||
virtual ~COSGI_Object_Interface() {}
|
||||
};
|
||||
|
||||
|
||||
class COSGI_Service_Interface : public COSGI_Object_Interface {
|
||||
public:
|
||||
virtual const char* getServiceName() = 0;
|
||||
virtual void getServiceVersion(int& major, int& minor) = 0;
|
||||
};
|
||||
|
||||
|
||||
} // namespace cosgi
|
||||
} // namespace hos
|
||||
|
||||
#endif /* INCLUDE_HOS_COSGI_COMMON_COSGI_CORE_H_ */
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* impl.h
|
||||
*
|
||||
* Created on: 09.05.2015
|
||||
* Author: matzman
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_HOS_COSGI_COMMON_HOS_COSGI_SERVICE_IMPLEMENTATION_H_
|
||||
#define INCLUDE_HOS_COSGI_COMMON_HOS_COSGI_SERVICE_IMPLEMENTATION_H_
|
||||
|
||||
#include "interface.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace hos {
|
||||
namespace cosgi{
|
||||
namespace service {
|
||||
namespace implementation {
|
||||
|
||||
|
||||
template<class _Service>
|
||||
struct ServiceInfo {
|
||||
static const char* name;
|
||||
static const int major;
|
||||
static const int minor;
|
||||
};
|
||||
|
||||
|
||||
template<class _Service>
|
||||
class Service : public virtual interface::Service {
|
||||
public:
|
||||
virtual const char* getServiceName() { return ServiceInfo<_Service>::name; };
|
||||
virtual void getServiceVersion(int& major, int& minor) override {
|
||||
major = ServiceInfo<_Service>::major;
|
||||
minor = ServiceInfo<_Service>::minor;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
template <class... _Args> class Observable {};
|
||||
|
||||
|
||||
template<class _Event>
|
||||
class Observable<_Event> : public virtual interface::Observable<_Event> {
|
||||
protected:
|
||||
std::mutex _observersMutex;
|
||||
typedef interface::Observer<_Event> observer_type;
|
||||
typedef std::unordered_set<observer_type*> observers_type;
|
||||
observers_type _observers;
|
||||
public:
|
||||
virtual ~Observable() {}
|
||||
virtual void addObserver(observer_type* o) override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
if (o != nullptr) {
|
||||
auto elem = _observers.find(o);
|
||||
if (elem == _observers.end()) {
|
||||
_observers.insert(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void deleteObserver(observer_type* o) override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
if (o != nullptr) {
|
||||
auto elem = _observers.find(o);
|
||||
if (elem != _observers.end()) {
|
||||
_observers.erase(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void deleteObservers() override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
_observers.clear();
|
||||
};
|
||||
virtual size_t countObservers() override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
return _observers.size();
|
||||
}
|
||||
virtual void notifyObservers(const _Event* e) override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
for (auto l : _observers) {
|
||||
l->notificationReceived(this, e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
template<class _Key, class _Event>
|
||||
class Observable<_Key, _Event> : public virtual interface::Observable<_Key, _Event> {
|
||||
protected:
|
||||
std::mutex _observersMutex;
|
||||
typedef interface::Observer<_Key, _Event> observer_type;
|
||||
typedef std::unordered_multimap<_Key, observer_type*> observers_type;
|
||||
observers_type _observers;
|
||||
public:
|
||||
virtual ~Observable() {}
|
||||
virtual void addObserver(_Key k, observer_type* o) override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
if (o != nullptr) {
|
||||
bool _found = false;
|
||||
auto range = _observers.equal_range(k);
|
||||
for (auto i = range.first; i != range.second; i++) {
|
||||
if (i.second == o) {
|
||||
_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!_found) {
|
||||
_observers.insert({k, o});
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void deleteObserver(_Key k, observer_type* o) override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
if (o != nullptr) {
|
||||
bool _found = false;
|
||||
auto range = _observers.equal_range(k);
|
||||
for (auto i = range.first; i != range.second; i++) {
|
||||
if (i.second == o) {
|
||||
_observers.erase(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void deleteObservers() override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
_observers.clear();
|
||||
}
|
||||
virtual size_t countObservers() override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
return _observers.size();
|
||||
}
|
||||
void notifyObservers(_Key k, const _Event* e) override {
|
||||
std::lock_guard<std::mutex> lock(_observersMutex);
|
||||
auto ol = _observers.equal_range(k);
|
||||
std::for_each(ol.first, ol.second, [&](const typename observer_type::value_type& o) {
|
||||
o.second->notificationReceived(this, k, e);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace implementation
|
||||
} // namespace service
|
||||
} // namespace cosgi
|
||||
} // namespace hos
|
||||
|
||||
|
||||
#endif /* INCLUDE_HOS_COSGI_COMMON_HOS_COSGI_SERVICE_IMPLEMENTATION_H_ */
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* cosgi_common.h
|
||||
*
|
||||
* Created on: 19.04.2015
|
||||
* Author: matzman
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_HOS_COSGI_COMMON_COSGI_CORE_H_
|
||||
#define INCLUDE_HOS_COSGI_COMMON_COSGI_CORE_H_
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace hos {
|
||||
namespace cosgi{
|
||||
namespace service {
|
||||
namespace interface {
|
||||
|
||||
|
||||
class Object {
|
||||
public:
|
||||
virtual ~Object() {}
|
||||
};
|
||||
|
||||
|
||||
class Service : public virtual Object {
|
||||
public:
|
||||
virtual const char* getServiceName() = 0;
|
||||
virtual void getServiceVersion(int& major, int& minor) = 0;
|
||||
};
|
||||
|
||||
|
||||
template<class...> class Observer {};
|
||||
template<class...> class Observable {};
|
||||
template<class... _Args> class ObservableObject : public virtual Object, public virtual Observable<_Args...> {};
|
||||
template<class... _Args> class ObservableService : public virtual Service, public virtual Observable<_Args...> {};
|
||||
|
||||
|
||||
template<class _Event>
|
||||
class Observer<_Event> : public virtual Object {
|
||||
public:
|
||||
typedef Observable<_Event> observable_type;
|
||||
virtual void notificationReceived(observable_type* o, const _Event* e) = 0;
|
||||
};
|
||||
|
||||
|
||||
template<class _Key, class _Event>
|
||||
class Observer<_Key, _Event> : public virtual Object {
|
||||
public:
|
||||
typedef Observable<_Key, _Event> observable_type;
|
||||
virtual void notificationReceived(observable_type* o, const _Key& key, const _Event* e) = 0;
|
||||
};
|
||||
|
||||
|
||||
template<class _Event>
|
||||
class Observable<_Event> : public virtual Object {
|
||||
protected:
|
||||
virtual void notifyObservers(const _Event* e) = 0;
|
||||
public:
|
||||
typedef Observer<_Event> observer_type;
|
||||
virtual void addObserver(observer_type* o) = 0;
|
||||
virtual void deleteObserver(observer_type* o) = 0;
|
||||
virtual void deleteObservers() = 0;
|
||||
virtual size_t countObservers() = 0;
|
||||
};
|
||||
|
||||
|
||||
template<class _Key, class _Event>
|
||||
class Observable<_Key, _Event> : public virtual Object {
|
||||
protected:
|
||||
virtual void notifyObservers(const _Key& key, const _Event* e) = 0;
|
||||
public:
|
||||
typedef Observer<_Key, _Event> observer_type;
|
||||
virtual void addObserver(_Key& key, observer_type* o) = 0;
|
||||
virtual void deleteObserver(_Key& key, observer_type* o) = 0;
|
||||
virtual void deleteObservers() = 0;
|
||||
virtual size_t countObservers() = 0;
|
||||
};
|
||||
|
||||
|
||||
} // namespace interface
|
||||
} // namespace service
|
||||
} // namespace cosgi
|
||||
} // namespace hos
|
||||
|
||||
#endif /* INCLUDE_HOS_COSGI_COMMON_COSGI_CORE_H_ */
|
||||
Reference in New Issue
Block a user