pvAccessCPP
client/pva/client.h
00001 /*
00002  * Copyright information and license terms for this software can be
00003  * found in the file LICENSE that is included with the distribution
00004  */
00005 #ifndef PVATESTCLIENT_H
00006 #define PVATESTCLIENT_H
00007 
00008 #include <ostream>
00009 #include <stdexcept>
00010 #include <list>
00011 
00012 #include <epicsMutex.h>
00013 
00014 #include <pv/pvData.h>
00015 #include <pv/bitSet.h>
00016 
00017 class epicsEvent;
00018 
00019 namespace epics {namespace pvAccess {
00020 class ChannelProvider;
00021 class Channel;
00022 class Monitor;
00023 class Configuration;
00024 }}//namespace epics::pvAccess
00025 
00027 namespace pvac {
00028 
00047 class ClientProvider;
00048 
00050 struct epicsShareClass Operation
00051 {
00052     struct Impl
00053     {
00054         virtual ~Impl() {}
00055         virtual std::string name() const =0;
00056         virtual void cancel() =0;
00057         virtual void show(std::ostream&) const =0;
00058     };
00059 
00060     Operation() {}
00061     Operation(const std::tr1::shared_ptr<Impl>&);
00062     ~Operation();
00064     std::string name() const;
00067     void cancel();
00068 
00069     bool valid() const { return !!impl; }
00070 
00071 #if __cplusplus>=201103L
00072     explicit operator bool() const { return valid(); }
00073 #else
00074 private:
00075     typedef bool (Operation::*bool_type)() const;
00076 public:
00077     operator bool_type() const { return valid() ? &Operation::valid : 0; }
00078 #endif
00079 
00080     void reset() { impl.reset(); }
00081 
00082 protected:
00083     friend epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const Operation& op);
00084     std::tr1::shared_ptr<Impl> impl;
00085 };
00086 
00088 struct PutEvent
00089 {
00090     enum event_t {
00091         Fail,    
00092         Cancel,  
00093         Success, 
00094     } event;
00095     std::string message;
00096     void *priv;
00097 };
00098 
00100 struct epicsShareClass GetEvent : public PutEvent
00101 {
00103     epics::pvData::PVStructure::const_shared_pointer value;
00104 };
00105 
00106 struct MonitorSync;
00107 
00109 struct epicsShareClass Monitor
00110 {
00111     struct Impl;
00112     Monitor() {}
00113     Monitor(const std::tr1::shared_ptr<Impl>&);
00114     ~Monitor();
00115 
00117     std::string name() const;
00120     void cancel();
00129     bool poll();
00132     bool complete() const;
00148     epics::pvData::PVStructure::const_shared_pointer root;
00149     epics::pvData::BitSet changed,
00150                           overrun;
00151 
00152     bool valid() const { return !!impl; }
00153 
00154 #if __cplusplus>=201103L
00155     explicit operator bool() const { return valid(); }
00156 #else
00157 private:
00158     typedef bool (Monitor::*bool_type)() const;
00159 public:
00160     operator bool_type() const { return valid() ? &Monitor::valid : 0; }
00161 #endif
00162 
00163     void reset() { impl.reset(); }
00164 
00165 private:
00166     std::tr1::shared_ptr<Impl> impl;
00167     friend epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const Monitor& op);
00168     friend struct MonitorSync;
00169 };
00170 
00172 struct MonitorEvent
00173 {
00174     enum event_t {
00175         Fail=1,      
00176         Cancel=2,    
00177         Disconnect=4,
00178         Data=8,      
00179     } event;
00180     std::string message; // set for event=Fail
00181 };
00182 
00190 struct epicsShareClass MonitorSync : public Monitor
00191 {
00192     struct SImpl;
00193     MonitorSync() {}
00194     MonitorSync(const Monitor&, const std::tr1::shared_ptr<SImpl>&);
00195     ~MonitorSync();
00196 
00198     bool wait();
00201     bool wait(double timeout);
00203     bool test();
00204 
00207     void wake();
00208 
00211     MonitorEvent event;
00212 private:
00213     std::tr1::shared_ptr<SImpl> simpl;
00214 };
00215 
00217 struct ConnectEvent
00218 {
00219     bool connected;
00220 };
00221 
00223 struct Timeout : public std::runtime_error
00224 {
00225     Timeout();
00226 };
00227 
00228 namespace detail {
00229 class PutBuilder;
00230 void registerRefTrack();
00231 }
00232 
00242 class epicsShareClass ClientChannel
00243 {
00244 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 305)
00245 public:
00246     // Impl is public only as a workaround on older GCC
00247 #endif
00248     struct Impl;
00249 private:
00250     std::tr1::shared_ptr<Impl> impl;
00251     friend class ClientProvider;
00252     friend void detail::registerRefTrack();
00253     friend epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const ClientChannel& op);
00254 
00255     ClientChannel(const std::tr1::shared_ptr<Impl>& i) :impl(i) {}
00256 public:
00258     struct epicsShareClass Options {
00259         short priority;
00260         std::string address;
00261         Options();
00262         bool operator<(const Options&) const;
00263     };
00264 
00266     ClientChannel() {}
00273     ClientChannel(const std::tr1::shared_ptr<epics::pvAccess::ChannelProvider>& provider,
00274                       const std::string& name,
00275                       const Options& opt = Options());
00276     ~ClientChannel();
00277 
00279     std::string name() const;
00280 
00281     bool valid() const { return !!impl; }
00282 
00283 #if __cplusplus>=201103L
00284     explicit operator bool() const { return valid(); }
00285 #else
00286 private:
00287     typedef bool (ClientChannel::*bool_type)() const;
00288 public:
00289     operator bool_type() const { return valid() ? &ClientChannel::valid : 0; }
00290 #endif
00291 
00292     void reset() { impl.reset(); }
00293 
00295     struct GetCallback {
00296         virtual ~GetCallback() {}
00298         virtual void getDone(const GetEvent& evt)=0;
00299     };
00300 
00304     Operation get(GetCallback* cb,
00305                       epics::pvData::PVStructure::const_shared_pointer pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00306 
00311     epics::pvData::PVStructure::const_shared_pointer
00312     get(double timeout = 3.0,
00313         epics::pvData::PVStructure::const_shared_pointer pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00314 
00315 
00320     Operation rpc(GetCallback* cb,
00321                       const epics::pvData::PVStructure::const_shared_pointer& arguments,
00322                       epics::pvData::PVStructure::const_shared_pointer pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00323 
00328     epics::pvData::PVStructure::const_shared_pointer
00329     rpc(double timeout,
00330         const epics::pvData::PVStructure::const_shared_pointer& arguments,
00331         epics::pvData::PVStructure::const_shared_pointer pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00332 
00334     struct PutCallback {
00335         virtual ~PutCallback() {}
00336         struct Args {
00337             Args(epics::pvData::BitSet& tosend) :tosend(tosend) {}
00338             epics::pvData::PVStructure::const_shared_pointer root;
00339             epics::pvData::BitSet& tosend;
00340         };
00348         virtual void putBuild(const epics::pvData::StructureConstPtr& build, Args& args) =0;
00350         virtual void putDone(const PutEvent& evt)=0;
00351     };
00352 
00356     Operation put(PutCallback* cb,
00357                       epics::pvData::PVStructure::const_shared_pointer pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00358 
00360     inline
00361     detail::PutBuilder put(const epics::pvData::PVStructure::const_shared_pointer &pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00362 
00364     struct MonitorCallback {
00365         virtual ~MonitorCallback() {}
00373         virtual void monitorEvent(const MonitorEvent& evt)=0;
00374     };
00375 
00378     Monitor monitor(MonitorCallback *cb,
00379                           epics::pvData::PVStructure::const_shared_pointer pvRequest = epics::pvData::PVStructure::const_shared_pointer());
00380 
00391     MonitorSync monitor(const epics::pvData::PVStructure::const_shared_pointer& pvRequest = epics::pvData::PVStructure::const_shared_pointer(),
00392                         epicsEvent *event =0);
00393 
00395     struct ConnectCallback {
00396         virtual ~ConnectCallback() {}
00397         virtual void connectEvent(const ConnectEvent& evt)=0;
00398     };
00401     void addConnectListener(ConnectCallback*);
00403     void removeConnectListener(ConnectCallback*);
00404 
00405     void show(std::ostream& strm) const;
00406 private:
00407     std::tr1::shared_ptr<epics::pvAccess::Channel> getChannel();
00408 };
00409 
00410 namespace detail {
00411 
00414 class epicsShareClass PutBuilder {
00415     ClientChannel& channel;
00416     epics::pvData::PVStructure::const_shared_pointer request;
00417 
00418     template<typename V>
00419     struct triple {
00420         std::string name;
00421         bool required;
00422         V value;
00423         triple(const std::string& name, const V& value, bool required =true)
00424             :name(name), required(required), value(value)
00425         {}
00426     };
00427 
00428     typedef std::list<triple<epics::pvData::AnyScalar> > scalars_t;
00429     scalars_t scalars;
00430 
00431     typedef std::list<triple<epics::pvData::shared_vector<const void> > > arrays_t;
00432     arrays_t arrays;
00433 
00434     struct Exec;
00435 
00436     friend class pvac::ClientChannel;
00437     PutBuilder(ClientChannel& channel, const epics::pvData::PVStructure::const_shared_pointer& request)
00438         :channel(channel), request(request)
00439     {}
00440 public:
00441     PutBuilder& set(const std::string& name, const epics::pvData::AnyScalar& value, bool required=true) {
00442         scalars.push_back(scalars_t::value_type(name, value, required));
00443         return *this;
00444     }
00445     template<typename T>
00446     PutBuilder& set(const std::string& name, T value, bool required=true) {
00447         return set(name, epics::pvData::AnyScalar(value), required);
00448     }
00449     PutBuilder& set(const std::string& name, const epics::pvData::shared_vector<const void>& value, bool required=true) {
00450         arrays.push_back(arrays_t::value_type(name, value, required));
00451         return *this;
00452     }
00453     template<typename T>
00454     PutBuilder& set(const std::string& name, const epics::pvData::shared_vector<const T>& value, bool required=true) {
00455         return set(name, epics::pvData::static_shared_vector_cast<const void>(value), required);
00456     }
00457     void exec(double timeout=3.0);
00458 };
00459 
00460 
00461 }// namespace detail
00462 
00464 class epicsShareClass ClientProvider
00465 {
00466     struct Impl;
00467     std::tr1::shared_ptr<Impl> impl;
00468     friend void detail::registerRefTrack();
00469     friend epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const ClientProvider& op);
00470 public:
00471 
00479     ClientProvider(const std::string& providerName,
00480                    const std::tr1::shared_ptr<epics::pvAccess::Configuration>& conf = std::tr1::shared_ptr<epics::pvAccess::Configuration>());
00481     explicit ClientProvider(const std::tr1::shared_ptr<epics::pvAccess::ChannelProvider>& provider);
00482     ~ClientProvider();
00483 
00484     std::string name() const;
00485 
00492     ClientChannel connect(const std::string& name,
00493                           const ClientChannel::Options& conf = ClientChannel::Options());
00494 
00496     bool disconnect(const std::string& name,
00497                     const ClientChannel::Options& conf = ClientChannel::Options());
00498 
00500     void disconnect();
00501 
00502     bool valid() const { return !!impl; }
00503 
00504 #if __cplusplus>=201103L
00505     explicit operator bool() const { return valid(); }
00506 #else
00507 private:
00508     typedef bool (ClientProvider::*bool_type)() const;
00509 public:
00510     operator bool_type() const { return valid() ? &ClientProvider::valid : 0; }
00511 #endif
00512 
00513     void reset() { impl.reset(); }
00514 };
00515 
00516 
00517 
00518 detail::PutBuilder
00519 ClientChannel::put(const epics::pvData::PVStructure::const_shared_pointer& pvRequest)
00520 {
00521     return detail::PutBuilder(*this, pvRequest);
00522 }
00523 
00524 epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const Operation& op);
00525 epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const Monitor& op);
00526 epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const ClientChannel& op);
00527 epicsShareFunc ::std::ostream& operator<<(::std::ostream& strm, const ClientProvider& op);
00528 
00530 
00531 }//namespace pvac
00532 
00533 #endif // PVATESTCLIENT_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator