Skip to content

Commit c413b35

Browse files
committed
Add HasClassRegistry static method isRegisteredType.
Return true when given name is registered string type or its alias.
1 parent 9d79ce3 commit c413b35

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/diffpy/HasClassRegistry.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class HasClassRegistry
6464
/// Create new instance of a specified string type.
6565
static SharedPtr createByType(const std::string& tp);
6666

67+
/// Return true if string is a registered string type or its alias.
68+
static bool isRegisteredType(const std::string& tp);
69+
6770
/// Return a set of all registered string types
6871
static std::set<std::string> getRegisteredTypes();
6972

src/diffpy/HasClassRegistry.ipp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ HasClassRegistry<TBase>::createByType(const std::string& tp)
9696
}
9797

9898

99+
template <class TBase>
100+
bool
101+
HasClassRegistry<TBase>::isRegisteredType(const std::string& tp)
102+
{
103+
const RegistryStorage& reg = getRegistry();
104+
return reg.count(tp);
105+
}
106+
107+
99108
template <class TBase>
100109
std::set<std::string>
101110
HasClassRegistry<TBase>::getRegisteredTypes()

src/tests/TestHasClassRegistry.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*****************************************************************************
2+
*
3+
* libdiffpy Complex Modeling Initiative
4+
* (c) 2016 Brookhaven Science Associates,
5+
* Brookhaven National Laboratory.
6+
* All rights reserved.
7+
*
8+
* File coded by: Pavol Juhas
9+
*
10+
* See AUTHORS.txt for a list of people who contributed.
11+
* See LICENSE.txt for license information.
12+
*
13+
******************************************************************************
14+
*
15+
* class TestHasClassRegistry -- test registration machinery for named classes.
16+
*
17+
*****************************************************************************/
18+
19+
#include <cxxtest/TestSuite.h>
20+
21+
#include <diffpy/srreal/ScatteringFactorTable.hpp>
22+
23+
using diffpy::srreal::ScatteringFactorTable;
24+
using diffpy::srreal::ScatteringFactorTablePtr;
25+
26+
//////////////////////////////////////////////////////////////////////////////
27+
// class TestHasClassRegistry
28+
//////////////////////////////////////////////////////////////////////////////
29+
30+
class TestHasClassRegistry : public CxxTest::TestSuite
31+
{
32+
private:
33+
34+
typedef diffpy::HasClassRegistry<ScatteringFactorTable> HCRBase;
35+
ScatteringFactorTablePtr msftb;
36+
HCRBase* mpreg;
37+
38+
public:
39+
40+
void setUp()
41+
{
42+
msftb = ScatteringFactorTable::createByType("xray");
43+
mpreg = msftb.get();
44+
}
45+
46+
47+
void test_isRegisteredType()
48+
{
49+
TS_ASSERT(msftb->isRegisteredType("xray"));
50+
TS_ASSERT(ScatteringFactorTable::isRegisteredType("X"));
51+
TS_ASSERT_EQUALS(false, mpreg->isRegisteredType("invalid"));
52+
}
53+
54+
}; // class TestHasClassRegistry
55+
56+
// End of file

0 commit comments

Comments
 (0)