summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyleLayer_Private.h
blob: 656f74ca423188b2798fe9818180635b1298ff44 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#import <Foundation/Foundation.h>

#import "MGLStyleLayer.h"
#import "MGLStyleValue_Private.h"

#include <mbgl/style/layer.hpp>

NS_ASSUME_NONNULL_BEGIN

// A struct to be stored in the `peer` member of mbgl::style::Layer, in order to implement
// object identity. We don't store a MGLStyleLayer pointer directly because that doesn't
// interoperate with ARC. The inner pointer is weak in order to avoid a reference cycle for
// "pending" MGLStyleLayers, which have a strong owning pointer to the mbgl::style::Layer.
struct LayerWrapper {
    __weak MGLStyleLayer *layer;
};

/**
 Assert that the style layer is valid.

 This macro should be used at the beginning of any public-facing instance method
 of `MGLStyleLayer` and its subclasses. For private methods, an assertion is more appropriate.
 */
#define MGLAssertStyleLayerIsValid() \
    do { \
        if (!self.rawLayer) { \
            [NSException raise:MGLInvalidStyleLayerException \
                        format: \
            @"-[MGLStyle removeLayer:] has been called " \
            @"with this instance but another style layer instance was added with the same identifer. It is an " \
            @"error to send any message to this layer since it cannot be recovered after removal due to the " \
            @"identifier collision. Use unique identifiers for all layer instances including layers of " \
            @"different types."]; \
        } \
    } while (NO);

@class MGLStyle;

@interface MGLStyleLayer (Private)

/**
 Initializes and returns a layer with a raw pointer to the backing store,
 associated with a style.
 */
- (instancetype)initWithRawLayer:(mbgl::style::Layer *)rawLayer;

/**
 Initializes and returns a layer with an owning pointer to the backing store,
 unassociated from a style.
 */
- (instancetype)initWithPendingLayer:(std::unique_ptr<mbgl::style::Layer>)pendingLayer;

@property (nonatomic, readwrite, copy) NSString *identifier;

/**
 A raw pointer to the mbgl object, which is always initialized, either to the
 value returned by `mbgl::Map getLayer`, or for independently created objects,
 to the pointer value held in `pendingLayer`. In the latter case, this raw
 pointer value stays even after ownership of the object is transferred via
 `mbgl::Map addLayer`.
 */
@property (nonatomic, readonly) mbgl::style::Layer *rawLayer;

/**
 Adds the mbgl style layer that this object represents to the mbgl map below the
 specified `otherLayer`.

 Once a mbgl style layer is added, ownership of the object is transferred to the
 `mbgl::Map` and this object no longer has an active unique_ptr reference to the
 `mbgl::style::Layer`.
 */
- (void)addToStyle:(MGLStyle *)style belowLayer:(nullable MGLStyleLayer *)otherLayer;

/**
 Removes the mbgl style layer that this object represents from the mbgl map.

 When a mbgl style layer is removed, ownership of the object is transferred back
 to the `MGLStyleLayer` instance and the unique_ptr reference is valid again. It
 is safe to add the layer back to the style after it is removed.
 */
- (void)removeFromStyle:(MGLStyle *)style;

@end

namespace mbgl {

class LayerPeerFactory {
public:
    virtual ~LayerPeerFactory() = default;
    /**
     Get the corresponding core layer factory.
     */
    virtual LayerFactory* getCoreLayerFactory() = 0;
    /**
     Creates an MGLStyleLayer instance with a raw pointer to the backing store.
     */
    virtual MGLStyleLayer* createPeer(style::Layer*) = 0;
};

}  // namespace mbgl

NS_ASSUME_NONNULL_END