3#ifndef DATASTORM_DATASTORM_H
4#define DATASTORM_DATASTORM_H
7#include "DataStorm/SampleEvent.h"
17# pragma clang diagnostic push
18# pragma clang diagnostic ignored "-Wshadow-field-in-constructor"
19#elif defined(__GNUC__)
20# pragma GCC diagnostic push
21# pragma GCC diagnostic ignored "-Wshadow"
30 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
class Sample
49 [[nodiscard]] const Key&
getKey() const noexcept;
55 [[nodiscard]] const Value&
getValue() const noexcept;
65 [[nodiscard]] std::chrono::time_point<std::chrono::system_clock>
getTimeStamp() const noexcept;
72 [[nodiscard]] const std::
string&
getOrigin() const noexcept;
77 [[nodiscard]] const std::
string&
getSession() const noexcept;
80 Sample(const std::shared_ptr<DataStormI::Sample>&) noexcept;
83 std::shared_ptr<DataStormI::SampleT<Key, Value, UpdateTag>> _impl;
90 inline std::ostream& operator<<(std::ostream& os, const
SampleEventSeq& types)
100 template<
typename K,
typename V,
typename U>
109 template<
typename Key,
typename Value,
typename UpdateTag>
class Reader
133 [[nodiscard]]
bool hasWriters() const noexcept;
162 [[nodiscard]]
bool hasUnread() const noexcept;
177 std::function<
void(std::vector<Key>)> init,
187 std::function<
void(std::vector<std::
string>)> init,
188 std::function<
void(
CallbackReason, std::
string)> update) noexcept;
198 std::function<
void(std::vector<
Sample<Key, Value, UpdateTag>>)> init,
199 std::function<
void(
Sample<Key, Value, UpdateTag>)> queue) noexcept;
203 Reader(const std::shared_ptr<DataStormI::DataReader>& impl) noexcept : _impl(impl) {}
206 std::shared_ptr<DataStormI::DataReader> _impl;
211 template<
typename Key,
typename Value,
typename UpdateTag>
class Writer
235 [[nodiscard]]
bool hasReaders() const noexcept;
261 [[nodiscard]] std::vector<
Sample<Key, Value, UpdateTag>>
getAll();
271 std::function<
void(std::vector<Key>)> init,
281 std::function<
void(std::vector<std::
string>)> init,
282 std::function<
void(
CallbackReason, std::
string)> update) noexcept;
286 Writer(const std::shared_ptr<DataStormI::DataWriter>& impl) noexcept : _impl(impl) {}
289 std::shared_ptr<DataStormI::DataWriter> _impl;
296 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
class Topic
320 Topic(
const Node& node, std::string name)
noexcept;
325 : _name(std::move(topic._name)),
326 _topicFactory(std::move(topic._topicFactory)),
327 _keyFactory(std::move(topic._keyFactory)),
328 _tagFactory(std::move(topic._tagFactory)),
329 _keyFilterFactories(std::move(topic._keyFilterFactories)),
330 _sampleFilterFactories(std::move(topic._sampleFilterFactories)),
331 _reader(std::move(topic._reader)),
332 _writer(std::move(topic._writer)),
333 _updaters(std::move(topic._updaters))
348 [[nodiscard]]
bool hasWriters() const noexcept;
353 void waitForWriters(
unsigned int count = 1) const;
357 void waitForNoWriters() const;
361 void setWriterDefaultConfig(const
WriterConfig& config) noexcept;
365 [[nodiscard]]
bool hasReaders() const noexcept;
370 void waitForReaders(
unsigned int count = 1) const;
374 void waitForNoReaders() const;
378 void setReaderDefaultConfig(const
ReaderConfig& config) noexcept;
387 template<typename UpdateValue>
388 void setUpdater(const UpdateTag& tag, std::function<
void(Value&, UpdateValue)> updater) noexcept;
396 template<typename Criteria>
399 std::function<std::function<
bool(const Key&)>(const Criteria&)> factory) noexcept;
407 template<typename Criteria>
408 void setSampleFilter(
410 std::function<std::function<
bool(const SampleType&)>(const Criteria&)> factory) noexcept;
413 [[nodiscard]] std::shared_ptr<DataStormI::TopicReader> getReader() const;
414 [[nodiscard]] std::shared_ptr<DataStormI::TopicWriter> getWriter() const;
415 [[nodiscard]]
Ice::CommunicatorPtr getCommunicator() const noexcept;
418 template<typename, typename, typename> friend class
MultiKeyWriter;
420 template<typename, typename, typename> friend class
MultiKeyReader;
425 std::shared_ptr<DataStormI::TopicFactory> _topicFactory;
426 std::shared_ptr<DataStormI::KeyFactoryT<Key>> _keyFactory;
427 std::shared_ptr<DataStormI::TagFactoryT<UpdateTag>> _tagFactory;
428 std::shared_ptr<DataStormI::FilterManagerT<DataStormI::KeyT<Key>>> _keyFilterFactories;
429 std::shared_ptr<DataStormI::FilterManagerT<DataStormI::SampleT<Key, Value, UpdateTag>>> _sampleFilterFactories;
431 mutable std::mutex _mutex;
432 mutable std::shared_ptr<DataStormI::TopicReader> _reader;
433 mutable std::shared_ptr<DataStormI::TopicWriter> _writer;
434 mutable std::map<std::shared_ptr<DataStormI::Tag>, DataStormI::
Topic::Updater> _updaters;
444 template<
typename TT>
459 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
472 std::string name = std::string(),
483 template<
typename SampleFilterCriteria>
488 std::string name = std::string(),
509 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
522 const std::vector<Key>& keys,
523 std::string name = std::string(),
535 template<
typename SampleFilterCriteria>
538 const std::vector<Key>& keys,
540 std::string name = std::string(),
559 template<
typename K,
typename V,
typename UT>
563 std::string name = std::string(),
576 template<
typename SFC,
typename K,
typename V,
typename UT>
581 std::string name = std::string(),
594 template<
typename K,
typename V,
typename UT>
598 std::string name = std::string(),
612 template<
typename SFC,
typename K,
typename V,
typename UT>
617 std::string name = std::string(),
629 template<
typename K,
typename V,
typename UT>
632 std::string name = std::string(),
645 template<
typename SFC,
typename K,
typename V,
typename UT>
649 std::string name = std::string(),
662 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
674 template<
typename KeyFilterCriteria>
678 std::string name = std::string(),
690 template<
typename KeyFilterCriteria,
typename SampleFilterCriteria>
695 std::string name = std::string(),
714 template<
typename KFC,
typename K,
typename V,
typename UT>
718 std::string name = std::string(),
731 template<
typename KFC,
typename SFC,
typename K,
typename V,
typename UT>
736 std::string name = std::string(),
744 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
757 std::string name = std::string(),
771 void add(
const Value& value);
775 void update(
const Value& value);
786 template<
typename UpdateValue>
787 [[nodiscard]] std::function<void(
const UpdateValue&)>
partialUpdate(
const UpdateTag& tag);
795 const std::shared_ptr<DataStormI::TagFactoryT<UpdateTag>> _tagFactory;
807 template<
typename Key,
typename Value,
typename UpdateTag = std::
string>
820 const std::vector<Key>& keys,
821 std::string name = std::string(),
836 void add(
const Key& key,
const Value& value);
841 void update(
const Key& key,
const Value& value);
852 template<
typename UpdateValue>
853 [[nodiscard]] std::function<void(
const Key&,
const UpdateValue&)>
partialUpdate(
const UpdateTag& tag);
859 void remove(
const Key& key)
noexcept;
862 const std::shared_ptr<DataStormI::KeyFactoryT<Key>> _keyFactory;
863 const std::shared_ptr<DataStormI::TagFactoryT<UpdateTag>> _tagFactory;
872 template<
typename K,
typename V,
typename UT>
876 std::string name = std::string(),
888 template<
typename K,
typename V,
typename UT>
892 std::string name = std::string(),
903 template<
typename K,
typename V,
typename UT>
906 std::string name = std::string(),
919 template<
typename Key,
typename Value,
typename UpdateTag>
925 template<
typename Key,
typename Value,
typename UpdateTag>
928 return _impl->getKey();
931 template<
typename Key,
typename Value,
typename UpdateTag>
934 return _impl->getValue();
937 template<
typename Key,
typename Value,
typename UpdateTag>
940 return _impl->getTag();
943 template<
typename Key,
typename Value,
typename UpdateTag>
946 return _impl->timestamp;
949 template<
typename Key,
typename Value,
typename UpdateTag>
952 return _impl->origin;
955 template<
typename Key,
typename Value,
typename UpdateTag>
958 return _impl->session;
961 template<
typename Key,
typename Value,
typename UpdateTag>
962 Sample<Key, Value, UpdateTag>::Sample(
const std::shared_ptr<DataStormI::Sample>& impl) noexcept
963 : _impl(std::static_pointer_cast<DataStormI::SampleT<Key, Value, UpdateTag>>(impl))
970 template<
typename Key,
typename Value,
typename UpdateTag>
972 : _impl(std::move(reader._impl))
984 template<
typename Key,
typename Value,
typename UpdateTag>
991 _impl = std::move(reader._impl);
995 template<
typename Key,
typename Value,
typename UpdateTag>
998 return _impl->hasWriters();
1001 template<
typename Key,
typename Value,
typename UpdateTag>
1004 _impl->waitForWriters(
static_cast<int>(count));
1007 template<
typename Key,
typename Value,
typename UpdateTag>
1010 _impl->waitForWriters(-1);
1013 template<
typename Key,
typename Value,
typename UpdateTag>
1016 return _impl->getConnectedElements();
1019 template<
typename Key,
typename Value,
typename UpdateTag>
1022 std::vector<Key> keys;
1023 auto connectedKeys = _impl->getConnectedKeys();
1024 keys.reserve(connectedKeys.size());
1025 for (
const auto& k : connectedKeys)
1027 keys.push_back(std::static_pointer_cast<DataStormI::KeyT<Key>>(k)->get());
1032 template<
typename Key,
typename Value,
typename UpdateTag>
1035 auto unread = _impl->getAllUnread();
1036 std::vector<Sample<Key, Value, UpdateTag>> samples;
1037 samples.reserve(unread.size());
1038 for (
const auto& sample : unread)
1040 samples.push_back(sample);
1045 template<
typename Key,
typename Value,
typename UpdateTag>
1048 _impl->waitForUnread(count);
1051 template<
typename Key,
typename Value,
typename UpdateTag>
1054 return _impl->hasUnread();
1057 template<
typename Key,
typename Value,
typename UpdateTag>
1063 template<
typename Key,
typename Value,
typename UpdateTag>
1065 std::function<
void(std::vector<Key>)> init,
1068 _impl->onConnectedKeys(
1070 [init = std::move(init)](
const std::vector<std::shared_ptr<DataStormI::Key>>& connectedKeys)
1072 std::vector<Key> keys;
1073 keys.reserve(connectedKeys.size());
1074 for(
const auto& k : connectedKeys)
1076 keys.push_back(std::static_pointer_cast<DataStormI::KeyT<Key>>(k)->get());
1078 init(std::move(keys));
1079 } : std::function<void(std::vector<std::shared_ptr<DataStormI::Key>>)>{},
1081 [update = std::move(update)](
CallbackReason action,
const std::shared_ptr<DataStormI::Key>& key)
1083 update(action, std::static_pointer_cast<DataStormI::KeyT<Key>>(key)->get());
1084 } : std::function<void(
CallbackReason, std::shared_ptr<DataStormI::Key>)>{});
1087 template<
typename Key,
typename Value,
typename UpdateTag>
1089 std::function<
void(std::vector<std::string>)> init,
1090 std::function<
void(
CallbackReason, std::string)> update)
noexcept
1092 _impl->onConnectedElements(std::move(init), std::move(update));
1095 template<
typename Key,
typename Value,
typename UpdateTag>
1100 auto communicator = _impl->getCommunicator();
1103 [communicator, init = std::move(init)](
const std::vector<std::shared_ptr<DataStormI::Sample>>& samplesI)
1105 std::vector<Sample<Key, Value, UpdateTag>> samples;
1106 samples.reserve(samplesI.size());
1107 for(
const auto& s : samplesI)
1109 samples.emplace_back(s);
1111 init(std::move(samples));
1112 } : std::function<void(
const std::vector<std::shared_ptr<DataStormI::Sample>>&)>(),
1114 [communicator, update = std::move(update)](
const std::shared_ptr<DataStormI::Sample>& sampleI)
1117 } : std::function<void(
const std::shared_ptr<DataStormI::Sample>&)>{});
1120 template<
typename Key,
typename Value,
typename UpdateTag>
1126 :
Reader<Key, Value, UpdateTag>(
1127 topic.getReader()->create({topic._keyFactory->create(key)}, std::move(name), config))
1131 template<
typename Key,
typename Value,
typename UpdateTag>
1132 template<
typename SampleFilterCriteria>
1139 :
Reader<Key, Value, UpdateTag>(topic.getReader()->create(
1140 {topic._keyFactory->create(key)},
1144 DataStormI::EncoderT<SampleFilterCriteria>::encode(topic.getCommunicator(), sampleFilter.
criteria)))
1148 template<
typename Key,
typename Value,
typename UpdateTag>
1154 template<
typename Key,
typename Value,
typename UpdateTag>
1162 template<
typename Key,
typename Value,
typename UpdateTag>
1165 const std::vector<Key>& keys,
1168 :
Reader<Key, Value, UpdateTag>(
1169 topic.getReader()->create(topic._keyFactory->create(keys), std::move(name), config))
1173 template<
typename Key,
typename Value,
typename UpdateTag>
1174 template<
typename SampleFilterCriteria>
1177 const std::vector<Key>& keys,
1181 :
Reader<Key, Value, UpdateTag>(topic.getReader()->create(
1182 topic._keyFactory->create(keys),
1186 DataStormI::EncoderT<SampleFilterCriteria>::encode(topic.getCommunicator(), sampleFilter.criteria)))
1190 template<
typename Key,
typename Value,
typename UpdateTag>
1196 template<
typename Key,
typename Value,
typename UpdateTag>
1204 template<
typename Key,
typename Value,
typename UpdateTag>
1205 template<
typename KeyFilterCriteria>
1211 :
Reader<Key, Value, UpdateTag>(topic.getReader()->createFiltered(
1212 topic._keyFilterFactories->create(filter.name, filter.criteria),
1218 template<
typename Key,
typename Value,
typename UpdateTag>
1219 template<
typename KeyFilterCriteria,
typename SampleFilterCriteria>
1226 :
Reader<Key, Value, UpdateTag>(topic.getReader()->createFiltered(
1227 topic._keyFilterFactories->create(keyFilter.name, keyFilter.criteria),
1231 DataStormI::EncoderT<SampleFilterCriteria>::encode(topic.getCommunicator(), sampleFilter.criteria)))
1235 template<
typename Key,
typename Value,
typename UpdateTag>
1242 template<
typename Key,
typename Value,
typename UpdateTag>
1253 template<
typename Key,
typename Value,
typename UpdateTag>
1266 template<
typename Key,
typename Value,
typename UpdateTag>
1273 _impl = std::move(writer._impl);
1277 template<
typename Key,
typename Value,
typename UpdateTag>
1283 template<
typename Key,
typename Value,
typename UpdateTag>
1286 return _impl->waitForReaders(
static_cast<int>(count));
1289 template<
typename Key,
typename Value,
typename UpdateTag>
1292 return _impl->waitForReaders(-1);
1295 template<
typename Key,
typename Value,
typename UpdateTag>
1298 return _impl->getConnectedElements();
1301 template<
typename Key,
typename Value,
typename UpdateTag>
1304 std::vector<Key> keys;
1305 auto connectedKeys = _impl->getConnectedKeys();
1306 keys.reserve(connectedKeys.size());
1307 for (
const auto& k : connectedKeys)
1309 keys.push_back(std::static_pointer_cast<DataStormI::KeyT<Key>>(k)->get());
1314 template<
typename Key,
typename Value,
typename UpdateTag>
1317 auto sample = _impl->getLast();
1320 throw std::logic_error(
"no sample");
1325 template<
typename Key,
typename Value,
typename UpdateTag>
1328 auto all = _impl->getAll();
1329 std::vector<Sample<Key, Value, UpdateTag>> samples;
1330 samples.reserve(all.size());
1331 for (
const auto& sample : all)
1333 samples.push_back(sample);
1338 template<
typename Key,
typename Value,
typename UpdateTag>
1340 std::function<
void(std::vector<Key>)> init,
1343 _impl->onConnectedKeys(
1345 [init = std::move(init)](
const std::vector<std::shared_ptr<DataStormI::Key>>& connectedKeys)
1347 std::vector<Key> keys;
1348 keys.reserve(connectedKeys.size());
1349 for(
const auto& k : connectedKeys)
1351 keys.push_back(std::static_pointer_cast<DataStormI::KeyT<Key>>(k)->get());
1353 init(std::move(keys));
1354 } : std::function<void(std::vector<std::shared_ptr<DataStormI::Key>>)>{},
1356 [update = std::move(update)](
CallbackReason action,
const std::shared_ptr<DataStormI::Key>& key)
1358 update(action, std::static_pointer_cast<DataStormI::KeyT<Key>>(key)->get());
1359 } : std::function<void(
CallbackReason, std::shared_ptr<DataStormI::Key>)>{});
1362 template<
typename Key,
typename Value,
typename UpdateTag>
1364 std::function<
void(std::vector<std::string>)> init,
1365 std::function<
void(
CallbackReason, std::string)> update)
noexcept
1367 _impl->onConnectedElements(std::move(init), std::move(update));
1370 template<
typename Key,
typename Value,
typename UpdateTag>
1376 :
Writer<Key, Value, UpdateTag>(
1377 topic.getWriter()->create({topic._keyFactory->create(key)}, std::move(name), config)),
1378 _tagFactory(topic._tagFactory)
1382 template<
typename Key,
typename Value,
typename UpdateTag>
1385 _tagFactory(std::move(writer._tagFactory))
1389 template<
typename Key,
typename Value,
typename UpdateTag>
1397 template<
typename Key,
typename Value,
typename UpdateTag>
1400 Writer<Key, Value, UpdateTag>::_impl->publish(
1402 std::make_shared<DataStormI::SampleT<Key, Value, UpdateTag>>(
SampleEvent::Add, value));
1405 template<
typename Key,
typename Value,
typename UpdateTag>
1408 Writer<Key, Value, UpdateTag>::_impl->publish(
1410 std::make_shared<DataStormI::SampleT<Key, Value, UpdateTag>>(
SampleEvent::Update, value));
1413 template<
typename Key,
typename Value,
typename UpdateTag>
1414 template<
typename UpdateValue>
1417 auto impl = Writer<Key, Value, UpdateTag>::_impl;
1418 auto updateTag = _tagFactory->create(tag);
1419 return [impl, updateTag](
const UpdateValue& value)
1421 auto encoded = DataStormI::EncoderT<UpdateValue>::encode(impl->getCommunicator(), value);
1422 impl->publish(
nullptr, std::make_shared<DataStormI::SampleT<Key, Value, UpdateTag>>(encoded, updateTag));
1426 template<
typename Key,
typename Value,
typename UpdateTag>
1429 Writer<Key, Value, UpdateTag>::_impl->publish(
1434 template<
typename Key,
typename Value,
typename UpdateTag>
1437 const std::vector<Key>& keys,
1440 :
Writer<Key, Value, UpdateTag>(
1441 topic.getWriter()->create(topic._keyFactory->create(keys), std::move(name), config)),
1442 _keyFactory(topic._keyFactory),
1443 _tagFactory(topic._tagFactory)
1447 template<
typename Key,
typename Value,
typename UpdateTag>
1450 _keyFactory(std::move(writer._keyFactory)),
1451 _tagFactory(std::move(writer._tagFactory))
1455 template<
typename Key,
typename Value,
typename UpdateTag>
1463 template<
typename Key,
typename Value,
typename UpdateTag>
1466 Writer<Key, Value, UpdateTag>::_impl->publish(
1467 _keyFactory->create(key),
1468 std::make_shared<DataStormI::SampleT<Key, Value, UpdateTag>>(
SampleEvent::Add, value));
1471 template<
typename Key,
typename Value,
typename UpdateTag>
1474 Writer<Key, Value, UpdateTag>::_impl->publish(
1475 _keyFactory->create(key),
1476 std::make_shared<DataStormI::SampleT<Key, Value, UpdateTag>>(
SampleEvent::Update, value));
1479 template<
typename Key,
typename Value,
typename UpdateTag>
1480 template<
typename UpdateValue>
1481 std::function<void(
const Key&,
const UpdateValue&)>
1484 auto impl = Writer<Key, Value, UpdateTag>::_impl;
1485 auto updateTag = _tagFactory->create(tag);
1486 auto keyFactory = _keyFactory;
1487 return [impl, updateTag, keyFactory](
const Key& key,
const UpdateValue& value)
1489 auto encoded = DataStormI::EncoderT<UpdateValue>::encode(impl->getCommunicator(), value);
1491 keyFactory->create(key),
1492 std::make_shared<DataStormI::SampleT<Key, Value, UpdateTag>>(encoded, updateTag));
1496 template<
typename Key,
typename Value,
typename UpdateTag>
1499 Writer<Key, Value, UpdateTag>::_impl->publish(
1500 _keyFactory->create(key),
1505 template<
typename Value> std::function<std::function<bool(const Value&)>(
const std::string&)> makeRegexFilter()
1508 return [](
const std::string& criteria)
1510 std::regex expr(criteria);
1511 return [expr = std::move(expr)](
const Value& value)
1513 std::ostringstream os;
1515 return std::regex_match(os.str(), expr);
1521 template<
typename Key,
typename Value,
typename UpdateTag>
1528 {
return std::find(criteria.begin(), criteria.end(), sample.getEvent()) != criteria.end(); };
1533 template<
typename T,
typename V,
typename Enabler =
void>
struct RegexFilter
1535 template<
typename F>
static void add(
const F&) {}
1539 template<
typename T,
typename V>
struct RegexFilter<T, V, std::enable_if_t<DataStormI::is_streamable<V>::value>>
1541 template<
typename F>
static void add(
const F& factory)
1543 factory->set(
"_regex", makeRegexFilter<T>());
1550 template<
typename Key,
typename Value,
typename UpdateTag>
1552 : _name(std::move(name)),
1553 _topicFactory(node._factory),
1554 _keyFactory(DataStormI::KeyFactoryT<Key>::createFactory()),
1555 _tagFactory(DataStormI::TagFactoryT<UpdateTag>::createFactory()),
1556 _keyFilterFactories(std::make_shared<DataStormI::FilterManagerT<DataStormI::KeyT<Key>>>()),
1557 _sampleFilterFactories(
1558 std::make_shared<DataStormI::FilterManagerT<DataStormI::SampleT<Key, Value, UpdateTag>>>())
1560 RegexFilter<Key, Key>::add(_keyFilterFactories);
1561 RegexFilter<Sample<Key, Value, UpdateTag>, Value>::add(_sampleFilterFactories);
1562 _sampleFilterFactories->set(
"_event", makeSampleEventFilter(*
this));
1567 std::lock_guard<std::mutex> lock(_mutex);
1578 template<
typename Key,
typename Value,
typename UpdateTag>
1581 std::lock_guard<std::mutex> lock(_mutex);
1590 _name = std::move(topic._name);
1591 _topicFactory = std::move(topic._topicFactory);
1592 _keyFactory = std::move(topic._keyFactory);
1593 _tagFactory = std::move(topic._tagFactory);
1594 _keyFilterFactories = std::move(topic._keyFilterFactories);
1595 _sampleFilterFactories = std::move(topic._sampleFilterFactories);
1596 _reader = std::move(topic._reader);
1597 _writer = std::move(topic._writer);
1598 _updaters = std::move(topic._updaters);
1602 template<
typename Key,
typename Value,
typename UpdateTag>
1608 template<
typename Key,
typename Value,
typename UpdateTag>
1611 getReader()->waitForWriters(
static_cast<int>(count));
1614 template<
typename Key,
typename Value,
typename UpdateTag>
1617 getReader()->waitForWriters(-1);
1620 template<
typename Key,
typename Value,
typename UpdateTag>
1623 getReader()->setDefaultConfig(config);
1626 template<
typename Key,
typename Value,
typename UpdateTag>
1629 return getWriter()->hasReaders();
1632 template<
typename Key,
typename Value,
typename UpdateTag>
1635 getWriter()->waitForReaders(
static_cast<int>(count));
1638 template<
typename Key,
typename Value,
typename UpdateTag>
1641 getWriter()->waitForReaders(-1);
1644 template<
typename Key,
typename Value,
typename UpdateTag>
1647 getWriter()->setDefaultConfig(config);
1650 template<
typename Key,
typename Value,
typename UpdateTag>
1651 template<
typename UpdateValue>
1653 const UpdateTag& tag,
1654 std::function<
void(Value&, UpdateValue)> updater)
noexcept
1656 std::lock_guard<std::mutex> lock(_mutex);
1657 auto tagI = _tagFactory->create(std::move(tag));
1660 [updater = std::move(updater)](
const std::shared_ptr<DataStormI::Sample>& previous,
1661 const std::shared_ptr<DataStormI::Sample>& next,
1669 assert(previous && previous->hasValue());
1671 if (previous && previous->hasValue())
1674 std::static_pointer_cast<DataStormI::SampleT<Key, Value, UpdateTag>>(previous)->getValue());
1676 updater(value, DataStormI::DecoderT<UpdateValue>::decode(communicator, next->getEncodedValue()));
1677 std::static_pointer_cast<DataStormI::SampleT<Key, Value, UpdateTag>>(next)->setValue(std::move(value));
1678 } : std::function<void(
const std::shared_ptr<DataStormI::Sample>&,
1679 const std::shared_ptr<DataStormI::Sample>&,
1682 if (_reader && !_writer)
1684 _reader->setUpdater(tagI, updaterImpl);
1686 else if (_writer && !_reader)
1688 _writer->setUpdater(tagI, updaterImpl);
1690 else if (_reader && _writer)
1692 _reader->setUpdater(tagI, updaterImpl);
1693 _writer->setUpdater(tagI, updaterImpl);
1697 _updaters[tagI] = updaterImpl;
1701 template<
typename Key,
typename Value,
typename UpdateTag>
1702 template<
typename Criteria>
1705 std::function<std::function<
bool(
const Key&)>(
const Criteria&)> factory)
noexcept
1707 std::lock_guard<std::mutex> lock(_mutex);
1708 _keyFilterFactories->set(std::move(name), std::move(factory));
1711 template<
typename Key,
typename Value,
typename UpdateTag>
1712 template<
typename Criteria>
1715 std::function<std::function<
bool(
const SampleType&)>(
const Criteria&)> factory)
noexcept
1717 std::lock_guard<std::mutex> lock(_mutex);
1718 _sampleFilterFactories->set(std::move(name), std::move(factory));
1721 template<
typename Key,
typename Value,
typename UpdateTag>
1722 std::shared_ptr<DataStormI::TopicReader> Topic<Key, Value, UpdateTag>::getReader()
const
1724 std::lock_guard<std::mutex> lock(_mutex);
1727 auto sampleFactory = std::make_shared<DataStormI::SampleFactoryT<Key, Value, UpdateTag>>();
1728 _reader = _topicFactory->createTopicReader(
1732 std::move(sampleFactory),
1733 _keyFilterFactories,
1734 _sampleFilterFactories);
1735 _reader->setUpdaters(_writer ? _writer->getUpdaters() : _updaters);
1741 template<
typename Key,
typename Value,
typename UpdateTag>
1742 std::shared_ptr<DataStormI::TopicWriter> Topic<Key, Value, UpdateTag>::getWriter()
const
1744 std::lock_guard<std::mutex> lock(_mutex);
1747 _writer = _topicFactory->createTopicWriter(
1752 _keyFilterFactories,
1753 _sampleFilterFactories);
1754 _writer->setUpdaters(_reader ? _reader->getUpdaters() : _updaters);
1760 template<
typename Key,
typename Value,
typename UpdateTag>
1763 return _topicFactory->getCommunicator();
1767#if defined(__clang__)
1768# pragma clang diagnostic pop
1769#elif defined(__GNUC__)
1770# pragma GCC diagnostic pop
FilteredKeyReader & operator=(FilteredKeyReader &&reader) noexcept
Move assignment operator.
FilteredKeyReader(const Topic< Key, Value, UpdateTag > &topic, const Filter< KeyFilterCriteria > &keyFilter, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Constructs a new reader for the given key filter.
The filtered reader to read data elements whose key match a given filter.
MultiKeyReader(const Topic< Key, Value, UpdateTag > &topic, const std::vector< Key > &keys, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Constructs a new reader for the given keys.
MultiKeyReader & operator=(MultiKeyReader &&reader) noexcept
Move assignment operator.
The key reader to read the data element associated with a given set of keys.
std::function< void(const Key &, const UpdateValue &)> partialUpdate(const UpdateTag &tag)
Gets a partial update generator function for the given partial update tag.
void add(const Key &key, const Value &value)
Adds the data element.
MultiKeyWriter(const Topic< Key, Value, UpdateTag > &topic, const std::vector< Key > &keys, std::string name=std::string(), const WriterConfig &config=WriterConfig())
Constructs a new writer for the given keys.
void remove(const Key &key) noexcept
Removes the data element.
void update(const Key &key, const Value &value)
Updates the data element.
MultiKeyWriter & operator=(MultiKeyWriter &&writer) noexcept
Move assignment operator.
The key writer to write data elements associated with a given set of keys.
The Node class allows creating topic readers and writers.
The ReaderConfig class specifies configuration options specific to readers.
std::vector< Key > getConnectedKeys() const
Gets the keys for which writers are connected to this reader.
void onConnectedWriters(std::function< void(std::vector< std::string >)> init, std::function< void(CallbackReason, std::string)> update) noexcept
Calls the given functions to provide the initial set of connected writers and when a new writer conne...
std::vector< std::string > getConnectedWriters() const
Gets the connected writers.
Reader & operator=(Reader &&reader) noexcept
Move assignment operator.
void waitForNoWriters() const
Waits for writers to be offline.
Value ValueType
The value type.
Reader(Reader &&reader) noexcept
Move constructor.
void waitForWriters(unsigned int count=1) const
Waits for the given number of writers to be online.
bool hasUnread() const noexcept
Returns whether or not unread samples are available.
void onConnectedKeys(std::function< void(std::vector< Key >)> init, std::function< void(CallbackReason, Key)> update) noexcept
Calls the given functions to provide the initial set of connected keys and when a key is added or rem...
std::vector< Sample< Key, Value, UpdateTag > > getAllUnread()
Returns all the unread samples.
Sample< Key, Value, UpdateTag > getNextUnread()
Returns the next unread sample.
bool hasWriters() const noexcept
Indicates whether or not writers are online.
void waitForUnread(unsigned int count=1) const
Waits for the given number of unread samples to be available.
void onSamples(std::function< void(std::vector< Sample< Key, Value, UpdateTag > >)> init, std::function< void(Sample< Key, Value, UpdateTag >)> queue) noexcept
Calls the given function to provide the initial set of unread samples and when new samples are queued...
The Reader class is used to retrieve samples for a data element.
const Value & getValue() const noexcept
Value ValueType
The type of the sample value.
const std::string & getSession() const noexcept
UpdateTag getUpdateTag() const
const std::string & getOrigin() const noexcept
UpdateTag UpdateTagType
The type of the update tag.
Key KeyType
The type of the sample key.
SampleEvent getEvent() const noexcept
Gets the event associated with the sample.
std::chrono::time_point< std::chrono::system_clock > getTimeStamp() const noexcept
const Key & getKey() const noexcept
A sample provides information about a data element update.
SingleKeyReader & operator=(SingleKeyReader &&reader) noexcept
Move assignment operator.
SingleKeyReader(const Topic< Key, Value, UpdateTag > &topic, const Key &key, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Constructs a new reader for the given key.
The key reader to read the data element associated with a given key.
std::function< void(const UpdateValue &)> partialUpdate(const UpdateTag &tag)
Gets a partial update generator function for the given partial update tag.
void add(const Value &value)
Adds the data element.
void update(const Value &value)
Updates the data element.
void remove() noexcept
Removes the data element.
SingleKeyWriter & operator=(SingleKeyWriter &&writer) noexcept
Move assignment operator.
SingleKeyWriter(const Topic< Key, Value, UpdateTag > &topic, const Key &key, std::string name=std::string(), const WriterConfig &config=WriterConfig())
Constructs a new writer for the given key.
The key writer to write the data element associated with a given key.
void setUpdater(const UpdateTag &tag, std::function< void(Value &, UpdateValue)> updater) noexcept
Sets an updater function for the given update tag.
Topic(const Node &node, std::string name) noexcept
Constructs a new Topic for the topic with the given name.
void setKeyFilter(std::string name, std::function< std::function< bool(const Key &)>(const Criteria &)> factory) noexcept
Sets a key filter factory.
UpdateTag UpdateTagType
The topic's update tag type (defaults to std::string if not specified).
Key KeyType
The topic's key type.
void waitForReaders(unsigned int count=1) const
Waits for the given number of data readers to be online.
void waitForNoWriters() const
Waits for data writers to be offline.
Value ValueType
The topic's value type.
bool hasReaders() const noexcept
Indicates whether or not data readers are online.
Sample< Key, Value, UpdateTag > SampleType
The topic's sample type.
void setSampleFilter(std::string name, std::function< std::function< bool(const SampleType &)>(const Criteria &)> factory) noexcept
Sets a sample filter factory.
Reader< Key, Value, UpdateTag > ReaderType
The topic's reader type.
bool hasWriters() const noexcept
Indicates whether or not data writers are online.
void setWriterDefaultConfig(const WriterConfig &config) noexcept
Sets the default configuration used to construct writers.
Topic & operator=(Topic &&topic) noexcept
Move assignment operator.
Writer< Key, Value, UpdateTag > WriterType
The topic's writer type.
void setReaderDefaultConfig(const ReaderConfig &config) noexcept
Sets the default configuration used to construct readers.
Topic(Topic &&topic) noexcept
Move constructor.
void waitForWriters(unsigned int count=1) const
Waits for the given number of data writers to be online.
void waitForNoReaders() const
Waits for data readers to be offline.
The WriterConfig class specifies configuration options specific to writers.
Sample< Key, Value, UpdateTag > getLast()
Gets the last written sample.
Writer & operator=(Writer &&writer) noexcept
Move assignment operator.
std::vector< Sample< Key, Value, UpdateTag > > getAll()
Gets all the written sample kept in the writer history.
std::vector< std::string > getConnectedReaders() const
Gets the connected readers.
void onConnectedReaders(std::function< void(std::vector< std::string >)> init, std::function< void(CallbackReason, std::string)> update) noexcept
Calls the given functions to provide the initial set of connected readers and when a new reader conne...
void waitForReaders(unsigned int count=1) const
Waits for the given number of readers to be online.
void onConnectedKeys(std::function< void(std::vector< Key >)> init, std::function< void(CallbackReason, Key)> update) noexcept
Calls the given functions to provide the initial set of connected keys and when a key is added or rem...
bool hasReaders() const noexcept
Indicates whether or not readers are online.
std::vector< Key > getConnectedKeys() const
Gets the keys for which readers are connected to this writer.
Value ValueType
The value type.
Writer(Writer &&writer) noexcept
Move constructor.
void waitForNoReaders() const
Waits for readers to be offline.
The Writer class is used to write samples for a data element.
SampleEvent
Describes the operation used by a data writer to update a data element.
@ Update
The data writer updated the element.
@ Remove
The data writer removed the element.
@ Add
The data writer added the element.
SingleKeyReader< K, V, UT > makeSingleKeyReader(const Topic< K, V, UT > &topic, const typename Topic< K, V, UT >::KeyType &key, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Creates a key reader for the given topic and key.
FilteredKeyReader< K, V, UT > makeFilteredKeyReader(const Topic< K, V, UT > &topic, const Filter< KFC > &filter, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Creates a new filtered reader for the given topic and key filter.
MultiKeyReader< K, V, UT > makeAnyKeyReader(const Topic< K, V, UT > &topic, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Creates an any-key reader for the given topic.
std::vector< SampleEvent > SampleEventSeq
A sequence of sample events.
MultiKeyWriter< K, V, UT > makeMultiKeyWriter(const Topic< K, V, UT > &topic, const std::vector< typename Topic< K, V, UT >::KeyType > &keys, std::string name=std::string(), const WriterConfig &config=WriterConfig())
Creates a multi-key writer for the given topic and keys.
SingleKeyWriter< K, V, UT > makeSingleKeyWriter(const Topic< K, V, UT > &topic, const typename Topic< K, V, UT >::KeyType &key, std::string name=std::string(), const WriterConfig &config=WriterConfig())
Creates a key writer for the given topic and key.
MultiKeyReader< K, V, UT > makeMultiKeyReader(const Topic< K, V, UT > &topic, const std::vector< typename Topic< K, V, UT >::KeyType > &keys, std::string name=std::string(), const ReaderConfig &config=ReaderConfig())
Creates a multi-key reader for the given topic.
std::ostream & operator<<(std::ostream &os, const SampleEventSeq &types)
Converts the given sample type vector to a string and add it to the stream.
MultiKeyWriter< K, V, UT > makeAnyKeyWriter(const Topic< K, V, UT > &topic, std::string name=std::string(), const WriterConfig &config=WriterConfig())
Creates an any-key writer for the given topic.
CallbackReason
The callback action enumerator specifies the reason why a callback is called.
Data-centric, broker-less publish/subscribe framework. C++ only.
std::shared_ptr< Communicator > CommunicatorPtr
A shared pointer to a Communicator.
void print(std::ostream &stream, T v)
Prints a value to a stream.
static T clone(const T &value) noexcept
Clones the given value.
Filter(std::string name, TT &&criteria) noexcept
Constructs a filter structure with the given name and criteria.
std::string name
The filter name.
T criteria
The filter criteria value.
Filter structure to specify the filter name and criteria value.