+///////////////////////////////////////
+// The lyxstring Invariant tester
+///////////////////////////////////////
+#ifdef DEVEL_VERSION
+
+/** Testing of the lyxstring invariant
+ * By creating an object that tests the lyxstring invariant during its
+ * construction *and* its deconstruction we greatly simplify our code.
+ * Calling TestlyxstringInvariant() upon entry to an lyxstring method
+ * will test the invariant upon entry to the code. If the Asserts fail
+ * then we know from the stack trace that the corruption occurred *before*
+ * entry to this method. We can also be sure it didn't happen in any of
+ * the tested lyxstring methods. It is therefore likely to be due to some
+ * other external force.
+ * Several lyxstring methods have multiple exit points which would otherwise
+ * require us to insert a separate test before each return. But since we
+ * created an object its destructor will be called upon exit (any exit!).
+ * We thus get testing at both start and end of a method with one line of
+ * code at the head of a method. More importantly, we get good testing
+ * everytime we run the code.
+ * NOTE: just because we test the invariant doesn't mean we can forget
+ * about testing pre and post conditions specific to any given method.
+ * This test simply proves that the lyxstring/Srep is in a valid state it
+ * does *not* prove that the method did what it was supposed to.
+ */
+class lyxstringInvariant {
+public:
+ lyxstringInvariant(lyxstring const *);
+ ~lyxstringInvariant();
+private:
+ void helper() const;
+ lyxstring const * object;
+};
+
+
+// To test if this scheme works "as advertised" uncomment the printf's in
+// the constructor and destructor below and then uncomment the printf and the
+// call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
+// The correct output when LyX has been recompiled and run is:
+// lyxstringInvariant constructor
+// lyxstring::operator=(char const *)
+// lyxstringInvariant constructor
+// lyxstringInvariant destructor completed
+// lyxstringInvariant destructor completed
+// NOTE: The easiest way to catch this snippet of the output is to wait for
+// the splash screen to disappear and then open and close Help->Credits
+//
+inline
+lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
+{
+ // printf("lyxstringInvariant constructor\n");
+ helper();
+}
+
+
+inline
+lyxstringInvariant::~lyxstringInvariant()
+{
+ helper();
+ // printf("lyxstringInvariant destructor completed\n");
+}
+
+
+inline
+void lyxstringInvariant::helper() const
+{
+ // Some of these tests might look pointless but they are
+ // all part of the invariant and if we want to make sure
+ // we have a bullet proof implementation then we need to
+ // test every last little thing we *know* should be true.
+ // I may have missed a test or two, so feel free to fill
+ // in the gaps. ARRae.
+ Assert(object);
+ Assert(object->rep);
+ Assert(object->rep->s); // s is never 0
+ Assert(object->rep->res); // res cannot be 0
+ Assert(object->rep->sz <= object->rep->res);
+ Assert(object->rep->ref >= 1); // its in use so it must be referenced
+ Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
+ // if it does ever == then we should be generating a new copy
+ // and starting again. (Is char always 8-bits?)
+}
+#define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
+#else
+#define TestlyxstringInvariant(s)
+#endif //DEVEL_VERSION
+
+
+///////////////////////////////////////
+// Constructors and Deconstructors.
+///////////////////////////////////////
+
+lyxstring::size_type const lyxstring::npos =
+static_cast<lyxstring::size_type>(-1);
+