aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/vespa/documentapi/messagebus/iroutablefactory.h
blob: cb1a046107ac932869b236e2c7373bc19406f664 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/document/util/bytebuffer.h>
#include <vespa/messagebus/routable.h>
#include <vespa/vespalib/component/version.h>
#include <vespa/vespalib/util/growablebytebuffer.h>

namespace documentapi {

/**
 * This interface defines the necessary methods of a routable factory that can be plugged into a {@link
 * DocumentProtocol} using the {@link DocumentProtocol#putRoutableFactory(int, RoutableFactory,
 * com.yahoo.component.VersionSpecification)} method. Consider extending {@link DocumentMessageFactory} or
 * {@link DocumentReplyFactory} instead of implementing this interface.
 *
 * Notice that no routable type is passed to the {@link #decode(ByteBuffer)} method, so you may NOT share a
 * factory across multiple routable types. To share serialization logic between factory use a common
 * superclass or composition with a common serialization utility.
 */
class IRoutableFactory {
protected:
    IRoutableFactory() = default;
public:
    /**
     * Convenience typedefs.
     */
    using UP = std::unique_ptr<IRoutableFactory>;
    using SP = std::shared_ptr<IRoutableFactory>;

    IRoutableFactory(const IRoutableFactory &) = delete;
    IRoutableFactory & operator = (const IRoutableFactory &) = delete;
    /**
     * Virtual destructor required for inheritance.
     */
    virtual ~IRoutableFactory() { }

    /**
     * This method encodes the content of the given routable into a byte buffer that can later be decoded
     * using the {@link #decode(ByteBuffer)} method.
     *
     * This method is NOT exception safe. Return false to signal failure.
     *
     * @param obj The routable to encode.
     * @param out The buffer to write into.
     * @return True if the routable could be encoded.
     */
    virtual bool encode(const mbus::Routable &obj,
                        vespalib::GrowableByteBuffer &out) const = 0;

    /**
     * This method decodes the given byte bufer to a routable.
     *
     * This method is NOT exception safe. Return null to signal failure.
     *
     * @param in The buffer to read from.
     * @param loadTypes The set of configured load types.
     * @return The decoded routable.
     */
    virtual mbus::Routable::UP decode(document::ByteBuffer &in) const = 0;
};

}