Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #36 from skalkoto/feature-constness
Browse files Browse the repository at this point in the history
Use const in the C++ interface wherever this is possible
  • Loading branch information
attie authored Jun 12, 2018
2 parents 12758fb + 279352f commit 5bfdb93
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 102 deletions.
4 changes: 2 additions & 2 deletions include/xbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ EXPORT void xbee_fini(void);
typedef void (*xbee_t_eofCallback)(struct xbee *xbee, void *rxInfo);

EXPORT void xbee_freeMemory(void *ptr); /* <-- this is for STUPID windows */
EXPORT xbee_err xbee_validate(struct xbee *xbee);
EXPORT xbee_err xbee_validate(const struct xbee *xbee);
EXPORT xbee_err xbee_setup(struct xbee **retXbee, const char *mode, ...);
EXPORT xbee_err xbee_vsetup(struct xbee **retXbee, const char *mode, va_list ap);
EXPORT xbee_err xbee_attachEOFCallback(struct xbee *xbee, xbee_t_eofCallback eofCallback);
Expand All @@ -232,7 +232,7 @@ EXPORT xbee_err xbee_dataGet(struct xbee *xbee, void **curData);
/* ######################################################################### */
/* --- mode.c --- */
EXPORT xbee_err xbee_modeGetList(char ***retList);
EXPORT xbee_err xbee_modeGet(struct xbee *xbee, const char **mode);
EXPORT xbee_err xbee_modeGet(const struct xbee *xbee, const char **mode);


