Skip to content

Commit 0463c78

Browse files
committed
Add HasClassRegistry static method deregisterType.
This clears registration of that class under any name or alias.
1 parent 600615c commit 0463c78

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/diffpy/HasClassRegistry.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ class HasClassRegistry
5555

5656
// methods provided by HasClassRegistry
5757

58-
/// Add a prototype of this instance to the registry
58+
/// Add a prototype of this instance to the registry.
5959
virtual bool registerThisType() const;
6060

61-
/// Make registered type tp available under a different alias
61+
/// Make registered type tp available under a different alias.
6262
static bool aliasType(const std::string& tp, const std::string& al);
6363

64+
/// Clear registration of the specified type under its standard name
65+
/// or any alias. Return the number of unset names and aliases or
66+
/// 0 if the specifed string type was not registered.
67+
static int deregisterType(const std::string& tp);
68+
6469
/// Create new instance of a specified string type.
6570
static SharedPtr createByType(const std::string& tp);
6671

src/diffpy/HasClassRegistry.ipp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ bool HasClassRegistry<TBase>::aliasType(const std::string& tp,
7777
}
7878

7979

80+
template <class TBase>
81+
int HasClassRegistry<TBase>::deregisterType(const std::string& tp)
82+
{
83+
RegistryStorage& reg = getRegistry();
84+
typename RegistryStorage::iterator ii = reg.find(tp);
85+
if (ii == reg.end()) return 0;
86+
SharedPtr p = ii->second;
87+
int rv = 0;
88+
for (ii = reg.begin(); ii != reg.end();)
89+
{
90+
typename RegistryStorage::iterator jj = ii++;
91+
if (jj->second == p)
92+
{
93+
reg.erase(jj);
94+
++rv;
95+
}
96+
}
97+
return rv;
98+
}
99+
100+
80101
template <class TBase>
81102
typename HasClassRegistry<TBase>::SharedPtr
82103
HasClassRegistry<TBase>::createByType(const std::string& tp)

src/tests/TestHasClassRegistry.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ class TestHasClassRegistry : public CxxTest::TestSuite
4343
mpreg = msftb.get();
4444
}
4545

46+
void tearDown()
47+
{
48+
// restore SFTXray registration if removed in some test.
49+
msftb->registerThisType();
50+
TS_ASSERT(ScatteringFactorTable::isRegisteredType("X"));
51+
TS_ASSERT(ScatteringFactorTable::isRegisteredType("xray"));
52+
}
53+
54+
55+
void test_deregisterType()
56+
{
57+
TS_ASSERT_EQUALS(0,
58+
ScatteringFactorTable::deregisterType("invalid"));
59+
TS_ASSERT_EQUALS(2,
60+
ScatteringFactorTable::deregisterType("X"));
61+
TS_ASSERT(!(mpreg->isRegisteredType("xray")));
62+
TS_ASSERT(!(mpreg->isRegisteredType("X")));
63+
}
64+
4665

4766
void test_isRegisteredType()
4867
{

0 commit comments

Comments
 (0)