Files
pvxs/ioc/subscriptionctx.h
T
Michael Davidsaver 6d1216daad pvalink: porting part 3
add pvalink json schema
avoid JSON5 in testpvalink for portability.
fixup build with pvalink
trap bad_weak_ptr open during dtor
  Not sure why this is happening, but need not be CRIT.
c++11, cleanup, and notes
fix pvalink test sync
fix test cleanup on exit
pvalink disconnected link is always INVALID
pvalink logging
pvalink capture Disconnect time
pvalink eliminate providerName
  restrict local to dbChannelTest()
  aka. no qsrv groups
pvalink onTypeChange when attaching link to existing channel
pvalink eliminate unused Connecting state
pvalink add InstCounter
pvalink AfterPut can be const
pvalink add atomic jlif flag
include epicsStdio.h later
  avoid #define printf troubles
assert cleanup state on exit
pvalink add newer lset functions
test link disconnect
testpvalink redo testPutAsync()
pvalink fill out meta-data fetch
pvalink fix FLNK
pvalink cache putReq
pvalink test atomic monitor
pvalink test enum handling
pvalink handle scalar read of empty array
  make it well defined anyway...
pvalink test array of strings
handle db_add_event() failure
handle record._options.DBE
2023-11-20 10:59:44 -08:00

84 lines
2.3 KiB
C++

/*
* Copyright - See the COPYRIGHT that is included with this distribution.
* pvxs is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*
* Author George S. McIntyre <george@level-n.com>, 2023
*
*/
#ifndef PVXS_SUBSCRIPTIONCTX_H
#define PVXS_SUBSCRIPTIONCTX_H
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <dbEvent.h>
#include "channel.h"
#include "dbeventcontextdeleter.h"
namespace pvxs {
namespace ioc {
class Subscription {
std::shared_ptr<std::remove_pointer<dbEventSubscription>::type> sub; // holds void* returned by db_add_event()
public:
/* Add a subscription event by calling db_add_event using the given subscriptionCtx
* and selecting the correct elements based on the given type of event being added.
* You need to specify the correct options that correspond to the event type.
* Adds a deleter to clean up the subscription by calling db_cancel_event.
*/
void subscribe(dbEventCtx context,
const Channel& pChan,
EVENTFUNC *user_sub, void *user_arg, unsigned select)
{
auto chan(pChan); // bind by value
sub.reset(db_add_event(context, chan,
user_sub, user_arg, select),
[chan](dbEventSubscription sub) mutable
{
if(sub)
db_cancel_event(sub);
chan = Channel(); // dbChannel* must outlive subscription
});
if(!sub)
throw std::runtime_error("Failed to create db subscription");
}
void cancel() {
sub.reset();
}
void enable() {
if(sub) {
db_event_enable(sub.get());
db_post_single_event(sub.get());
}
}
void disable() {
if(sub)
db_event_disable(sub.get());
}
};
/**
* A subscription context
*/
class SubscriptionCtx {
public:
// For locking access to subscription context
Subscription pValueEventSubscription;
Subscription pPropertiesEventSubscription;
bool hadValueEvent = false;
bool hadPropertyEvent = false;
void cancel() {
pValueEventSubscription.cancel();
pPropertiesEventSubscription.cancel();
}
};
} // ioc
} // pvxs
#endif //PVXS_SUBSCRIPTIONCTX_H