]> git.lyx.org Git - lyx.git/commitdiff
Transform CoordCache::check/checkDim in ASSERT_DIM/POS macros
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 13 Sep 2024 13:41:53 +0000 (15:41 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 13 Sep 2024 13:44:33 +0000 (15:44 +0200)
This makes it more obvious to the reader that these are actually
assertions, and should help Coverity scan too.

src/CoordCache.cpp
src/CoordCache.h

index cf34875bb9394a12b20335160f9332479fae25b4..ce169fe8c26e20c49152e35ed59ce502ecfe44d7 100644 (file)
@@ -11,9 +11,7 @@
 
 #include "CoordCache.h"
 
-
 #include "support/debug.h"
-
 #include "support/lassert.h"
 
 
index fbe91e87c673fc6409cea027295dc2b7e40461f7..6ca58a9330fd0c012a31b537ba6d1a812e8bbe27 100644 (file)
@@ -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