YARP Cleanup/const

From Wiki for iCub and Friends
Jump to: navigation, search

YARP Cleanup - const

The suggested changes are of 3 types:

const methods
harmless for API and usually for implementation:
  • Class methods that should not modify any class member.
  • This allows to call these methods inside other const methods.
const reference parameters
dangerous for API (derived classes) and usually harmless for implementation, if the methods called inside those are all made const:
  • Parameters that should not be modified inside the method
  • Compared to passing parameters by value it does not make a copy of the parameter.
  • Compared to passing a reference it ensures that when the method returns the value is not modified.
const reference return type
tricky for API (require to change references either to const, or to use the copy operator) and harmless for implementation
Some methods return a reference to a class member, that can be modified outside the class. If there are no reason to let the user modify these member, the return value should be const to enforce this.
  • Methods that return a reference to a member that should not be modified from outside the class.
  • Compared to returning a copy of the member, it allows to avoid the copy.
  • Compared to returning a reference, it ensures that it is not modified.

For YARP 2.4 we could choose either to mark the old methods as deprecated and add the new ones, or to replace the old ones with the new ones. The advantage of the first approach is that we get a better compatibility with the old code, but the main disadvantage is that, if we aim at ABI stability, we will have those around for a long time, so I'd suggest to break the API now.


yarp::os

yarp::os::Bottle

-    const char *toBinary(size_t *size);
+    const char *toBinary(size_t *size) const;
-    virtual bool check(const char *key);
+    virtual bool check(const char *key) const;
-    virtual Value& find(const char *key);
+    virtual const Value& find(const char *key) const;
-    Bottle& findGroup(const char *key);
+    virtual const Bottle& findGroup(const char *key) const;
private:
-    Value& findGroupBit(const char *key);
+    Value& findGroupBit(const char *key) const;
-    Value& findBit(const char *key);
+    Value& findBit(const char *key) const;
-    bool isInt(int index);
+    bool isInt(int index) const;
-    bool isDouble(int index);
+    bool isDouble(int index) const;
-    bool isString(int index);
+    bool isString(int index) const;
-    bool isList(int index);
+    bool isList(int index) const;
-    virtual bool isList();
+    virtual bool isList() const;


yarp::os::IConfig

-    virtual bool open(Searchable& config);
+    virtual bool open(const Searchable& config);
-    virtual bool configure(Searchable& config);
+    virtual bool configure(const Searchable& config);


yarp::os::Property

-    bool check(const char *key);
+    bool check(const char *key) const;
-    virtual Value& find(const char *key);
+    virtual const Value& find(const char *key) const;
-    virtual Bottle& findGroup(const char *key);
+    virtual const Bottle& findGroup(const char *key) const;
     bool fromConfigFile(const char *fname,
-                        Searchable& env,
+                        const Searchable &env,
                         bool wipe=true);
    void fromConfig(const char *txt,
-                   Searchable& env,
+                   const Searchable &env,
                    bool wipe=true);


yarp::os::ResourceFinder

-    yarp::os::ConstString findFile(const char *key);
+    yarp::os::ConstString findFile(const char *key) const;
-    yarp::os::ConstString findPath(const char *key);
+    yarp::os::ConstString findPath(const char *key) const;
-    yarp::os::ConstString findPath();
+    yarp::os::ConstString findPath() const;
-    yarp::os::ConstString getContext();
+    yarp::os::ConstString getContext() const;
-    yarp::os::ConstString getContextPath();
+    yarp::os::ConstString getContextPath() const;
-    yarp::os::Bottle getContexts();
+    yarp::os::Bottle getContexts() const;
-    virtual bool check(const char *key);
+    virtual bool check(const char *key) const;
-    virtual Value& find(const char *key);
+    virtual const Value& find(const char *key) const;
-    virtual Bottle& findGroup(const char *key);
+    virtual const Bottle& findGroup(const char *key) const;
-    virtual ResourceFinder findNestedResourceFinder(const char *key);
+    virtual ResourceFinder findNestedResourceFinder(const char *key) const;
-    ResourceFinder(Searchable& data, void *implementation);
+    ResourceFinder(const Searchable& data, void *implementation);


