diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2011-02-02 14:54:21 -0500 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2011-02-02 14:54:21 -0500 |
commit | 0113c042a1eb3080eb4e1b73c0db8234250d996a (patch) | |
tree | b400a73552603830d8c6b5e6c790e56d3edaa8b4 | |
parent | 0a1b13f791a6fa0965207deb44c3c67ff3deaf23 (diff) | |
download | gpsd-0113c042a1eb3080eb4e1b73c0db8234250d996a.tar.gz |
Rebuild the C++ interface to use RAII.
Simplifies the code and invalidates Berlios ug #17778: crash in libgpsmm.
-rw-r--r-- | libgpsmm.cpp | 16 | ||||
-rw-r--r-- | libgpsmm.h | 14 | ||||
-rw-r--r-- | test_gpsmm.cpp | 11 | ||||
-rw-r--r-- | www/protocol-transition.txt | 44 |
4 files changed, 51 insertions, 34 deletions
diff --git a/libgpsmm.cpp b/libgpsmm.cpp index 9e3d050b..f7d2354b 100644 --- a/libgpsmm.cpp +++ b/libgpsmm.cpp @@ -11,16 +11,7 @@ #include "libgpsmm.h" -gpsmm::gpsmm() : to_user(0) -{ -} - -struct gps_data_t* gpsmm::open(void) -{ - return open("localhost",DEFAULT_GPSD_PORT); -} - -struct gps_data_t* gpsmm::open(const char *host, const char *port) +struct gps_data_t* gpsmm::gps_inner_open(const char *host, const char *port) { const bool err = (gps_open(host, port, gps_data()) != 0); if ( err ) { @@ -65,11 +56,6 @@ struct gps_data_t* gpsmm::read(void) } } -int gpsmm::close(void) -{ - return gps_close(gps_data()); -} - bool gpsmm::waiting(void) { return gps_waiting(gps_data()); @@ -18,10 +18,16 @@ class gpsmm { class LIBQGPSMMSHARED_EXPORT gpsmm { #endif public: - gpsmm(); + gpsmm(const char *host, const char *port) : to_user(0) { + gps_inner_open(host, port); + } +#ifdef __UNUSED__ + gpsmm(void) : to_user(0) + { + gps_inner_open("localhost", DEFAULT_GPSD_PORT); + } +#endif virtual ~gpsmm(); - struct gps_data_t* open(const char *host,const char *port); //opens the connection with gpsd, MUST call this before any other method - struct gps_data_t* open(void); //open() with default values struct gps_data_t* send(const char *request); //put a command to gpsd and return the updated struct struct gps_data_t* stream(int); //set watcher and policy flags struct gps_data_t* read(void); //block until gpsd returns new data, then return the updated struct @@ -32,6 +38,8 @@ class LIBQGPSMMSHARED_EXPORT gpsmm { private: struct gps_data_t *to_user; //we return the user a copy of the internal structure. This way she can modify it without //integrity loss for the entire class + struct gps_data_t* gps_inner_open(const char *host, + const char *port); struct gps_data_t _gps_data; struct gps_data_t * gps_data() { return &_gps_data; } struct gps_data_t* backup(void) { *to_user=*gps_data(); return to_user;}; //return the backup copy diff --git a/test_gpsmm.cpp b/test_gpsmm.cpp index 2343d262..2731655e 100644 --- a/test_gpsmm.cpp +++ b/test_gpsmm.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005 Alfredo Pironti + * Copyright (C) 2010 Eric S. Raymond. * * This software is distributed under a BSD-style license. See the * file "COPYING" in the top-level directory of the distribution for details. @@ -96,14 +96,7 @@ static void libgps_dump_state(struct gps_data_t *collect) int main(void) { - gpsmm gps_rec; - struct gps_data_t *resp; - - resp = gps_rec.open(); - if (resp==NULL) { - cout << "Error opening gpsd\n"; - return (1); - } + gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT); gps_rec.stream(WATCH_ENABLE|WATCH_JSON); diff --git a/www/protocol-transition.txt b/www/protocol-transition.txt index 927a7f05..bbe920ce 100644 --- a/www/protocol-transition.txt +++ b/www/protocol-transition.txt @@ -403,17 +403,47 @@ of the gpsdata structure. Two new DOPs, xdop and ydop, are available; these express dilution of precision in longitude and latitude, respectively. -The raw_hook member is gone. +There is a gps_waiting() method analogous to the waiting() method in +the Python class -- a non-blocking way to check if input is waiting +from the daemon. -The 2.91 API includes a gps_waiting() method analogous to the -waiting() method in the Python class -- a non-blocking way to check -if input is waiting from the daemon. +The raw_hook member is gone. == C++ client library changes == -The C++ client library continues to be a trivial class wrpapper around -the C. About all you need to know is that the query() method is gone -and there is a new stream() method that calls gps_stream(). +In API version 5, the C++ library defines a single object using RAII. +There are no explicit open() and close() methods; instead, you initialize +the object handing it a host and server, and the connection is shut down +when the object is deleted or goes out of scope. + +The following code skeleton implements a C++ client: + +---------------------------------------------------------------------- + +int main(void) +{ + gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT); + + gps_rec.stream(WATCH_ENABLE|WATCH_JSON); + + for (;;) { + struct gps_data_t* newdata; + + if (!gps_rec.waiting()) + continue; + + if ((newdata = gps_rec.read()) == NULL) { + cerr << "Read error.\n"; + return 1; + } else { + PROCESS(newdata); + } + } + return 0; +} + +---------------------------------------------------------------------- + == Python client library changes == |