]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp
C++: Make most members private instead of protected
[libsigrok.git] / bindings / cxx / include / libsigrokcxx / libsigrokcxx.hpp
index b000e4cddf9a002063be3511b218afa19b96f057..a83600aa99753d9b4a5baf83ce95a4c4b7de5eaa 100644 (file)
@@ -120,16 +120,27 @@ class SR_API UserDevice;
 class SR_API Error: public exception
 {
 public:
-       Error(int result);
-       ~Error() throw();
+       explicit Error(int result);
+       ~Error() noexcept;
        const int result;
-       const char *what() const throw();
+       const char *what() const noexcept;
 };
 
 /* Base template for classes whose resources are owned by a parent object. */
 template <class Class, class Parent, typename Struct>
 class SR_API ParentOwned
 {
+private:
+       /* Weak pointer for shared_from_this() implementation. */
+       weak_ptr<Class> _weak_this;
+
+       static void reset_parent(Class *object)
+       {
+               if (!object->_parent)
+                       throw Error(SR_ERR_BUG);
+               object->_parent.reset();
+       }
+
 protected:
        /*  Parent object which owns this child object's underlying structure.
 
@@ -146,8 +157,12 @@ protected:
                references to both the parent and all its children are gone. */
        shared_ptr<Parent> _parent;
 
-       /* Weak pointer for shared_from_this() implementation. */
-       weak_ptr<Class> _weak_this;
+       Struct *_structure;
+
+       explicit ParentOwned(Struct *structure) :
+               _structure(structure)
+       {
+       }
 
 public:
        /* Get parent object that owns this object. */
@@ -163,7 +178,7 @@ public:
 
                if (!(shared = _weak_this.lock()))
                {
-                       shared = shared_ptr<Class>((Class *) this, reset_parent);
+                       shared = shared_ptr<Class>(static_cast<Class *>(this), reset_parent);
                        _weak_this = shared;
                }
 
@@ -184,20 +199,6 @@ public:
                        throw Error(SR_ERR_BUG);
                return get_shared_pointer(parent->shared_from_this());
        }
-protected:
-       static void reset_parent(Class *object)
-       {
-               if (!object->_parent)
-                       throw Error(SR_ERR_BUG);
-               object->_parent.reset();
-       }
-
-       Struct *_structure;
-
-       ParentOwned<Class, Parent, Struct>(Struct *structure) :
-               _structure(structure)
-       {
-       }
 };
 
 /* Base template for classes whose resources are owned by the user. */
@@ -215,7 +216,7 @@ public:
 protected:
        Struct *_structure;
 
-       UserOwned<Class, Struct>(Struct *structure) :
+       explicit UserOwned(Struct *structure) :
                _structure(structure)
        {
        }
@@ -246,11 +247,11 @@ private:
        virtual size_t read(const struct sr_resource *res, void *buf, size_t count) = 0;
 
        static SR_PRIV int open_callback(struct sr_resource *res,
-                       const char *name, void *cb_data);
+                       const char *name, void *cb_data) noexcept;
        static SR_PRIV int close_callback(struct sr_resource *res,
-                       void *cb_data);
+                       void *cb_data) noexcept;
        static SR_PRIV ssize_t read_callback(const struct sr_resource *res,
-                       void *buf, size_t count, void *cb_data);
+                       void *buf, size_t count, void *cb_data) noexcept;
        friend class Context;
 };
 
@@ -261,9 +262,9 @@ public:
        /** Create new context */
        static shared_ptr<Context> create();
        /** libsigrok package version. */
-       string package_version();
+       static string package_version();
        /** libsigrok library version. */
-       string lib_version();
+       static string lib_version();
        /** Available hardware drivers, indexed by name. */
        map<string, shared_ptr<Driver> > drivers();
        /** Available input formats, indexed by name. */
@@ -271,7 +272,7 @@ public:
        /** Available output formats, indexed by name. */
        map<string, shared_ptr<OutputFormat> > output_formats();
        /** Current log level. */
-       const LogLevel *log_level();
+       const LogLevel *log_level() const;
        /** Set the log level.
         * @param level LogLevel to use. */
        void set_log_level(const LogLevel *level);