yarp::os::Searchable

-    virtual bool check(const char *key) = 0;
+    virtual bool check(const char *key) const = 0;
     virtual bool check(const char *key,
-                       const char *comment);
+                       const char *comment) const;
-    virtual Value& find(const char *key) = 0;
+    virtual const Value& find(const char *key) const = 0;

     If you think that allowing to modify a Value inside a Searchable might be useful, we should have both --Daniele.Domenichelli@iit.it 17:09, 19 December 2012 (CET)


-    virtual Bottle& findGroup(const char *key) = 0;
+    virtual const Bottle& findGroup(const char *key) const = 0;

     If you think that allowing to modify a Bottle inside a Searchable might be useful, we should have both --Daniele.Domenichelli@iit.it 17:09, 19 December 2012 (CET)


-    Bottle& findGroup(const char *key, const char *comment);
+    const Bottle& findGroup(const char *key, const char *comment) const;

     If you think that allowing to modify a Bottle inside a Searchable might be useful, we should have both --Daniele.Domenichelli@iit.it 17:09, 19 December 2012 (CET)


-    virtual bool check(const char *key, Value *& result,
-                       const char *comment = 0 /*NULL*/);
+    virtual bool check(const char *key, const Value *& result,
+                       const char *comment = 0 /*NULL*/) const;
     virtual Value check(const char *key, const Value& fallback,
-                        const char *comment = 0 /*NULL*/);
+                        const char *comment = 0 /*NULL*/) const;
-    virtual SearchMonitor *getMonitor();
+    virtual SearchMonitor *getMonitor() const;
-    virtual ConstString getContext();
+    virtual ConstString getContext() const;
-    virtual void reportToMonitor(const SearchReport& report);
+    virtual void reportToMonitor(const SearchReport& report) const;

     Not sure about this one, but it is used by FindGroup --Daniele.Domenichelli@iit.it 17:09, 19 December 2012 (CET)

yarp::os::Value

-    virtual bool check(const char *key);
+    virtual bool check(const char *key) const;
-    virtual Value& find(const char *key);
+    virtual const Value& find(const char *key) const;
-    virtual Bottle& findGroup(const char *key);
+    virtual const Bottle& findGroup(const char *key) const;
-    virtual yarp::os::Value& find(const char *key);
+    virtual const yarp::os::Value& find(const char *key) const;
-    virtual yarp::os::Bottle& findGroup(const char *key);
+    virtual const yarp::os::Bottle& findGroup(const char *key) const;


yarp::dev

yarp::dev::DeviceDriver

-    virtual bool open(yarp::os::Searchable& config);
+    virtual bool open(const yarp::os::Searchable& config);


yarp::dev::DeviceGroup

-    virtual bool open(yarp::os::Searchable& config);
+    virtual bool open(const yarp::os::Searchable& config);
-    bool open(const char *key, PolyDriver& poly,
-              yarp::os::Searchable& config, const char *comment);
+    bool open(const char *key,
+              PolyDriver& poly,
+              const yarp::os::Searchable& config,
+              const char *comment);


yarp::dev::DevicePipe

-    virtual bool open(yarp::os::Searchable& config);
+    virtual bool open(const yarp::os::Searchable& config);

     bool open(const char *key, PolyDriver& poly,
-              yarp::os::Searchable& config, const char *comment);
+              const yarp::os::Searchable& config, const char *comment);


yarp::dev::Drivers

-    virtual DeviceDriver *open(yarp::os::Searchable& config);
+    virtual DeviceDriver *open(const yarp::os::Searchable& config);


yarp::dev::PolyDriver

-    PolyDriver(yarp::os::Searchable& config);
+    PolyDriver(const yarp::os::Searchable& config);
-    bool open(yarp::os::Searchable& config);
+    bool open(const yarp::os::Searchable& config);
     This will probably require to make it const to _all_ the PolyDrivers --Daniele.Domenichelli@iit.it 17:09, 19 December 2012 (CET)
private
-    bool coreOpen(yarp::os::Searchable& config);
+    bool coreOpen(const yarp::os::Searchable& config);