CatapultServer  v0.5.0.1 (Elephant)
BasicServerHooks.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/exceptions.h"
23 #include <vector>
24 
25 namespace catapult { namespace extensions {
26 
28  template<typename TFunc>
29  void SetOnce(TFunc& dest, const TFunc& source) {
30  if (dest)
31  CATAPULT_THROW_INVALID_ARGUMENT("server hook cannot be set more than once");
32 
33  dest = source;
34  }
35 
37  template<typename TFunc>
38  const TFunc& Require(const TFunc& func) {
39  if (!func)
40  CATAPULT_THROW_INVALID_ARGUMENT("server hook has not been set");
41 
42  return func;
43  }
44 
46  template<typename TConsumer>
47  TConsumer AggregateConsumers(const std::vector<TConsumer>& consumers) {
48  return [consumers](const auto& data) {
49  for (const auto& consumer : consumers)
50  consumer(data);
51  };
52  }
53 }}
exceptions.h
catapult::extensions::Require
const TFunc & Require(const TFunc &func)
Returns func if and only if it is set.
Definition: BasicServerHooks.h:38
catapult::extensions::SetOnce
void SetOnce(TFunc &dest, const TFunc &source)
Sets dest to source if and only if dest is unset.
Definition: BasicServerHooks.h:29
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::consumer
std::function< void(TArgs...)> consumer
A consumer function.
Definition: functions.h:35
catapult::extensions::AggregateConsumers
TConsumer AggregateConsumers(const std::vector< TConsumer > &consumers)
Aggregates multiple consumers into a single consumer.
Definition: BasicServerHooks.h:47