From 9f9316ff81bc251db3bd74908c48e2e7073ed248 Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 5 Aug 2009 21:50:58 +0000 Subject: [PATCH] add __nonzero__, respect lockf() return value --- dtool/src/dtoolutil/filename.I | 17 +++++++++++++++++ dtool/src/dtoolutil/filename.cxx | 14 +++++++++++--- dtool/src/dtoolutil/filename.h | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dtool/src/dtoolutil/filename.I b/dtool/src/dtoolutil/filename.I index a12e8bc5cd..abff29fcfb 100644 --- a/dtool/src/dtoolutil/filename.I +++ b/dtool/src/dtoolutil/filename.I @@ -572,6 +572,23 @@ compare_to(const Filename &other) const { } +//////////////////////////////////////////////////////////////////// +// Function: Filename::__nonzero__ +// Access: Published +// Description: Returns true if the Filename is valid (not empty), +// or false if it is an empty string. +// +// This implements the Python equivalent to operator +// bool. Defining an actual operator bool method for +// C++ use would work too, but it seems to cause too +// many ambiguities for the C++ compiler, so we use this +// Python-only approach instead. +//////////////////////////////////////////////////////////////////// +INLINE bool Filename:: +__nonzero__() const { + return !_filename.empty(); +} + //////////////////////////////////////////////////////////////////// // Function: Filename::output // Access: Published diff --git a/dtool/src/dtoolutil/filename.cxx b/dtool/src/dtoolutil/filename.cxx index 0bbe1b92ba..d70c84f36e 100644 --- a/dtool/src/dtoolutil/filename.cxx +++ b/dtool/src/dtoolutil/filename.cxx @@ -1559,7 +1559,7 @@ get_file_size() const { bool Filename:: resolve_filename(const DSearchPath &searchpath, const string &default_extension) { - string found; + Filename found; if (is_local()) { found = searchpath.find_file(*this); @@ -2586,7 +2586,11 @@ atomic_compare_and_exchange_contents(string &orig_contents, orig_contents = string(); - lockf(fd, F_LOCK, 0); + if (lockf(fd, F_LOCK, 0) != 0) { + perror(os_specific.c_str()); + close(fd); + return false; + } size_t bytes_read = read(fd, buf, buf_size); while (bytes_read > 0) { @@ -2699,7 +2703,11 @@ atomic_read_contents(string &contents) const { contents = string(); - lockf(fd, F_LOCK, 0); + if (lockf(fd, F_LOCK, 0) != 0) { + perror(os_specific.c_str()); + close(fd); + return false; + } size_t bytes_read = read(fd, buf, buf_size); while (bytes_read > 0) { diff --git a/dtool/src/dtoolutil/filename.h b/dtool/src/dtoolutil/filename.h index 167164866d..097f48e2ea 100644 --- a/dtool/src/dtoolutil/filename.h +++ b/dtool/src/dtoolutil/filename.h @@ -204,6 +204,7 @@ PUBLISHED: INLINE bool operator != (const string &other) const; INLINE bool operator < (const string &other) const; INLINE int compare_to(const Filename &other) const; + INLINE bool __nonzero__() const; INLINE void output(ostream &out) const;