[LyX/master] Transform CoordCache::check/checkDim in ASSERT_DIM/POS macros

Jean-Marc Lasgouttes lasgouttes at lyx.org
Fri Sep 13 13:45:43 UTC 2024


commit 375aba7b5d8404511553e4e2d6154476bd00fcf2
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Fri Sep 13 15:41:53 2024 +0200

    Transform CoordCache::check/checkDim in ASSERT_DIM/POS macros
    
    This makes it more obvious to the reader that these are actually
    assertions, and should help Coverity scan too.
---
 src/CoordCache.cpp |  2 --
 src/CoordCache.h   | 55 +++++++++++++++++++++++++++---------------------------
 2 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/src/CoordCache.cpp b/src/CoordCache.cpp
index cf34875bb9..ce169fe8c2 100644
--- a/src/CoordCache.cpp
+++ b/src/CoordCache.cpp
@@ -11,9 +11,7 @@
 
 #include "CoordCache.h"
 
-
 #include "support/debug.h"
-
 #include "support/lassert.h"
 
 
diff --git a/src/CoordCache.h b/src/CoordCache.h
index fbe91e87c6..6ca58a9330 100644
--- a/src/CoordCache.h
+++ b/src/CoordCache.h
@@ -11,9 +11,6 @@
 #ifndef COORDCACHE_H
 #define COORDCACHE_H
 
-// It seems that MacOSX define the check macro.
-#undef check
-
 #include "Dimension.h"
 
 #include <unordered_map>
@@ -23,6 +20,24 @@ namespace lyx {
 class Inset;
 class MathData;
 
+#ifdef ENABLE_ASSERTIONS
+#define ASSERT_HAS_DIM(thing, hint)			  \
+	if (data_.find(thing) != data_.end()) \
+	{} \
+	else \
+		lyxbreaker(thing, hint, data_.size());
+
+#define ASSERT_HAS_POS(thing, hint) \
+	auto it = data_.find(thing); \
+	if (it != data_.end() && it->second.pos.x != -10000) \
+	{} \
+	else \
+		lyxbreaker(thing, hint, data_.size());
+#else
+#define ASSERT_HAS_DIM(thing, hint)
+#define ASSERT_HAS_POS(thing, hint)
+#endif
+
 void lyxbreaker(void const * data, const char * hint, int size);
 
 struct Geometry {
@@ -94,37 +109,37 @@ public:
 
 	Geometry & geometry(T const * thing)
 	{
-		checkDim(thing, "geometry");
+		ASSERT_HAS_DIM(thing, "geometry");
 		return data_.find(thing)->second;
 	}
 
 	Geometry const & geometry(T const * thing) const
 	{
-		checkDim(thing, "geometry");
+		ASSERT_HAS_DIM(thing, "geometry");
 		return data_.find(thing)->second;
 	}
 
 	Dimension const & dim(T const * thing) const
 	{
-		checkDim(thing, "dim");
+		ASSERT_HAS_DIM(thing, "dim");
 		return data_.find(thing)->second.dim;
 	}
 
 	int x(T const * thing) const
 	{
-		check(thing, "x");
+		ASSERT_HAS_POS(thing, "x");
 		return data_.find(thing)->second.pos.x;
 	}
 
 	int y(T const * thing) const
 	{
-		check(thing, "y");
+		ASSERT_HAS_POS(thing, "y");
 		return data_.find(thing)->second.pos.y;
 	}
 
 	Point xy(T const * thing) const
 	{
-		check(thing, "xy");
+		ASSERT_HAS_POS(thing, "xy");
 		return data_.find(thing)->second.pos;
 	}
 
@@ -159,24 +174,6 @@ public:
 private:
 	friend class CoordCache;
 
-#ifdef ENABLE_ASSERTIONS
-	void checkDim(T const * thing, char const * hint) const
-	{
-		if (!hasDim(thing))
-			lyxbreaker(thing, hint, data_.size());
-	}
-
-	void check(T const * thing, char const * hint) const
-	{
-		if (!has(thing))
-			lyxbreaker(thing, hint, data_.size());
-	}
-#else
-	void checkDim(T const *, char const * const ) const {}
-
-	void check(T const *, char const *) const {}
-#endif
-
 	typedef std::unordered_map<T const *, Geometry> cache_type;
 	cache_type data_;
 };
@@ -213,12 +210,16 @@ public:
 	/// Dump the contents of the cache to lyxerr in debugging form
 	void dump() const;
 private:
+
 	/// MathDatas
 	Arrays arrays_;
 	// All insets
 	Insets insets_;
 };
 
+#undef ASSERT_HAS_DIM
+#undef ASSERT_HAS_POS
+
 } // namespace lyx
 
 #endif


More information about the lyx-cvs mailing list