diff --git a/include/svx/annotation/Annotation.hxx b/include/svx/annotation/Annotation.hxx index 491bbbfd0c6f..ba2021a70ec6 100644 --- a/include/svx/annotation/Annotation.hxx +++ b/include/svx/annotation/Annotation.hxx @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include class SdrUndoAction; diff --git a/include/svx/svdpage.hxx b/include/svx/svdpage.hxx index e38a9160bb00..55f11379356d 100644 --- a/include/svx/svdpage.hxx +++ b/include/svx/svdpage.hxx @@ -35,7 +35,7 @@ #include #include #include -#include +#include // predefines namespace model { class Theme; } diff --git a/include/svx/UniqueID.hxx b/include/tools/UniqueID.hxx similarity index 63% rename from include/svx/UniqueID.hxx rename to include/tools/UniqueID.hxx index 91e4664b2d5a..c649a5185469 100644 --- a/include/svx/UniqueID.hxx +++ b/include/tools/UniqueID.hxx @@ -10,31 +10,25 @@ #pragma once #include -#include +#include /** Unique ID for an object. * * Generates a unique ID and stores it in a member variable, so the ID returned * by getId() is the same as long as the object is alive. * - * ID 0 means the ID is not yet created, so 0 is not a valid ID + * ID numbers start with 1. * */ -class UniqueID final +class TOOLS_DLLPUBLIC UniqueID final { private: - sal_uInt64 mnID = 0; + sal_uInt64 mnID; public: - sal_uInt64 getID() const - { - if (!mnID) - { - static std::atomic staticCounter(1); - const_cast(this)->mnID = staticCounter.fetch_add(1); - } - return mnID; - } + UniqueID(); + + sal_uInt64 getID() const { return mnID; } }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/CppunitTest_tools_unique_id.mk b/tools/CppunitTest_tools_unique_id.mk new file mode 100644 index 000000000000..547832910f80 --- /dev/null +++ b/tools/CppunitTest_tools_unique_id.mk @@ -0,0 +1,29 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +$(eval $(call gb_CppunitTest_CppunitTest,tools_unique_id)) + + +$(eval $(call gb_CppunitTest_set_include,tools_unique_id,\ + $$(INCLUDE) \ + -I$(SRCDIR)/tools/inc \ +)) + +$(eval $(call gb_CppunitTest_add_exception_objects,tools_unique_id, \ + tools/qa/cppunit/UniqueIdTest \ +)) + +$(eval $(call gb_CppunitTest_use_libraries,tools_unique_id, \ + tl \ + test \ + unotest \ +)) + + +# vim: set noet sw=4 ts=4: diff --git a/tools/Library_tl.mk b/tools/Library_tl.mk index fee6fb1b4097..e2f1a9fccbf6 100644 --- a/tools/Library_tl.mk +++ b/tools/Library_tl.mk @@ -72,6 +72,7 @@ $(eval $(call gb_Library_add_exception_objects,tl,\ tools/source/misc/cpuid \ tools/source/misc/extendapplicationenvironment \ tools/source/misc/json_writer \ + tools/source/misc/UniqueID \ tools/source/ref/globname \ tools/source/ref/ref \ tools/source/stream/stream \ diff --git a/tools/Module_tools.mk b/tools/Module_tools.mk index 5632a5b94a2d..4d477462c0fe 100644 --- a/tools/Module_tools.mk +++ b/tools/Module_tools.mk @@ -33,6 +33,7 @@ $(eval $(call gb_Module_add_targets_for_build,tools,\ $(eval $(call gb_Module_add_check_targets,tools,\ CppunitTest_tools_test \ CppunitTest_tools_config \ + CppunitTest_tools_unique_id \ )) # vim: set noet sw=4 ts=4: diff --git a/tools/qa/cppunit/UniqueIdTest.cxx b/tools/qa/cppunit/UniqueIdTest.cxx new file mode 100644 index 000000000000..e85fcfb0cf39 --- /dev/null +++ b/tools/qa/cppunit/UniqueIdTest.cxx @@ -0,0 +1,79 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include +#include +#include + +namespace +{ +class UniqueIdTest : public CppUnit::TestFixture +{ +}; + +struct ObjectExample1 +{ + UniqueID maID; +}; + +struct ObjectExample2 +{ + UniqueID maID; +}; + +CPPUNIT_TEST_FIXTURE(UniqueIdTest, testUniqueness) +{ + UniqueID aID; + // Check ID + CPPUNIT_ASSERT_EQUAL(sal_uInt64(1), aID.getID()); + // Call again - same result + CPPUNIT_ASSERT_EQUAL(sal_uInt64(1), aID.getID()); + + // Check creating another instance + { + UniqueID aID2; + CPPUNIT_ASSERT_EQUAL(sal_uInt64(1), aID.getID()); + CPPUNIT_ASSERT_EQUAL(sal_uInt64(2), aID2.getID()); + } + + // Check creating third instance + { + UniqueID aID3; + CPPUNIT_ASSERT_EQUAL(sal_uInt64(1), aID.getID()); + CPPUNIT_ASSERT_EQUAL(sal_uInt64(3), aID3.getID()); + } + + // Check object copying - preserve the id + ObjectExample1 objectA; + CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), objectA.maID.getID()); + + ObjectExample1 objectB = objectA; + CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), objectA.maID.getID()); + CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), objectB.maID.getID()); + + // Multiple objects + ObjectExample2 objectC; + ObjectExample1 objectD; + ObjectExample2 objectE; + ObjectExample1 objectF; + + CPPUNIT_ASSERT_EQUAL(sal_uInt64(5), objectC.maID.getID()); + CPPUNIT_ASSERT_EQUAL(sal_uInt64(6), objectD.maID.getID()); + CPPUNIT_ASSERT_EQUAL(sal_uInt64(7), objectE.maID.getID()); + CPPUNIT_ASSERT_EQUAL(sal_uInt64(8), objectF.maID.getID()); +} + +} // end anonymous namespace + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/source/misc/UniqueID.cxx b/tools/source/misc/UniqueID.cxx new file mode 100644 index 000000000000..3af43eccb968 --- /dev/null +++ b/tools/source/misc/UniqueID.cxx @@ -0,0 +1,19 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +UniqueID::UniqueID() +{ + static std::atomic staticCounter(1); + mnID = staticCounter.fetch_add(1); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */