Vanetza
Loading...
Searching...
No Matches
factory.hpp
1#ifndef FACTORY_HPP_QLKNHPWZ
2#define FACTORY_HPP_QLKNHPWZ
3
4#include <functional>
5#include <map>
6#include <memory>
7#include <utility>
8
9namespace vanetza
10{
11
12/**
13 * Factory for a group of classes implementing T
14 */
15template<typename T, typename... Args>
17{
18public:
19 using Result = std::unique_ptr<T>;
20 using Function = std::function<Result(Args...)>;
21
22 Factory() : m_default(m_functions.end())
23 {
24 }
25
26 /**
27 * Create an instance of T using a named implementation
28 * \param name of wanted implementation
29 * \return created instance or nullptr if not found
30 */
31 Result create(const std::string& name, Args... args) const
32 {
33 std::unique_ptr<T> obj;
34 auto found = m_functions.find(name);
35 if (found != m_functions.end()) {
36 obj = found->second(std::forward<Args>(args)...);
37 }
38 return obj;
39 }
40
41 /**
42 * Create object using default implementation
43 * \return created instance or nullptr if no default is configured
44 */
45 Result create(Args... args) const
46 {
47 std::unique_ptr<T> obj;
48 if (m_default != m_functions.end()) {
49 obj = m_default->second(std::forward<Args>(args)...);
50 }
51 return obj;
52 }
53
54 /**
55 * Add an implementation to factory
56 * \param name of implementation
57 * \param f function creating a new instance of this implementation
58 * \return true if added successfully, i.e. no previous addition with same name
59 */
60 bool add(const std::string& name, Function f)
61 {
62 return m_functions.emplace(name, std::move(f)).second;
63 }
64
65 /**
66 * Set default implementation
67 * \param name selected default implementation
68 * \return true if an implementation exists with selected name
69 */
70 bool configure_default(const std::string& name)
71 {
72 m_default = m_functions.find(name);
73 return m_default != m_functions.end();
74 }
75
76private:
77 using map_type = std::map<std::string, Function>;
78 map_type m_functions;
79 typename map_type::const_iterator m_default;
80};
81
82} // namespace vanetza
83
84#endif /* FACTORY_HPP_QLKNHPWZ */
ChunckPacket is a packet consisting of several memory chunks.
ByteBufferConvertible & operator[](OsiLayer ol)
ChunkPacket & merge(ChunkPacket &packet, OsiLayer from, OsiLayer to)
std::size_t size() const
const ByteBufferConvertible & layer(OsiLayer ol) const
const ByteBufferConvertible & operator[](OsiLayer ol) const
ByteBufferConvertible & layer(OsiLayer ol)
std::size_t size(OsiLayer from, OsiLayer to) const
ChunkPacket extract(OsiLayer from, OsiLayer to)
std::size_t size(OsiLayer from, OsiLayer to) const
buffer_const_range operator[](OsiLayer layer) const
std::size_t size() const
void set_boundary(OsiLayer, unsigned bytes)
const ByteBuffer & buffer() const
CohesivePacket(const ByteBuffer &buffer, OsiLayer layer)
void trim(OsiLayer from, unsigned bytes)
std::size_t size(OsiLayer single_layer) const
Result create(Args... args) const
Definition factory.hpp:45
value_type operator[](size_type) const
Definition byte_view.cpp:48
byte_view_range(ByteBuffer &&)
Definition byte_view.cpp:37
byte_view_range(const ByteBuffer::const_iterator &, const ByteBuffer::const_iterator &)
Definition byte_view.cpp:27
ByteBuffer::const_pointer data() const
Definition byte_view.cpp:42
void encode(units::Duration)
Definition lifetime.cpp:45
units::Duration decode() const
Definition lifetime.cpp:59
bool after(const Timestamp &other) const
Definition timestamp.cpp:76
bool before(const Timestamp &other) const
Definition timestamp.cpp:71
virtual boost::optional< Uncompressed > decompress_point(const EccPoint &ecc_point)=0
decompress a possibly compressed elliptic curve point
static CertificateValidity valid()
Create CertificateValidity signalling a valid certificate This method is equivalent to default constr...
CertificateValidity(CertificateInvalidReason reason)
CertificateInvalidReason reason() const
Get reason for certificate invalidity This call is only safe if reason is available,...
IntX specified in TS 103 097 v1.2.1, section 4.2.1.
Definition int_x.hpp:21
static DecapConfirm from(VerifyConfirm &&verify_confirm, const SecuredMessageView &msg_view)
Input data for decapsulating a secured message.
EcdsaSignature specified in TS 103 097 v1.2.1, section 4.2.9.
Definition signature.hpp:17
Uncompressed specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:36
described in TS 103 097 v1.2.1 (2015-06), section 6.1
void add_permission(ItsAid aid, const ByteBuffer &ssp)
const ValidityRestriction * get_restriction(ValidityRestrictionType type) const
const SubjectAttribute * get_attribute(SubjectAttributeType type) const
void remove_attribute(SubjectAttributeType type)
const validity_restriction_type< T > * get_restriction() const
const subject_attribute_type< T > * get_attribute() const
void remove_restriction(ValidityRestrictionType type)
Payload specified in TS 103 097 v1.2.1, section 5.2.
Definition payload.hpp:28
SecuredMessage as specified in TS 103 097 v1.2.1, section 5.1.
HeaderField * header_field(HeaderFieldType)
const TrailerField * trailer_field(TrailerFieldType type) const
const HeaderField * header_field(HeaderFieldType type) const
TrailerField * trailer_field(TrailerFieldType)
described in TS 103 097 v1.2.1, section 6.2
ThreeDLocation specified in TS 103 097 v1.2.1, section 4.2.19.
Definition region.hpp:21
Time64WithStandardDeviation specified in TS 103 097 v1.2.1, section 4.2.16.
resolve type for matching HeaderFieldType
resolve type for matching TrailerFieldType