Skip to content

Commit 960e699

Browse files
committed
CxxTestRegistration: code-improvement, wip.
1 parent 0d7382c commit 960e699

33 files changed

+531
-16
lines changed

CxxTestProps/inc/Complex.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#pragma once
22

3+
#include <string>
4+
5+
// C-style/free-functions.
6+
std::string getComplexNumAsString();
7+
38
namespace complex
49
{
510
double getMagnitude();

CxxTestProps/inc/StringOps.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
#include <string>
44

5-
// C-style/free-functions.
6-
std::string getComplexNumAsString();
7-
85
std::string reverseString();
96

107
std::string reverseString(const char* pStr);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
namespace rtl {
6+
class Function;
7+
}
8+
9+
namespace test_mirror
10+
{
11+
static void registerPodStdTypes(std::vector<rtl::Function>&);
12+
13+
static void registerTypeComplex(std::vector<rtl::Function>&);
14+
15+
static void registerTypeDate(std::vector<rtl::Function>&);
16+
17+
static void registerTypeEvent(std::vector<rtl::Function>&);
18+
19+
static void registerTypeCalender(std::vector<rtl::Function>&);
20+
21+
static void registerTypePerson(std::vector<rtl::Function>&);
22+
23+
static void registerTypeBook(std::vector<rtl::Function>&);
24+
25+
static void registerTypeLibrary(std::vector<rtl::Function>&);
26+
27+
static void registerTypeAnimal(std::vector<rtl::Function>&);
28+
}

CxxTestRegistration/inc/TestMirrorProvider.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#pragma once
22

3-
#include <rtl/rtl.h>
3+
namespace rtl {
4+
class CxxMirror;
5+
}
46

57
namespace test_mirror
68
{
7-
struct cxx
8-
{
9+
struct cxx {
10+
911
static const rtl::CxxMirror& mirror();
1012
};
13+
}
1114

12-
15+
namespace test_mirror
16+
{
1317
// Optional setup: do this if you prefer to access your registered types by unique 'ID', not by string.
1418
struct reflected_id {
1519

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
#include <Reflect.hpp>
3+
4+
#include "Animal.h"
5+
#include "Registration.h"
6+
#include "TestUtilsAnimal.h"
7+
8+
using namespace test_utils;
9+
10+
namespace test_mirror
11+
{
12+
void registerTypeAnimal(std::vector<rtl::Function>& fns)
13+
{
14+
// class 'Animal', methods & constructors.
15+
fns.push_back(rtl::type().record<Animal>(animal::class_)
16+
.build());
17+
18+
fns.push_back(rtl::type().member<Animal>()
19+
.constructor<std::string>()
20+
.build()); //overloaded constructor.
21+
22+
fns.push_back(rtl::type().member<Animal>()
23+
.method(animal::str_setFamilyName)
24+
.build(&Animal::setFamilyName)); //unique method, no overloads.
25+
26+
// Unique const-method, no overloads.
27+
fns.push_back(rtl::type().member<Animal>()
28+
.methodConst(animal::str_getFamilyName)
29+
.build(&Animal::getFamilyName));
30+
31+
// Overloaded method, taking const-ref as argument.
32+
fns.push_back(rtl::type().member<Animal>()
33+
.method<const std::string&>(animal::str_setAnimalName)
34+
.build(&Animal::setAnimalName));
35+
36+
// Static method, taking const-ref as argument.
37+
fns.push_back(rtl::type().member<Animal>()
38+
.methodStatic<const std::string&>(animal::str_updateZooKeeper)
39+
.build(&Animal::updateZooKeeper));
40+
41+
#if defined(__GNUC__) && !defined(__clang__)
42+
/*
43+
GCC here fails to automatically resolve the correct overloaded functor
44+
when both a lvalue reference and an rvalue overload exist.
45+
To disambiguate, explicitly cast the member function pointer, e.g.:
46+
47+
static_cast<void (Animal::*)(std::string&)>(&Animal::setAnimalName)
48+
*/
49+
fns.push_back(rtl::type().member<Animal>()
50+
.method<std::string&>(animal::str_setAnimalName)
51+
.build(static_cast<void(Animal::*)(std::string&)>(&Animal::setAnimalName))); //overloaded method, taking non-const lvalue reference as argument.
52+
53+
fns.push_back(rtl::type().member<Animal>()
54+
.method<std::string&&>(animal::str_setAnimalName)
55+
.build(static_cast<void(Animal::*)(std::string&&)>(&Animal::setAnimalName))); //overloaded method, taking rvalue reference as argument.
56+
57+
fns.push_back(rtl::type().member<Animal>()
58+
.methodStatic<std::string&>(animal::str_updateZooKeeper)
59+
.build(static_cast<std::string(*)(std::string&)>(&Animal::updateZooKeeper))); //static method, taking non-const lvalue reference as argument.
60+
61+
fns.push_back(rtl::type().member<Animal>()
62+
.methodStatic<std::string&&>(animal::str_updateZooKeeper)
63+
.build(static_cast<std::string(*)(std::string&&)>(&Animal::updateZooKeeper))); //static method, taking rvalue reference as argument.
64+
#else
65+
fns.push_back(rtl::type().member<Animal>()
66+
.method<std::string&>(animal::str_setAnimalName)
67+
.build(&Animal::setAnimalName)); //overloaded method, taking non-const lvalue reference as argument.
68+
69+
fns.push_back(rtl::type().member<Animal>()
70+
.method<std::string&&>(animal::str_setAnimalName)
71+
.build(&Animal::setAnimalName)); //overloaded method, taking rvalue reference as argument.
72+
73+
fns.push_back(rtl::type().member<Animal>()
74+
.methodStatic<std::string&>(animal::str_updateZooKeeper)
75+
.build(&Animal::updateZooKeeper)); //static method, taking non-const lvalue reference as argument.
76+
77+
fns.push_back(rtl::type().member<Animal>()
78+
.methodStatic<std::string&&>(animal::str_updateZooKeeper)
79+
.build(&Animal::updateZooKeeper)); //static method, taking rvalue reference as argument.
80+
#endif
81+
}
82+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
#include <Reflect.hpp>
3+
4+
#include "Book.h"
5+
#include "Registration.h"
6+
#include "TestUtilsBook.h"
7+
8+
using namespace test_utils;
9+
10+
namespace test_mirror
11+
{
12+
void registerTypeBook(std::vector<rtl::Function>& fns)
13+
{
14+
// class 'Book', methods & constructors.
15+
// Registering default constructor.
16+
fns.push_back(rtl::type().record<Book>(book::class_)
17+
.build());
18+
19+
// Registering overloaded constructor, signature must be specified as template parameter.
20+
fns.push_back(rtl::type().member<Book>()
21+
.constructor<double, std::string>()
22+
.build());
23+
24+
// Unique methods, no overloads.
25+
fns.push_back(rtl::type().member<Book>()
26+
.method(book::str_setAuthor)
27+
.build(&Book::setAuthor));
28+
29+
// Unique method, taking 'std::string' & 'const std::string&' as argument, auto deduced via function-pointer.
30+
fns.push_back(rtl::type().member<Book>()
31+
.method(book::str_addPreface)
32+
.build(&Book::addPreface));
33+
34+
// Furthur registrations of unique-menthods, signature auto-deduced via function pointer.
35+
fns.push_back(rtl::type().member<Book>()
36+
.method(book::str_setDescription)
37+
.build(&Book::setDescription));
38+
39+
fns.push_back(rtl::type().member<Book>()
40+
.method(book::str_getPublishedOn)
41+
.build(&Book::getPublishedOn));
42+
43+
fns.push_back(rtl::type().member<Book>()
44+
.method(book::str_addCopyrightTag)
45+
.build(&Book::addCopyrightTag));
46+
47+
// Registering overloaded methods, signature must be specified as template params since other overloads exists, else compiler error.
48+
fns.push_back(rtl::type().member<Book>()
49+
.method<void>(book::str_updateBookInfo)
50+
.build(&Book::updateBookInfo));
51+
52+
fns.push_back(rtl::type().member<Book>()
53+
.method<const char*, double, std::string>(book::str_updateBookInfo)
54+
.build(&Book::updateBookInfo));
55+
56+
fns.push_back(rtl::type().member<Book>()
57+
.method<std::string, double, const char*>(book::str_updateBookInfo)
58+
.build(&Book::updateBookInfo));
59+
}
60+
}

CxxTestRegistration/src/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ project(CxxTestRegistration)
66
# Create a variable containing the source files for your target
77
set(LOCAL_SOURCES
88
"${CMAKE_CURRENT_LIST_DIR}/TestMirrorProvider.cpp"
9+
"${CMAKE_CURRENT_LIST_DIR}/AnimalRegistration.cpp"
10+
"${CMAKE_CURRENT_LIST_DIR}/BookRegistration.cpp"
11+
"${CMAKE_CURRENT_LIST_DIR}/CalenderRegistration.cpp"
12+
"${CMAKE_CURRENT_LIST_DIR}/ComplexRegistration.cpp"
13+
"${CMAKE_CURRENT_LIST_DIR}/DateRegistration.cpp"
14+
"${CMAKE_CURRENT_LIST_DIR}/EventRegistration.cpp"
15+
"${CMAKE_CURRENT_LIST_DIR}/LibraryRegistration.cpp"
16+
"${CMAKE_CURRENT_LIST_DIR}/PersonRegistration.cpp"
17+
"${CMAKE_CURRENT_LIST_DIR}/PodStdRegistration.cpp"
918
)
1019

1120
SET(LOCAL_HEADERS
21+
"${PROJECT_SOURCE_DIR}/inc/Registration.h"
1222
"${PROJECT_SOURCE_DIR}/inc/TestMirrorProvider.h"
1323
)
1424

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#include <Reflect.hpp>
3+
4+
#include "Registration.h"
5+
6+
#include "Date.h"
7+
#include "TestUtilsDate.h"
8+
9+
using namespace test_utils;
10+
11+
namespace test_mirror
12+
{
13+
void registerTypeCalender(std::vector<rtl::Function>& fns)
14+
{
15+
// Registring static-method, 'methodStatic()' function must be used. compiler error otherwise.
16+
fns.push_back(rtl::type().member<nsdate::Calender>()
17+
.methodStatic(calender::str_create)
18+
.build(&nsdate::Calender::create));
19+
20+
// Registring unique methods of class Calender, no overloads.
21+
fns.push_back(rtl::type().member<nsdate::Calender>()
22+
.method(calender::str_getTheEvent)
23+
.build(&nsdate::Calender::getTheEvent));
24+
25+
fns.push_back(rtl::type().member<nsdate::Calender>()
26+
.method(calender::str_getTheDate)
27+
.build(&nsdate::Calender::getTheDate));
28+
29+
fns.push_back(rtl::type().member<nsdate::Calender>()
30+
.method(calender::str_getSavedEvent)
31+
.build(&nsdate::Calender::getSavedEvent));
32+
33+
fns.push_back(rtl::type().member<nsdate::Calender>()
34+
.method(calender::str_getSavedDate)
35+
.build(&nsdate::Calender::getSavedDate));
36+
37+
// class Calender, registering after the methods. (order doesn't matter)
38+
fns.push_back(rtl::type().ns(date::ns)
39+
.record<nsdate::Calender>(calender::struct_)
40+
.build());
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#include <Reflect.hpp>
3+
4+
#include "Complex.h"
5+
#include "Registration.h"
6+
#include "GlobalTestUtils.h"
7+
8+
using namespace test_utils;
9+
10+
namespace test_mirror
11+
{
12+
void registerTypeComplex(std::vector<rtl::Function>& fns)
13+
{
14+
// Unique function, no overloads, no need to specify signature as template parameters.
15+
fns.push_back(rtl::type().function(str_getComplexNumAsString)
16+
.build(getComplexNumAsString));
17+
18+
/* Grouping functions under a namespace, which is optional. they can be registered without it as well.
19+
but if registered under namspace, then to retrieve it from CxxMirror object, namespace name must be passed,
20+
e.g. cxx::mirror().getFunction("namespace_name", "function_name") & cxx::mirror().getRecord("namespace_name", "record_name") */
21+
fns.push_back(rtl::type().ns(str_complex)
22+
.function(str_setReal)
23+
.build(complex::setReal));
24+
25+
fns.push_back(rtl::type().ns(str_complex)
26+
.function(str_setImaginary)
27+
.build(complex::setImaginary));
28+
29+
fns.push_back(rtl::type().ns(str_complex)
30+
.function(str_getMagnitude)
31+
.build(complex::getMagnitude));
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#include <Reflect.hpp>
3+
4+
#include "Date.h"
5+
#include "Registration.h"
6+
#include "TestUtilsDate.h"
7+
8+
using namespace test_utils;
9+
10+
namespace test_mirror
11+
{
12+
void registerTypeDate(std::vector<rtl::Function>& fns)
13+
{
14+
// Constructors registration, class/struct name and type must be passed 'record<TYPE>("NAME")'.
15+
// Registers default constructor with implicit registration of destructor & copy-constructor.
16+
fns.push_back(rtl::type().ns(date::ns)
17+
.record<nsdate::Date>(date::struct_)
18+
.build());
19+
20+
// Overloaded constructor, taking 'string' as argument, signature must be specified as template parameter.
21+
fns.push_back(rtl::type().member<nsdate::Date>()
22+
.constructor<std::string>()
23+
.build());
24+
25+
// Again, register an overloaded constructor with diffeent signature.
26+
fns.push_back(rtl::type().member<nsdate::Date>()
27+
.constructor<unsigned, unsigned, unsigned>()
28+
.build());
29+
30+
// Registring, Unique method, no overloads. Taking param 'std::string', auto deduced via function-pointer.
31+
fns.push_back(rtl::type().member<nsdate::Date>()
32+
.method(date::str_updateDate)
33+
.build(&nsdate::Date::updateDate));
34+
35+
// Registring const-method, 'methodConst()' function must be used. compiler error otherwise.
36+
fns.push_back(rtl::type().member<nsdate::Date>()
37+
.methodConst(date::str_getAsString)
38+
.build(&nsdate::Date::getAsString));
39+
}
40+
}

0 commit comments

Comments
 (0)