/* ######################################################################### */
Expand Down
93 changes: 49 additions & 44 deletions include/xbeep.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ namespace libxbee {

class EXPORT XBee {
public:
EXPORT explicit XBee(std::string mode);
EXPORT explicit XBee(std::string mode, std::string device, int baudrate);
EXPORT explicit XBee(std::string mode, va_list ap);
EXPORT explicit XBee(const std::string &mode);
EXPORT explicit XBee(const std::string &mode, const std::string &device, int baudrate);
EXPORT explicit XBee(const std::string &mode, va_list ap);
EXPORT ~XBee(void);

private:
Expand All @@ -67,30 +67,32 @@ namespace libxbee {

public:
EXPORT struct xbee *getHnd(void);
EXPORT const struct xbee *getHnd(void) const;
EXPORT void conRegister(Con *con);
EXPORT void conUnregister(Con *con);
EXPORT Con *conLocate(struct xbee_con *con);
EXPORT std::list<std::string> getConTypes(void);
EXPORT const Con *conLocate(const struct xbee_con *con) const;
EXPORT Con *conLocate(const struct xbee_con *con);
EXPORT std::list<std::string> getConTypes(void) const;

EXPORT std::string mode(void);
EXPORT std::string mode(void) const;

EXPORT void setLogTarget(FILE *f);
EXPORT void setLogLevel(int level);
EXPORT int getLogLevel(void);
EXPORT void setLogTarget(FILE *f) const;
EXPORT void setLogLevel(int level) const;
EXPORT int getLogLevel(void) const;
};

class EXPORT Con {
public:
EXPORT explicit Con(XBee &parent, std::string type, struct xbee_conAddress *address = NULL);
EXPORT explicit Con(XBee &parent, const std::string &type, struct xbee_conAddress *address = NULL);
EXPORT ~Con(void);

EXPORT unsigned char operator<< (std::string data);
EXPORT unsigned char operator<< (std::vector<unsigned char> data);
EXPORT unsigned char operator<< (std::vector<char> data);
EXPORT void operator>> (Pkt &pkt);
EXPORT void operator>> (std::string &data);
EXPORT void operator>> (std::vector<unsigned char> &data);
EXPORT void operator>> (std::vector<char> &data);
EXPORT unsigned char operator<< (const std::string &data) const;
EXPORT unsigned char operator<< (const std::vector<unsigned char> &data) const;
EXPORT unsigned char operator<< (const std::vector<char> &data) const;
EXPORT void operator>> (Pkt &pkt) const;
EXPORT void operator>> (std::string &data) const;
EXPORT void operator>> (std::vector<unsigned char> &data) const;
EXPORT void operator>> (std::vector<char> &data) const;

private:
friend class XBee;
Expand All @@ -102,17 +104,18 @@ namespace libxbee {
virtual void xbee_conCallback(Pkt **pkt);

public:
EXPORT const struct xbee_con *getHnd(void) const;
EXPORT struct xbee_con *getHnd(void);
EXPORT unsigned char Tx(std::string data);
EXPORT unsigned char Tx(std::vector<unsigned char> data);
EXPORT unsigned char Tx(std::vector<char> data);
EXPORT unsigned char Tx(const unsigned char *buf, int len);
EXPORT unsigned char Tx(unsigned char *frameId, std::string data);
EXPORT unsigned char Tx(unsigned char *frameId, std::vector<unsigned char> data);
EXPORT unsigned char Tx(unsigned char *frameId, std::vector<char> data);
EXPORT unsigned char Tx(unsigned char *frameId, const unsigned char *buf, int len);
EXPORT void Rx(Pkt &pkt, int *remainingPackets = NULL);
EXPORT int RxAvailable(void);
EXPORT unsigned char Tx(const std::string &data) const;
EXPORT unsigned char Tx(const std::vector<unsigned char> &data) const;
EXPORT unsigned char Tx(const std::vector<char> &data) const;
EXPORT unsigned char Tx(const unsigned char *buf, int len) const;
EXPORT unsigned char Tx(unsigned char *frameId, const std::string &data) const;
EXPORT unsigned char Tx(unsigned char *frameId, const std::vector<unsigned char> &data) const;
EXPORT unsigned char Tx(unsigned char *frameId, const std::vector<char> &data) const;
EXPORT unsigned char Tx(unsigned char *frameId, const unsigned char *buf, int len) const;
EXPORT void Rx(Pkt &pkt, int *remainingPackets = NULL) const;
EXPORT int RxAvailable(void) const;

EXPORT void purge(void);

Expand Down Expand Up @@ -142,39 +145,41 @@ namespace libxbee {
EXPORT ~Pkt(void);

EXPORT unsigned char operator[] (int index);
EXPORT void operator<< (Con &con);
EXPORT void operator>> (std::string &data);
EXPORT void operator>> (std::vector<unsigned char> &data);
EXPORT void operator>> (std::vector<char> &data);
EXPORT void operator<< (const Con &con);
EXPORT void operator>> (std::string &data) const;
EXPORT void operator>> (std::vector<unsigned char> &data) const;
EXPORT void operator>> (std::vector<char> &data) const;

private:
struct xbee_pkt *pkt;

public:
EXPORT const struct xbee_pkt *getHnd(void) const;
EXPORT struct xbee_pkt *getHnd(void);
EXPORT void setHnd(struct xbee_pkt *pkt);

/* when calling this function, YOU become responsible for freeing the previously held packet */
EXPORT struct xbee_pkt *dropHnd(void);

EXPORT int size(void);
EXPORT int size(void) const;

EXPORT std::string getData(void);
EXPORT std::vector<unsigned char> getVector(void);
EXPORT std::vector<char> getVector2(void);
EXPORT std::string getData(void) const;
EXPORT std::vector<unsigned char> getVector(void) const;
EXPORT std::vector<char> getVector2(void) const;

/* use these three with care... */
EXPORT void *getData(const char *key);
EXPORT void *getData(const char *key, int id);
EXPORT void *getData(const char *key, int id, int index);
EXPORT void *getData(const char *key) const ;
EXPORT void *getData(const char *key, int id) const;
EXPORT void *getData(const char *key, int id, int index) const;

EXPORT std::string getATCommand(void);
EXPORT std::string getATCommand(void) const;

EXPORT int getAnalog(int channel);
EXPORT int getAnalog(int channel, int index);
EXPORT bool getDigital(int channel);
EXPORT bool getDigital(int channel, int index);
EXPORT int getAnalog(int channel) const;
EXPORT int getAnalog(int channel, int index) const;
EXPORT bool getDigital(int channel) const;
EXPORT bool getDigital(int channel, int index) const;

EXPORT unsigned char getRssi(void);
EXPORT unsigned char getRssi(void) const;
};
};

Expand Down
6 changes: 3 additions & 3 deletions ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct xbee_ll_info {
void *item;
};

xbee_err __xbee_ll_get_item(void *list, void *item, struct xbee_ll_info **retItem, int needMutex);
xbee_err __xbee_ll_get_item(void *list, const void *item, struct xbee_ll_info **retItem, int needMutex);

/* this file is scary, sorry it isn't commented... i nearly broke myself writing it
maybe oneday soon i'll be brave and put some commends down */
Expand Down Expand Up @@ -284,7 +284,7 @@ xbee_err _xbee_ll_get_tail(void *list, void **retItem, int needMutex) {
}

/* returns struct xbee_ll_info* or NULL - don't touch the pointer if you don't know what you're doing ;) */
xbee_err __xbee_ll_get_item(void *list, void *item, struct xbee_ll_info **retItem, int needMutex) {
xbee_err __xbee_ll_get_item(void *list, const void *item, struct xbee_ll_info **retItem, int needMutex) {
struct xbee_ll_head *h;
struct xbee_ll_info *i;
if (!list) return XBEE_EMISSINGPARAM;
Expand All @@ -302,7 +302,7 @@ xbee_err __xbee_ll_get_item(void *list, void *item, struct xbee_ll_info **retIte
if (!i) return XBEE_ENOTEXISTS;
return XBEE_ENONE;
}
xbee_err _xbee_ll_get_item(void *list, void *item, int needMutex) {
xbee_err _xbee_ll_get_item(void *list, const void *item, int needMutex) {
return __xbee_ll_get_item(list, item, NULL, needMutex);
}

Expand Down
2 changes: 1 addition & 1 deletion ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ xbee_err _xbee_ll_add_before(void *list, void *ref, void *item, int needMutex);

xbee_err _xbee_ll_get_head(void *list, void **retItem, int needMutex);
xbee_err _xbee_ll_get_tail(void *list, void **retItem, int needMutex);
xbee_err _xbee_ll_get_item(void *list, void *item, int needMutex);
xbee_err _xbee_ll_get_item(void *list, const void *item, int needMutex);
xbee_err _xbee_ll_get_next(void *list, void *ref, void **retItem, int needMutex);
xbee_err _xbee_ll_get_prev(void *list, void *ref, void **retItem, int needMutex);
xbee_err _xbee_ll_get_index(void *list, unsigned int index, void **retItem, int needMutex);
Expand Down
2 changes: 1 addition & 1 deletion mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ EXPORT xbee_err xbee_modeGetList(char ***retList) {
return XBEE_ENONE;
}

EXPORT xbee_err xbee_modeGet(struct xbee *xbee, const char **mode) {
EXPORT xbee_err xbee_modeGet(const struct xbee *xbee, const char **mode) {
if (!xbee || !mode) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
if (xbee_validate(xbee) != XBEE_ENONE) return XBEE_EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion xbee.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ EXPORT void xbee_freeMemory(void *ptr) {

/* ######################################################################### */

EXPORT xbee_err xbee_validate(struct xbee *xbee) {
EXPORT xbee_err xbee_validate(const struct xbee *xbee) {
if (xbee_ll_get_item(xbeeList, xbee) != XBEE_ENONE) return XBEE_EINVAL;
return XBEE_ENONE;
}
Expand Down
Loading

0 comments on commit 5bfdb93

Please sign in to comment.