浏览代码

add __nonzero__, respect lockf() return value

David Rose 16 年之前
父节点
当前提交
9f9316ff81
共有 3 个文件被更改,包括 29 次插入3 次删除
  1. 17 0
      dtool/src/dtoolutil/filename.I
  2. 11 3
      dtool/src/dtoolutil/filename.cxx
  3. 1 0
      dtool/src/dtoolutil/filename.h

+ 17 - 0
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

+ 11 - 3
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) {

+ 1 - 0
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;