@@ -292,15 +293,15 @@ public:
        shared_ptr<Packet> create_header_packet(Glib::TimeVal start_time);
        /** Create a meta packet. */
        shared_ptr<Packet> create_meta_packet(
-               map<const ConfigKey *, Glib::VariantBase> config);
+               const map<const ConfigKey *, Glib::VariantBase> &config);
        /** Create a logic packet. */
        shared_ptr<Packet> create_logic_packet(
                void *data_pointer, size_t data_length, unsigned int unit_size);
        /** Create an analog packet. */
        shared_ptr<Packet> create_analog_packet(
-               vector<shared_ptr<Channel> > channels,
+               const vector<shared_ptr<Channel> > &channels,
                float *data_pointer, unsigned int num_samples, const Quantity *mq,
-               const Unit *unit, vector<const QuantityFlag *> mqflags);
+               const Unit *unit, const vector<const QuantityFlag *> &mqflags);
        /** Load a saved session.
         * @param filename File name string. */
        shared_ptr<Session> load_session(string filename);
@@ -313,8 +314,8 @@ public:
        /** Open an input stream based on header data.
         * @param header Initial data from stream. */
        shared_ptr<Input> open_stream(string header);
-       map<string, string> serials(shared_ptr<Driver> driver);
-protected:
+       map<string, string> serials(shared_ptr<Driver> driver) const;
+private:
        map<string, Driver *> _drivers;
        map<string, InputFormat *> _input_formats;
        map<string, OutputFormat *> _output_formats;
@@ -339,18 +340,18 @@ class SR_API Configurable
 public:
        /** Read configuration for the given key.
         * @param key ConfigKey to read. */
-       Glib::VariantBase config_get(const ConfigKey *key);
+       Glib::VariantBase config_get(const ConfigKey *key) const;
        /** Set configuration for the given key to a specified value.
         * @param key ConfigKey to set.
         * @param value Value to set. */
-       void config_set(const ConfigKey *key, Glib::VariantBase value);
+       void config_set(const ConfigKey *key, const Glib::VariantBase &value);
        /** Enumerate available values for the given configuration key.
         * @param key ConfigKey to enumerate values for. */
-       Glib::VariantContainerBase config_list(const ConfigKey *key);
+       Glib::VariantContainerBase config_list(const ConfigKey *key) const;
        /** Enumerate available keys, according to a given index key. */
        map<const ConfigKey *, set<Capability> > config_keys(const ConfigKey *key);
        /** Check for a key in the list from a given index key. */
