Skip to content

Commit 9bf748f

Browse files
authored
c4PeerDiscovery.hh no longer uses nonpublic headers (#2301)
1 parent 5886a90 commit 9bf748f

3 files changed

Lines changed: 56 additions & 40 deletions

File tree

C/Cpp_include/Observer.hh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Observer.hh
3+
//
4+
// Copyright 2025-Present Couchbase, Inc.
5+
//
6+
// Use of this software is governed by the Business Source License included
7+
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
8+
// in that file, in accordance with the Business Source License, use of this
9+
// software will be governed by the Apache License, Version 2.0, included in
10+
// the file licenses/APL2.txt.
11+
//
12+
13+
#pragma once
14+
#include "c4Compat.h"
15+
#include <atomic>
16+
17+
C4_ASSUME_NONNULL_BEGIN
18+
19+
namespace litecore {
20+
class ObserverListBase;
21+
22+
/** Base class of observer interfaces used by ObserverList<>. */
23+
class Observer {
24+
public:
25+
Observer() = default;
26+
27+
Observer(const Observer&) {} // deliberately avoids setting _list
28+
29+
/// Removes this observer from any ObserverList it was added to.
30+
/// @warning Any subclass that implements observer methods **must** call this (from its
31+
/// destructor or earlier) if has been added to an ObserverList. Otherwise its notification
32+
/// methods could be called after it's been destructed, causing crashes or worse.
33+
void removeFromObserverList();
34+
35+
protected:
36+
virtual ~Observer();
37+
38+
private:
39+
friend class ObserverListBase;
40+
Observer& operator=(const Observer&) = delete;
41+
Observer(Observer&&) = delete;
42+
43+
std::atomic<ObserverListBase*> _list = nullptr; // The ObserverList I belong to
44+
};
45+
} // namespace litecore
46+
47+
C4_ASSUME_NONNULL_END

C/Cpp_include/c4PeerDiscovery.hh

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
#pragma once
1414
#include "c4Base.hh"
15-
#include "c4DatabaseTypes.h"
1615
#include "c4PeerSyncTypes.h" // for C4PeerID
1716
#include "c4Error.h"
18-
#include "ObserverList.hh"
17+
#include "Observer.hh"
1918
#include "fleece/InstanceCounted.hh"
2019
#include "fleece/RefCounted.hh"
2120
#include <functional>
@@ -24,6 +23,7 @@
2423
#include <span>
2524
#include <unordered_map>
2625
#include <vector>
26+
#include <memory>
2727

2828
#ifdef COUCHBASE_ENTERPRISE
2929
C4_ASSUME_NONNULL_BEGIN
@@ -80,18 +80,18 @@ class C4PeerDiscovery {
8080
~C4PeerDiscovery();
8181

8282
/// The peer group ID.
83-
std::string const& peerGroupID() const { return _peerGroupID; }
83+
std::string const& peerGroupID() const;
8484

8585
/// This device's peer ID.
86-
C4PeerID const& peerID() const { return _peerID; }
86+
C4PeerID const& peerID() const;
8787

8888
/// The `C4PeerDiscoveryProvider`s in use.
89-
std::vector<ProviderRef> const& providers() const { return _providers; }
89+
std::vector<ProviderRef> const& providers() const;
9090

9191
/// Registers a default C4SocketFactory to be used when a Provider doesn't have a custom one.
9292
/// This factory is expected to handle normal IP-based WebSocket connections.
9393
/// @warning Do not call this after there have been any calls to `startPublishing`.
94-
static void setDefaultSocketFactory(C4SocketFactory const&);
94+
CBL_CORE_API static void setDefaultSocketFactory(C4SocketFactory const&);
9595

9696
/// Tells providers to start looking for peers.
9797
void startBrowsing();
@@ -181,13 +181,8 @@ class C4PeerDiscovery {
181181
void notifyMetadataChanged(C4Peer*);
182182

183183
private:
184-
std::mutex _mutex;
185-
std::string const _peerGroupID;
186-
C4PeerID const _peerID;
187-
std::vector<ProviderRef> _providers; // List of providers. Never changes.
188-
std::unordered_map<std::string, fleece::Ref<C4Peer>> _peers;
189-
std::vector<fleece::Ref<C4Peer>> _peersComing, _peersGoing;
190-
litecore::ObserverList<Observer> _observers;
184+
struct Impl;
185+
std::unique_ptr<Impl> _impl;
191186
};
192187

193188
# pragma mark - PEER:

LiteCore/Support/ObserverList.hh

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,16 @@
1111
//
1212

1313
#pragma once
14-
#include "c4Compat.h"
14+
#include "Observer.hh"
1515
#include "SmallVector.hh"
1616
#include "fleece/function_ref.hh"
1717
#include "fleece/PlatformCompat.hh" // for ssize_t
18-
#include <atomic>
1918
#include <concepts>
2019
#include <mutex>
2120

2221
C4_ASSUME_NONNULL_BEGIN
2322

2423
namespace litecore {
25-
class ObserverListBase;
26-
27-
/** Base class of observer interfaces used by ObserverList<>. */
28-
class Observer {
29-
public:
30-
Observer() = default;
31-
32-
Observer(const Observer&) {} // deliberately avoids setting _list
33-
34-
/// Removes this observer from any ObserverList it was added to.
35-
/// @warning Any subclass that implements observer methods **must** call this (from its
36-
/// destructor or earlier) if has been added to an ObserverList. Otherwise its notification
37-
/// methods could be called after it's been destructed, causing crashes or worse.
38-
void removeFromObserverList();
39-
40-
protected:
41-
virtual ~Observer();
42-
43-
private:
44-
friend class ObserverListBase;
45-
Observer& operator=(const Observer&) = delete;
46-
Observer(Observer&&) = delete;
47-
48-
std::atomic<ObserverListBase*> _list = nullptr; // The ObserverList I belong to
49-
};
5024

5125
// Abstract superclass of ObserverList<>.
5226
class ObserverListBase {

0 commit comments

Comments
 (0)