Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:38

0001 /*************************************************************************
0002  * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers.               *
0003  * All rights reserved.                                                  *
0004  *                                                                       *
0005  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0006  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0007  *************************************************************************/
0008 
0009 #ifndef ROOT7_RDrawable
0010 #define ROOT7_RDrawable
0011 
0012 #include <memory>
0013 #include <string>
0014 #include <vector>
0015 
0016 #include <ROOT/RAttrMap.hxx>
0017 #include <ROOT/RStyle.hxx>
0018 
0019 namespace ROOT {
0020 namespace Experimental {
0021 
0022 class RMenuItems;
0023 class RPadBase;
0024 class RAttrBase;
0025 class RDisplayItem;
0026 class RDrawableDisplayItem;
0027 class RIndirectDisplayItem;
0028 class RLegend;
0029 class RCanvas;
0030 class RChangeAttrRequest;
0031 class RDrawableMenuRequest;
0032 class RDrawableExecRequest;
0033 
0034 namespace Internal {
0035 
0036 /** \class RIOSharedBase
0037 \ingroup GpadROOT7
0038 \author Sergey Linev <s.linev@gsi.de>
0039 \date 2019-09-24
0040 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
0041 */
0042 
0043 class RIOSharedBase {
0044 public:
0045    virtual const void *GetIOPtr() const = 0;
0046    virtual bool HasShared() const = 0;
0047    virtual void *MakeShared() = 0;
0048    virtual void SetShared(void *shared) = 0;
0049    virtual ~RIOSharedBase() = default;
0050 };
0051 
0052 using RIOSharedVector_t = std::vector<RIOSharedBase *>;
0053 
0054 template<class T>
0055 class RIOShared final : public RIOSharedBase {
0056    std::shared_ptr<T>  fShared;  ///<!   holder of object
0057    T* fIO{nullptr};              ///<    plain pointer for IO
0058 public:
0059    const void *GetIOPtr() const final { return fIO; }
0060    bool HasShared() const final { return fShared.get() != nullptr; }
0061    void *MakeShared() final { fShared.reset(fIO); return &fShared; }
0062    void SetShared(void *shared) final { fShared = *((std::shared_ptr<T> *) shared); }
0063 
0064    RIOShared() = default;
0065 
0066    RIOShared(const std::shared_ptr<T> &ptr) : RIOSharedBase()
0067    {
0068       fShared = ptr;
0069       fIO = ptr.get();
0070    }
0071 
0072    RIOShared &operator=(const std::shared_ptr<T> &ptr)
0073    {
0074       fShared = ptr;
0075       fIO = ptr.get();
0076       return *this;
0077    }
0078 
0079    operator bool() const { return !!fShared || !!fIO; }
0080 
0081    const T *get() const { return fShared ? fShared.get() : fIO; }
0082    T *get() { return fShared ? fShared.get() : fIO; }
0083 
0084    const T *operator->() const { return get(); }
0085    T *operator->() { return get(); }
0086 
0087    std::shared_ptr<T> get_shared() const { return fShared; }
0088 
0089    void reset() { fShared.reset(); fIO = nullptr; }
0090 
0091    // reset IO pointer, object will not be stored in normal output operation
0092    void reset_io() { fIO = nullptr; }
0093 
0094    // restore IO pointer, object will be stored in normal output operation
0095    void restore_io() { fIO = fShared.get(); }
0096 };
0097 
0098 } // namespace Internal
0099 
0100 /** \class RDrawable
0101 \ingroup GpadROOT7
0102 \brief Base class for drawable entities: objects that can be painted on a `RPad`.
0103 \authors Axel Naumann <axel@cern.ch>, Sergey Linev <s.linev@gsi.de>
0104 \date 2015-08-07
0105 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
0106 */
0107 
0108 class RDrawable {
0109 
0110 friend class RPadBase; // to access Display method and IsFrameRequired
0111 friend class RCanvas; // to access SetDrawableVersion
0112 friend class RAttrBase;
0113 friend class RStyle;
0114 friend class RLegend; // to access CollectShared method
0115 friend class RDrawableDisplayItem;  // to call OnDisplayItemDestroyed
0116 friend class RIndirectDisplayItem;  // to access attributes and other members
0117 friend class RChangeAttrRequest; // access SetDrawableVersion and AttrMap
0118 friend class RDrawableMenuRequest; // access PopulateMenu method
0119 friend class RDrawableExecRequest; // access Execute() method
0120 
0121 public:
0122 
0123    using Version_t = uint64_t;
0124 
0125    class RDisplayContext {
0126       RCanvas *fCanvas{nullptr};     ///<! canvas where drawable is displayed
0127       RPadBase *fPad{nullptr};       ///<! subpad where drawable is displayed
0128       RDrawable *fDrawable{nullptr}; ///<! reference on the drawable
0129       Version_t fLastVersion{0};     ///<! last displayed version
0130       unsigned fIndex{0};            ///<! index in list of primitives
0131       unsigned fConnId{0};           ///<! connection id
0132       bool fMainConn{false};         ///<! is main connection
0133 
0134    public:
0135 
0136       RDisplayContext() = default;
0137 
0138       RDisplayContext(RCanvas *canv, RPadBase *pad, Version_t vers = 0) :
0139          fCanvas(canv), fPad(pad), fLastVersion(vers)
0140       {
0141       }
0142 
0143       /** Set canvas */
0144       void SetCanvas(RCanvas *canv) { fCanvas = canv; }
0145       /** Set pad */
0146       void SetPad(RPadBase *pad) { fPad = pad; }
0147       /** Set drawable and its index in list of primitives */
0148       void SetDrawable(RDrawable *dr, unsigned indx)
0149       {
0150          fDrawable = dr;
0151          fIndex = indx;
0152       }
0153       /** Set connection id and ismain flag for connection */
0154       void SetConnection(unsigned connid, bool ismain)
0155       {
0156          fConnId = connid;
0157          fMainConn = ismain;
0158       }
0159 
0160       RCanvas *GetCanvas() const { return fCanvas; }
0161       RPadBase *GetPad() const { return fPad; }
0162       RDrawable *GetDrawable() const { return fDrawable; }
0163       unsigned GetIndex() const { return fIndex; }
0164 
0165       Version_t GetLastVersion() const { return fLastVersion; }
0166 
0167       unsigned GetConnId() const { return fConnId; }
0168       bool IsMainConn() const { return fMainConn; }
0169    };
0170 
0171 private:
0172    RAttrMap fAttr;                ///< attributes values
0173    std::weak_ptr<RStyle> fStyle;  ///<! style applied for RDrawable, not stored when canvas is saved
0174    const char *fCssType{nullptr}; ///<! drawable type, not stored in the root file, must be initialized in constructor
0175    std::string fCssClass;         ///< user-defined CSS class, used for RStyle
0176    std::string fId;               ///< user-defined CSS id, used for RStyle
0177    Version_t fVersion{1};         ///<! drawable version, changed from the canvas
0178 
0179 protected:
0180 
0181    virtual void CollectShared(Internal::RIOSharedVector_t &) {}
0182 
0183    virtual bool IsFrameRequired() const { return false; }
0184 
0185    RAttrMap &GetAttrMap() { return fAttr; }
0186    const RAttrMap &GetAttrMap() const { return fAttr; }
0187 
0188    bool MatchSelector(const std::string &selector) const;
0189 
0190    virtual std::unique_ptr<RDisplayItem> Display(const RDisplayContext &);
0191 
0192    void SetCssType(const char *csstype) { fCssType = csstype; }
0193 
0194    virtual void OnDisplayItemDestroyed(RDisplayItem *) const {}
0195 
0196    virtual void SetDrawableVersion(Version_t vers) { fVersion = vers; }
0197    Version_t GetVersion() const { return fVersion; }
0198 
0199    virtual void PopulateMenu(RMenuItems &);
0200 
0201    virtual void Execute(const std::string &);
0202 
0203    RDrawable(const RDrawable &) = delete;
0204    RDrawable &operator=(const RDrawable &) = delete;
0205 
0206 public:
0207 
0208    explicit RDrawable(const char *csstype) : fCssType(csstype) {}
0209 
0210    virtual ~RDrawable();
0211 
0212    virtual void UseStyle(const std::shared_ptr<RStyle> &style) { fStyle = style; }
0213    void ClearStyle() { UseStyle(nullptr); }
0214 
0215    const char *GetCssType() const { return fCssType; }
0216 
0217    void SetCssClass(const std::string &cl) { fCssClass = cl; }
0218    const std::string &GetCssClass() const { return fCssClass; }
0219 
0220    void SetId(const std::string &id) { fId = id; }
0221    const std::string &GetId() const { return fId; }
0222 };
0223 
0224 /// Central method to insert drawable in list of pad primitives
0225 /// By default drawable placed as is.
0226 
0227 template <class DRAWABLE, std::enable_if_t<std::is_base_of<RDrawable, DRAWABLE>{}>* = nullptr>
0228 inline auto GetDrawable(const std::shared_ptr<DRAWABLE> &drawable)
0229 {
0230    return drawable;
0231 }
0232 
0233 } // namespace Experimental
0234 } // namespace ROOT
0235 
0236 #endif