-       bool config_check(const ConfigKey *key, const ConfigKey *index_key);
+       bool config_check(const ConfigKey *key, const ConfigKey *index_key) const;
 protected:
        Configurable(
                struct sr_dev_driver *driver,
@@ -369,18 +370,17 @@ class SR_API Driver :
 {
 public:
        /** Name of this driver. */
-       string name();
+       string name() const;
        /** Long name for this driver. */
-       string long_name();
+       string long_name() const;
        /** Scan for devices and return a list of devices found.
         * @param options Mapping of (ConfigKey, value) pairs. */
        vector<shared_ptr<HardwareDevice> > scan(
-               map<const ConfigKey *, Glib::VariantBase> options =
-                       map<const ConfigKey *, Glib::VariantBase>());
-protected:
+               const map<const ConfigKey *, Glib::VariantBase> &options = {});
+private:
        bool _initialized;
        vector<HardwareDevice *> _devices;
-       Driver(struct sr_dev_driver *structure);
+       explicit Driver(struct sr_dev_driver *structure);
        ~Driver();
        friend class Context;
        friend class HardwareDevice;
@@ -392,15 +392,15 @@ class SR_API Device : public Configurable
 {
 public:
        /** Vendor name for this device. */
-       string vendor();
+       string vendor() const;
        /** Model name for this device. */
-       string model();
+       string model() const;
        /** Version string for this device. */
-       string version();
+       string version() const;
        /** Serial number for this device. */
-       string serial_number();
+       string serial_number() const;
        /** Connection ID for this device. */
-       string connection_id();
+       string connection_id() const;
        /** List of the channels available on this device. */
        vector<shared_ptr<Channel> > channels();
        /** Channel groups available on this device, indexed by name. */
@@ -410,12 +410,14 @@ public:
        /** Close device. */
        void close();
 protected:
-       Device(struct sr_dev_inst *structure);
+       explicit Device(struct sr_dev_inst *structure);
        ~Device();
        virtual shared_ptr<Device> get_shared_from_this() = 0;
        shared_ptr<Channel> get_channel(struct sr_channel *ptr);
+
        struct sr_dev_inst *_structure;
        map<struct sr_channel *, Channel *> _channels;
+private:
        map<string, ChannelGroup *> _channel_groups;
        /** Deleter needed to allow shared_ptr use with protected destructor. */
        class Deleter
@@ -439,7 +441,7 @@ class SR_API HardwareDevice :
 public:
        /** Driver providing this device. */
        shared_ptr<Driver> driver();
-protected:
+private:
        HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
        ~HardwareDevice();
        shared_ptr<Device> get_shared_from_this();
@@ -463,7 +465,7 @@ class SR_API UserDevice :
 public:
        /** Add a new channel to this device. */
        shared_ptr<Channel> add_channel(unsigned int index, const ChannelType *type, string name);
-protected:
+private:
        UserDevice(string vendor, string model, string version);
        ~UserDevice();
        shared_ptr<Device> get_shared_from_this();
@@ -483,21 +485,21 @@ class SR_API Channel :
 {
 public:
        /** Current name of this channel. */
-       string name();
+       string name() const;
        /** Set the name of this channel. *
         * @param name Name string to set. */
        void set_name(string name);
        /** Type of this channel. */
-       const ChannelType *type();
+       const ChannelType *type() const;
        /** Enabled status of this channel. */
-       bool enabled();
+       bool enabled() const;
        /** Set the enabled status of this channel.
         * @param value Boolean value to set. */
        void set_enabled(bool value);
        /** Get the index number of this channel. */
-       unsigned int index();
-protected:
-       Channel(struct sr_channel *structure);
+       unsigned int index() const;
+private:
+       explicit Channel(struct sr_channel *structure);
        ~Channel();
        const ChannelType * const _type;
        friend class Device;
@@ -515,10 +517,10 @@ class SR_API ChannelGroup :
 {
 public:
        /** Name of this channel group. */
-       string name();
+       string name() const;
        /** List of the channels in this group. */
        vector<shared_ptr<Channel> > channels();
-protected:
+private:
        ChannelGroup(Device *device, struct sr_channel_group *structure);
        ~ChannelGroup();
        vector<Channel *> _channels;
@@ -530,12 +532,12 @@ class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
 {
 public:
        /** Name of this trigger configuration. */
-       string name();
+       string name() const;
        /** List of the stages in this trigger. */
        vector<shared_ptr<TriggerStage> > stages();
        /** Add a new stage to this trigger. */
        shared_ptr<TriggerStage> add_stage();
-protected:
+private:
        Trigger(shared_ptr<Context> context, string name);
        ~Trigger();
        shared_ptr<Context> _context;
@@ -551,7 +553,7 @@ class SR_API TriggerStage :
 {
 public:
        /** Index number of this stage. */
-       int number();
+       int number() const;
        /** List of match conditions on this stage. */
        vector<shared_ptr<TriggerMatch> > matches();
        /** Add a new match condition to this stage.
@@ -563,9 +565,9 @@ public:
         * @param type TriggerMatchType to apply.
         * @param value Threshold value. */
        void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
-protected:
+private:
        vector<TriggerMatch *> _matches;
-       TriggerStage(struct sr_trigger_stage *structure);
+       explicit TriggerStage(struct sr_trigger_stage *structure);
        ~TriggerStage();
        friend class Trigger;
 };
@@ -578,10 +580,10 @@ public:
        /** Channel this condition matches on. */
        shared_ptr<Channel> channel();
        /** Type of match. */
-       const TriggerMatchType *type();
+       const TriggerMatchType *type() const;
        /** Threshold value. */
-       float value();
-protected:
+       float value() const;
+private:
        TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
        ~TriggerMatch();
        shared_ptr<Channel> _channel;
@@ -601,7 +603,7 @@ class SR_PRIV DatafeedCallbackData
 public:
        void run(const struct sr_dev_inst *sdi,
                const struct sr_datafeed_packet *pkt);
-protected:
+private:
        DatafeedCallbackFunction _callback;
        DatafeedCallbackData(Session *session,
                DatafeedCallbackFunction callback);
@@ -614,8 +616,8 @@ class SR_API SessionDevice :
        public ParentOwned<SessionDevice, Session, struct sr_dev_inst>,
        public Device
 {
-protected:
-       SessionDevice(struct sr_dev_inst *sdi);
+private:
+       explicit SessionDevice(struct sr_dev_inst *sdi);
        ~SessionDevice();
        shared_ptr<Device> get_shared_from_this();
        /** Deleter needed to allow shared_ptr use with protected destructor. */
@@ -662,9 +664,9 @@ public:
         * @param trigger Trigger object to use. */
        void set_trigger(shared_ptr<Trigger> trigger);
        /** Get filename this session was loaded from. */
-       string filename();
-protected:
-       Session(shared_ptr<Context> context);
+       string filename() const;
+private:
+       explicit Session(shared_ptr<Context> context);
        Session(shared_ptr<Context> context, string filename);
        ~Session();
        shared_ptr<Device> get_device(const struct sr_dev_inst *sdi);
@@ -686,10 +688,10 @@ class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
 {
 public:
        /** Type of this packet. */
-       const PacketType *type();
+       const PacketType *type() const;
        /** Payload of this packet. */
        shared_ptr<PacketPayload> payload();
-protected:
+private:
        Packet(shared_ptr<Device> device,
                const struct sr_datafeed_packet *structure);
        ~Packet();
@@ -713,12 +715,7 @@ protected:
        PacketPayload();
        virtual ~PacketPayload() = 0;
        virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
-       /** Deleter needed to allow shared_ptr use with protected destructor. */
-       class Deleter
-       {
-       public:
-               void operator()(PacketPayload *payload) { delete payload; }
-       };
+
        friend class Deleter;
        friend class Packet;
        friend class Output;
@@ -731,11 +728,11 @@ class SR_API Header :
 {
 public:
        /* Feed version number. */
-       int feed_version();
+       int feed_version() const;
        /* Start time of this session. */
-       Glib::TimeVal start_time();
-protected:
-       Header(const struct sr_datafeed_header *structure);
+       Glib::TimeVal start_time() const;
+private:
+       explicit Header(const struct sr_datafeed_header *structure);
        ~Header();
        shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
        friend class Packet;
@@ -748,9 +745,9 @@ class SR_API Meta :
 {
 public:
        /* Mapping of (ConfigKey, value) pairs. */
-       map<const ConfigKey *, Glib::VariantBase> config();
-protected:
-       Meta(const struct sr_datafeed_meta *structure);
+       map<const ConfigKey *, Glib::VariantBase> config() const;
+private:
+       explicit Meta(const struct sr_datafeed_meta *structure);
        ~Meta();
        shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
        map<const ConfigKey *, Glib::VariantBase> _config;
@@ -766,11 +763,11 @@ public:
        /* Pointer to data. */
        void *data_pointer();
        /* Data length in bytes. */
-       size_t data_length();
+       size_t data_length() const;
        /* Size of each sample in bytes. */
-       unsigned int unit_size();
-protected:
-       Logic(const struct sr_datafeed_logic *structure);
+       unsigned int unit_size() const;
+private:
+       explicit Logic(const struct sr_datafeed_logic *structure);
        ~Logic();
        shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
        friend class Packet;
@@ -783,19 +780,19 @@ class SR_API Analog :
 {
 public:
        /** Pointer to data. */
-       float *data_pointer();
+       void *data_pointer();
        /** Number of samples in this packet. */
-       unsigned int num_samples();
+       unsigned int num_samples() const;
        /** Channels for which this packet contains data. */
        vector<shared_ptr<Channel> > channels();
        /** Measured quantity of the samples in this packet. */
-       const Quantity *mq();
+       const Quantity *mq() const;
        /** Unit of the samples in this packet. */
-       const Unit *unit();
+       const Unit *unit() const;
        /** Measurement flags associated with the samples in this packet. */
-       vector<const QuantityFlag *> mq_flags();
-protected:
-       Analog(const struct sr_datafeed_analog *structure);
+       vector<const QuantityFlag *> mq_flags() const;
+private:
+       explicit Analog(const struct sr_datafeed_analog *structure);
        ~Analog();
        shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
        friend class Packet;
@@ -807,20 +804,19 @@ class SR_API InputFormat :
 {
 public:
        /** Name of this input format. */
-       string name();
+       string name() const;
        /** Description of this input format. */
-       string description();
+       string description() const;
        /** A list of preferred file name extensions for this file format.
          * @note This list is a recommendation only. */
-       vector<string> extensions();
+       vector<string> extensions() const;
        /** Options supported by this input format. */
        map<string, shared_ptr<Option> > options();
        /** Create an input using this input format.
         * @param options Mapping of (option name, value) pairs. */
-       shared_ptr<Input> create_input(map<string, Glib::VariantBase> options =
-               map<string, Glib::VariantBase>());
-protected:
-       InputFormat(const struct sr_input_module *structure);
+       shared_ptr<Input> create_input(const map<string, Glib::VariantBase> &options = {});
+private:
+       explicit InputFormat(const struct sr_input_module *structure);
        ~InputFormat();
        friend class Context;
        friend class InputDevice;
@@ -838,7 +834,7 @@ public:
        void send(void *data, size_t length);
        /** Signal end of input data. */
        void end();
-protected:
+private:
        Input(shared_ptr<Context> context, const struct sr_input *structure);
        ~Input();
        shared_ptr<Context> _context;
@@ -853,7 +849,7 @@ class SR_API InputDevice :
        public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
        public Device
 {
-protected:
+private:
        InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
        ~InputDevice();
        shared_ptr<Device> get_shared_from_this();
@@ -866,16 +862,16 @@ class SR_API Option : public UserOwned<Option, const struct sr_option>
 {
 public:
        /** Short name of this option suitable for command line usage. */
-       string id();
+       string id() const;
        /** Short name of this option suitable for GUI usage. */
-       string name();
+       string name() const;
        /** Description of this option in a sentence. */
-       string description();
+       string description() const;
        /** Default value for this option. */
-       Glib::VariantBase default_value();
+       Glib::VariantBase default_value() const;
        /** Possible values for this option, if a limited set. */
-       vector<Glib::VariantBase> values();
-protected:
+       vector<Glib::VariantBase> values() const;
+private:
        Option(const struct sr_option *structure,
                shared_ptr<const struct sr_option *> structure_array);
        ~Option();
@@ -891,12 +887,12 @@ class SR_API OutputFormat :
 {
 public:
        /** Name of this output format. */
-       string name();
+       string name() const;
        /** Description of this output format. */
-       string description();
+       string description() const;
        /** A list of preferred file name extensions for this file format.
          * @note This list is a recommendation only. */
-       vector<string> extensions();
+       vector<string> extensions() const;
        /** Options supported by this output format. */
        map<string, shared_ptr<Option> > options();
        /** Create an output using this format.
@@ -904,25 +900,23 @@ public:
         * @param options Mapping of (option name, value) pairs. */
        shared_ptr<Output> create_output(
                shared_ptr<Device> device,
-               map<string, Glib::VariantBase> options =
-                       map<string, Glib::VariantBase>());
+               const map<string, Glib::VariantBase> &options = {});
        /** Create an output using this format.
         * @param filename Name of destination file.
         * @param device Device to output for.
         * @param options Mapping of (option name, value) pairs. */
        shared_ptr<Output> create_output(string filename,
                shared_ptr<Device> device,
-               map<string, Glib::VariantBase> options =
-                       map<string, Glib::VariantBase>());
+               const map<string, Glib::VariantBase> &options = {});
        /**
         * Checks whether a given flag is set.
         * @param flag Flag to check
         * @return true if flag is set for this module
         * @see sr_output_flags
         */
-       bool test_flag(const OutputFlag *flag);
-protected:
-       OutputFormat(const struct sr_output_module *structure);
+       bool test_flag(const OutputFlag *flag) const;
+private:
+       explicit OutputFormat(const struct sr_output_module *structure);
        ~OutputFormat();
        friend class Context;
        friend class Output;
@@ -935,12 +929,12 @@ public:
        /** Update output with data from the given packet.
         * @param packet Packet to handle. */
        string receive(shared_ptr<Packet> packet);
-protected:
+private:
        Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
        Output(shared_ptr<OutputFormat> format,
-               shared_ptr<Device> device, map<string, Glib::VariantBase> options);
+               shared_ptr<Device> device, const map<string, Glib::VariantBase> &options);
        Output(string filename, shared_ptr<OutputFormat> format,
-               shared_ptr<Device> device, map<string, Glib::VariantBase> options);
+               shared_ptr<Device> device, const map<string, Glib::VariantBase> &options);
        ~Output();
        const shared_ptr<OutputFormat> _format;
        const shared_ptr<Device> _device;
@@ -966,10 +960,10 @@ public:
        /** Get value associated with a given integer constant. */
        static const Class *get(int id)
        {
-               auto key = static_cast<Enum>(id);
-               if (_values.find(key) == _values.end())
+               const auto pos = _values.find(static_cast<Enum>(id));
+               if (pos == _values.end())
                        throw Error(SR_ERR_ARG);
-               return _values.at(key);
+               return pos->second;
        }
        /** Get possible values. */
        static std::vector<const Class *> values()
@@ -986,6 +980,7 @@ protected:
        ~EnumValue()
        {
        }
+private:
        static const std::map<const Enum, const Class * const> _values;
        const Enum _id;
        const string _name;