move UniqueID to tools and rework to increase counter in constructor
In the tools module it can be used in more other modules. Changed the instantiation to increase the counter already in the constructor, so no need to check if the ID is 0. Also test for UniqueID Change-Id: I77c80461875c69496a7e218534d16d6550c168c2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170061 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
This commit is contained in:
parent
7cc291a8f2
commit
02c91f82c2
8 changed files with 138 additions and 15 deletions
|
@ -21,7 +21,7 @@
|
|||
#include <cppuhelper/propertysetmixin.hxx>
|
||||
#include <svx/annotation/Annotation.hxx>
|
||||
#include <svx/annotation/TextAPI.hxx>
|
||||
#include <svx/UniqueID.hxx>
|
||||
#include <tools/UniqueID.hxx>
|
||||
#include <vcl/bitmapex.hxx>
|
||||
|
||||
class SdrUndoAction;
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include <optional>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <svx/UniqueID.hxx>
|
||||
#include <tools/UniqueID.hxx>
|
||||
|
||||
// predefines
|
||||
namespace model { class Theme; }
|
||||
|
|
|
@ -10,31 +10,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <sal/types.h>
|
||||
#include <atomic>
|
||||
#include <tools/toolsdllapi.h>
|
||||
|
||||
/** 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<sal_uInt64> staticCounter(1);
|
||||
const_cast<UniqueID*>(this)->mnID = staticCounter.fetch_add(1);
|
||||
}
|
||||
return mnID;
|
||||
}
|
||||
UniqueID();
|
||||
|
||||
sal_uInt64 getID() const { return mnID; }
|
||||
};
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
29
tools/CppunitTest_tools_unique_id.mk
Normal file
29
tools/CppunitTest_tools_unique_id.mk
Normal file
|
@ -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:
|
|
@ -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 \
|
||||
|
|
|
@ -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:
|
||||
|
|
79
tools/qa/cppunit/UniqueIdTest.cxx
Normal file
79
tools/qa/cppunit/UniqueIdTest.cxx
Normal file
|
@ -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 <tools/UniqueID.hxx>
|
||||
|
||||
#include <cppunit/TestAssert.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/plugin/TestPlugIn.h>
|
||||
|
||||
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: */
|
19
tools/source/misc/UniqueID.cxx
Normal file
19
tools/source/misc/UniqueID.cxx
Normal file
|
@ -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 <tools/UniqueID.hxx>
|
||||
#include <atomic>
|
||||
|
||||
UniqueID::UniqueID()
|
||||
{
|
||||
static std::atomic<sal_uInt64> staticCounter(1);
|
||||
mnID = staticCounter.fetch_add(1);
|
||||
}
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
Loading…
Reference in a new issue