aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2021-01-19 12:34:44 +0100
committerSven Gothel <[email protected]>2021-01-19 12:34:44 +0100
commitf52391f0b280c2a8b7ab5b1a4baab4991e0ddf89 (patch)
tree7c823c1b094033c32370aafdacb5b6647241db84
parent0412bbb5d49ef9293742d3a40549e67ad6dfc666 (diff)
smart_ptr-6: Create make_shared(..) vehicle w/ public ctor and private cookie for friends to benefit from single mem-allocation via std::make_shared<T>(..)
-rw-r--r--api/direct_bt/DBTAdapter.hpp11
-rw-r--r--api/direct_bt/DBTDevice.hpp11
-rw-r--r--src/direct_bt/DBTAdapter.cpp9
-rw-r--r--src/direct_bt/DBTDevice.cpp3
-rw-r--r--src/direct_bt/DBTManager.cpp6
5 files changed, 28 insertions, 12 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp
index 1ab4fc90..b69a1c37 100644
--- a/api/direct_bt/DBTAdapter.hpp
+++ b/api/direct_bt/DBTAdapter.hpp
@@ -250,7 +250,13 @@ namespace direct_bt {
static std::shared_ptr<DBTDevice> findDevice(device_list_t & devices, const EUI48 & address, const BDAddressType addressType) noexcept;
static std::shared_ptr<DBTDevice> findDevice(device_list_t & devices, DBTDevice const & device) noexcept;
- DBTAdapter(DBTManager& mgmt_, const AdapterInfo& adapterInfo_) noexcept;
+ /** Private class only for private make_shared(). */
+ class ctor_cookie { friend DBTAdapter; ctor_cookie(const uint16_t secret) { (void)secret; } };
+
+ /** Private std::make_shared<DBTAdapter>(..) vehicle for friends. */
+ static std::shared_ptr<DBTAdapter> make_shared(DBTManager& mgmt_, const AdapterInfo& adapterInfo_) {
+ return std::make_shared<DBTAdapter>(DBTAdapter::ctor_cookie(0), mgmt_, adapterInfo_);
+ }
/**
* Closes all device connections, stops discovery and cleans up all references.
@@ -333,6 +339,9 @@ namespace direct_bt {
public:
+ /** Private ctor for private DBTAdapter::make_shared() intended for friends. */
+ DBTAdapter(const DBTAdapter::ctor_cookie& cc, DBTManager& mgmt_, const AdapterInfo& adapterInfo_) noexcept;
+
DBTAdapter(const DBTAdapter&) = delete;
void operator=(const DBTAdapter&) = delete;
diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp
index fe0c6d2b..92b56b46 100644
--- a/api/direct_bt/DBTDevice.hpp
+++ b/api/direct_bt/DBTDevice.hpp
@@ -114,7 +114,13 @@ namespace direct_bt {
std::mutex mtx_pairing;
jau::sc_atomic_bool sync_pairing;
- DBTDevice(DBTAdapter & adapter, EInfoReport const & r);
+ /** Private class only for private make_shared(). */
+ class ctor_cookie { friend DBTDevice; ctor_cookie(const uint16_t secret) { (void)secret; } };
+
+ /** Private std::make_shared<DBTDevice>(..) vehicle for friends. */
+ static std::shared_ptr<DBTDevice> make_shared(DBTAdapter & adapter, EInfoReport const & r) {
+ return std::make_shared<DBTDevice>(DBTDevice::ctor_cookie(0), adapter, r);
+ }
/** Add advertised service (GAP discovery) */
bool addAdvService(std::shared_ptr<uuid_t> const &uuid) noexcept;
@@ -212,6 +218,9 @@ namespace direct_bt {
/** Device's unique mac address and type tuple. */
const BDAddressAndType addressAndType; // FIXME: Mutable for resolvable -> identity during pairing?
+ /** Private ctor for private DBTDevice::make_shared() intended for friends. */
+ DBTDevice(const DBTDevice::ctor_cookie& cc, DBTAdapter & adapter, EInfoReport const & r);
+
DBTDevice(const DBTDevice&) = delete;
void operator=(const DBTDevice&) = delete;
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index 82e498e3..2edbb4bc 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -197,7 +197,7 @@ errout0:
return false;
}
-DBTAdapter::DBTAdapter(DBTManager& mgmt_, const AdapterInfo& adapterInfo_) noexcept
+DBTAdapter::DBTAdapter(const DBTAdapter::ctor_cookie& cc, DBTManager& mgmt_, const AdapterInfo& adapterInfo_) noexcept
: debug_event(jau::environment::getBooleanProperty("direct_bt.debug.adapter.event", false)),
debug_lock(jau::environment::getBooleanProperty("direct_bt.debug.adapter.lock", false)),
mgmt( mgmt_ ),
@@ -205,6 +205,7 @@ DBTAdapter::DBTAdapter(DBTManager& mgmt_, const AdapterInfo& adapterInfo_) noexc
dev_id( adapterInfo.dev_id ),
hci( dev_id )
{
+ (void)cc;
valid = validateDevInfo();
}
@@ -1037,8 +1038,7 @@ bool DBTAdapter::mgmtEvDeviceConnectedHCI(const MgmtEvent& e) noexcept {
}
if( nullptr == device ) {
// a whitelist auto-connect w/o previous discovery
- // private ctor: device = std::make_shared<DBTDevice>(*this, ad_report);
- device = std::shared_ptr<DBTDevice>(new DBTDevice(*this, ad_report));
+ device = DBTDevice::make_shared(*this, ad_report);
addDiscoveredDevice(device);
addSharedDevice(device);
new_connect = 3;
@@ -1327,8 +1327,7 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
//
// new device
//
- // private ctor: dev = std::make_shared<DBTDevice>(*this, *eir);
- dev = std::shared_ptr<DBTDevice>(new DBTDevice(*this, *eir));
+ dev = DBTDevice::make_shared(*this, *eir);
addDiscoveredDevice(dev);
addSharedDevice(dev);
COND_PRINT(debug_event, "DBTAdapter:hci:DeviceFound: Use new %s, %s",
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index d37a8318..38824bac 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -43,12 +43,13 @@
using namespace direct_bt;
-DBTDevice::DBTDevice(DBTAdapter & a, EInfoReport const & r)
+DBTDevice::DBTDevice(const ctor_cookie& cc, DBTAdapter & a, EInfoReport const & r)
: adapter(a),
l2cap_att(adapter.getAddress(), L2CAP_PSM_UNDEF, L2CAP_CID_ATT),
ts_creation(r.getTimestamp()),
addressAndType{r.getAddress(), r.getAddressType()}
{
+ (void)cc;
ts_last_discovery = ts_creation;
hciConnHandle = 0;
le_features = LEFeatures::NONE;
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index 0d4136e9..70248d60 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -486,8 +486,7 @@ next1:
const uint16_t dev_id = jau::get_uint16(data, 2+i*2, true /* littleEndian */);
std::unique_ptr<AdapterInfo> adapterInfo = initAdapter(dev_id, defaultBTMode);
if( nullptr != adapterInfo ) {
- // private: std::shared_ptr<DBTAdapter> adapter = std::make_shared<DBTAdapter>(*this, *adapterInfo);
- std::shared_ptr<DBTAdapter> adapter( new DBTAdapter(*this, *adapterInfo) );
+ std::shared_ptr<DBTAdapter> adapter = DBTAdapter::make_shared(*this, *adapterInfo);
adapters.push_back( adapter );
adapterIOCapability.push_back(defaultIOCapability);
DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: %s", i, num_adapter, dev_id, adapter->toString().c_str());
@@ -655,8 +654,7 @@ std::shared_ptr<DBTAdapter> DBTManager::addAdapter(const AdapterInfo& ai ) noexc
}
if( it.is_end() ) {
// new entry
- // private: std::shared_ptr<DBTAdapter> adapter = std::make_shared<DBTAdapter>(*this, ai);
- std::shared_ptr<DBTAdapter> adapter( new DBTAdapter(*this, ai) );
+ std::shared_ptr<DBTAdapter> adapter = DBTAdapter::make_shared(*this, ai);
it.push_back( adapter );
adapterIOCapability.push_back(defaultIOCapability);
DBG_PRINT("DBTManager::addAdapter: Adding new: %s", adapter->toString().c_str())