Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
LEFEBVREJP email
radix
Commits
2e7dba2f
Commit
2e7dba2f
authored
Apr 22, 2017
by
LEFEBVREJP email
Browse files
Adding radixpattern subpackage to contain regular c++ patterns.
parent
683d61af
Changes
6
Hide whitespace changes
Inline
Side-by-side
cmake/Dependencies.cmake
View file @
2e7dba2f
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES
(
SUBPACKAGES_DIRS_CLASSIFICATIONS_OPTREQS
bug radixbug SS OPTIONAL
pattern radixpattern SS OPTIONAL
para radixpara SS OPTIONAL
command radixcommand SS OPTIONAL
math radixmath SS OPTIONAL
...
...
radixpattern/CMakeLists.txt
0 → 100644
View file @
2e7dba2f
TRIBITS_SUBPACKAGE
(
pattern
)
SET
(
HEADERS
factory.hh
)
#
# Add testing directory
TRIBITS_ADD_TEST_DIRECTORIES
(
tests
)
INSTALL
(
FILES
${
HEADERS
}
DESTINATION
"include/radixpattern/"
)
TRIBITS_SUBPACKAGE_POSTPROCESS
()
radixpattern/cmake/Dependencies.cmake
0 → 100644
View file @
2e7dba2f
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES
(
LIB_REQUIRED_PACKAGES radixbug
LIB_OPTIONAL_PACKAGES
TEST_REQUIRED_PACKAGES testframework
TEST_OPTIONAL_PACKAGES
LIB_REQUIRED_TPLS
LIB_OPTIONAL_TPLS
TEST_REQUIRED_TPLS
TEST_OPTIONAL_TPLS
)
radixpattern/factory.hh
0 → 100644
View file @
2e7dba2f
#ifndef RADIXPATTERN_FACTORY_HH_
#define RADIXPATTERN_FACTORY_HH_
/*
* @file: factory.hh
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
*
* Created on April 22, 2017, 10:39 AM
*/
#include
<string>
#include
<map>
#include
<memory>
#include
<functional>
#include
<stdexcept>
#include
"radixbug/bug.hh"
namespace
radix
{
template
<
typename
ReturnType
,
typename
KeyType
=
std
::
string
,
typename
MakerType
=
std
::
function
<
ReturnType
()>
>
/**
* @class Factory
* @brief The Factory pattern class
* Can register creators for key to object relations.
*/
class
Factory
{
protected:
std
::
map
<
KeyType
,
MakerType
>
typeMakers
;
public:
typedef
std
::
shared_ptr
<
Factory
<
ReturnType
,
KeyType
,
MakerType
>>
SP
;
/**
* @brief registerMaker - Registers a KeyType key and MakerType creator method
* @param key - const KeyType&
* @param maker - MakerType function
*/
void
registerMaker
(
const
KeyType
&
key
,
MakerType
maker
)
{
radix_line
(
"Register maker with:"
<<
key
);
typeMakers
[
key
]
=
maker
;
}
// registerMaker
/**
* @brief create - Invokes the stores creator
* @param key - const KeyType associated which was registered with the creator
* @param paramets - Variadic template arguments
* @return - typename T*
*/
template
<
typename
...
Arguments
>
ReturnType
make
(
const
KeyType
&
key
,
Arguments
...
parameters
)
const
{
radix_line
(
"Searching for maker("
<<
key
<<
") ..."
);
auto
makerIterator
=
typeMakers
.
find
(
key
);
if
(
makerIterator
==
typeMakers
.
end
())
{
throw
std
::
runtime_error
(
"No creator registered for key"
);
}
radix_line
(
"Calling maker("
<<
key
<<
") ..."
);
return
makerIterator
->
second
(
parameters
...);
}
// make
};
//class Factory
}
//namespace radix;
#endif
/* RADIXPATTERN_FACTORY_HH_ */
radixpattern/tests/CMakeLists.txt
0 → 100644
View file @
2e7dba2f
INCLUDE
(
GoogleTest
)
ADD_GOOGLE_TEST
(
tstFactory.cc NP 1
)
radixpattern/tests/tstFactory.cc
0 → 100644
View file @
2e7dba2f
#include
"gtest/gtest.h"
#include
<memory>
#include
"radixpattern/factory.hh"
using
namespace
radix
;
TEST
(
radixpattern
,
Factory
)
{
auto
two
=
[]()
->
int
{
return
2
;};
auto
three
=
[]()
->
int
{
return
3
;};
Factory
<
int
,
int
>::
SP
factory
=
std
::
make_shared
<
Factory
<
int
,
int
>>
();
factory
->
registerMaker
(
2
,
two
);
factory
->
registerMaker
(
3
,
three
);
auto
twoValue
=
factory
->
make
(
2
);
EXPECT_EQ
(
2
,
twoValue
);
auto
threeValue
=
factory
->
make
(
3
);
EXPECT_EQ
(
3
,
threeValue
);
typedef
radix
::
Factory
<
std
::
function
<
double
(
double
)
>
,
int
>
MassConverterFactory
;
MassConverterFactory
::
SP
cFactory
=
std
::
make_shared
<
MassConverterFactory
>
();
auto
straight
=
[]()
{
return
[](
double
conc
){
return
conc
;};
};
auto
doubleStraight
=
[]()
{
return
[](
double
conc
){
return
conc
*
2.
;};
};
cFactory
->
registerMaker
(
0
,
straight
);
cFactory
->
registerMaker
(
1
,
doubleStraight
);
{
auto
func
=
cFactory
->
make
(
0
);
EXPECT_EQ
(
1.5
,
func
(
1.5
));
}
{
auto
func
=
cFactory
->
make
(
1
);
EXPECT_EQ
(
-
21.
,
func
(
-
10.5
));
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment