Skip to content

Commit 8c906c4

Browse files
author
Wojciech Sekta
committed
Refactor model table name handling to use std::string_view for improved performance and memory efficiency. Update tests and related files to reflect these changes. Add new utility functions for constexpr string views and type name retrieval.
1 parent 3a169e6 commit 8c906c4

File tree

17 files changed

+192
-54
lines changed

17 files changed

+192
-54
lines changed

CMakeLists.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,25 @@ if (BUILD_ORM_CXX_TESTS)
7676
add_subdirectory(externals/googletest)
7777

7878
set(UT_SOURCES
79-
tests/ModelTest.cpp
80-
8179
src/database/sqlite/SqliteTypeTranslatorTest.cpp
8280
src/model/ColumnTypeTest.cpp
8381
src/utils/StringUtilsTest.cpp
84-
tests/model/IdInfoTest.cpp
85-
tests/model/TableInfoTest.cpp
86-
tests/database/TransactionsTest.cpp
87-
tests/database/SimpleModelTest.cpp
88-
tests/database/IdModelsTest.cpp
89-
tests/database/RelatedModelTest.cpp
90-
tests/database/BaseOperationsTest.cpp
9182
src/database/defaults/DefaultCreateTableCommandTest.cpp
9283
src/database/defaults/DefaultDropTableCommandTest.cpp
9384
src/database/defaults/DefaultInsertCommandTest.cpp
9485
src/database/defaults/DefaultSelectCommandTest.cpp
9586
src/query/ConditionTest.cpp
87+
88+
tests/ModelTest.cpp
89+
tests/database/TransactionsTest.cpp
90+
tests/database/SimpleModelTest.cpp
91+
tests/database/IdModelsTest.cpp
92+
tests/database/RelatedModelTest.cpp
93+
tests/database/BaseOperationsTest.cpp
94+
tests/model/IdInfoTest.cpp
95+
tests/model/TableInfoTest.cpp
96+
tests/reflection/TypeNameTest.cpp
97+
tests/utils/ConstexprStringViewTest.cpp
9698
)
9799

98100
set(GTEST_INCLUDE_DIR

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct ObjectModel
5151
// int id;
5252

5353
// defining table_name is optional, adding it will overwrite default table name
54-
inline static const std::string table_name = "object_model";
54+
inline static constexpr std::string_view table_name = "object_model";
5555

5656
// defining columns_names is optional, adding it will overwrite default columns names
5757
// not all columns have to be defined, others will get default names

examples/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct ObjectModel
1717
// int id;
1818

1919
// defining table_name is optional, adding it will overwrite default table name
20-
inline static const std::string table_name = "object_model";
20+
inline static constexpr std::string_view table_name = "object_model";
2121

2222
// defining columns_names is optional, adding it will overwrite default columns names
2323
// not all columns have to be defined, others will get default names

include/orm-cxx/model/GetForeignModelInfoFromField.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "IdInfo.hpp"
24
#include "ModelInfo.hpp"
35

include/orm-cxx/model/GetModelInfo.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "GetForeignModelInfoFromField.hpp"
24
#include "ModelInfo.hpp"
35
#include "orm-cxx/utils/ConstexprFor.hpp"

include/orm-cxx/model/IdInfo.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ auto getPrimaryIdColumnsNames() -> std::unordered_set<std::string>
3232
}
3333

3434
template <typename T>
35-
constexpr auto checkIfIsModelWithId() -> bool
35+
consteval auto checkIfIsModelWithId() -> bool
3636
{
3737
if constexpr (requires { T::id_columns; })
3838
{

include/orm-cxx/model/ModelInfo.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace orm::model
77
{
88
struct ModelInfo
99
{
10-
std::string tableName;
10+
std::string_view tableName;
1111
std::vector<ColumnInfo> columnsInfo;
1212
std::unordered_set<std::string> idColumnsNames;
1313
std::unordered_map<std::string, ModelInfo> foreignModelsInfo;
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
#pragma once
22

33
#include <string>
4+
#include <string_view>
45

5-
#include "orm-cxx/utils/DisableExternalsWarning.hpp"
66
#include "orm-cxx/utils/StringUtils.hpp"
7-
8-
DISABLE_WARNING_PUSH
9-
10-
DISABLE_EXTERNAL_WARNINGS
11-
12-
#include <rfl.hpp>
13-
14-
DISABLE_WARNING_POP
7+
#include "orm-cxx/utils/ConstexprStringView.hpp"
8+
#include "orm-cxx/reflection/TypeName.hpp"
159

1610
namespace orm::model
1711
{
@@ -22,21 +16,25 @@ namespace orm::model
2216
* @return The table name as a string view.
2317
*/
2418
template <typename T>
25-
auto getTableName() -> std::string
19+
consteval auto getTableName() -> std::string_view
2620
{
2721
if constexpr (requires { T::table_name; })
2822
{
29-
return std::string(T::table_name);
23+
return T::table_name;
3024
}
3125
else
3226
{
33-
auto typeName = rfl::type_name_t<T>().str();
27+
constexpr auto callable = []() {
28+
auto typeName = std::string(reflection::getTypeName<T>());
29+
30+
utils::replaceAll(typeName, "::", "_");
31+
utils::replaceAll(typeName, "class ", "");
32+
utils::replaceAll(typeName, "struct ", "");
3433

35-
utils::replaceAll(typeName, "::", "_");
36-
utils::replaceAll(typeName, "class ", "");
37-
utils::replaceAll(typeName, "struct ", "");
34+
return typeName;
35+
};
3836

39-
return typeName;
37+
return utils::makeConstexprStringView(callable);
4038
}
4139
}
4240
} // namespace orm::model
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
#include <source_location>
5+
6+
namespace orm::reflection
7+
{
8+
template <typename T>
9+
consteval auto getTypeName() -> std::string_view
10+
{
11+
const auto func_name =
12+
std::string_view{std::source_location::current().function_name()};
13+
#if defined(__clang__)
14+
const auto split = func_name.substr(0, func_name.size() - 1);
15+
return split.substr(split.find("T = ") + 4);
16+
#elif defined(__GNUC__)
17+
const auto split = func_name.substr(0, func_name.size() - 1);
18+
return split.substr(split.find("T = ") + 4);
19+
#elif defined(_MSC_VER)
20+
const auto split = func_name.substr(0, func_name.size() - 7);
21+
return split.substr(split.find("get_type_name_str_view<") + 23);
22+
#else
23+
static_assert(
24+
false,
25+
"You are using an unsupported compiler. Please use GCC, Clang "
26+
"or MSVC or explicitly tag your structs using 'Tag' or 'Name'.");
27+
#endif
28+
}
29+
} // namespace orm::reflection
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <string_view>
5+
#include <array>
6+
7+
namespace orm::utils
8+
{
9+
template <typename T>
10+
concept StringCallable = requires(T t) { t(); } && std::is_convertible_v<std::invoke_result_t<T>, std::string>;
11+
12+
struct OversizedArray
13+
{
14+
std::array<char, 1024 * 1024> data{};
15+
std::size_t size{};
16+
};
17+
18+
constexpr auto makeOversizedArray(const std::string& str)
19+
{
20+
OversizedArray result;
21+
std::copy(str.begin(), str.end(), result.data.begin());
22+
result.size = str.size();
23+
return result;
24+
}
25+
26+
consteval auto makeRightSizedArray(auto callable)
27+
{
28+
constexpr auto oversized = makeOversizedArray(callable());
29+
std::array<char, oversized.size> result;
30+
std::copy(oversized.data.begin(), oversized.data.begin() + oversized.size, result.begin());
31+
return result;
32+
}
33+
34+
template<auto Data>
35+
consteval const auto& makeStatic()
36+
{
37+
return Data;
38+
}
39+
40+
consteval auto makeConstexprStringView(auto callable) -> std::string_view
41+
{
42+
constexpr auto& data = makeStatic<makeRightSizedArray(callable)>();
43+
return std::string_view{data.begin(), data.size()};
44+
}
45+
} // namespace orm::utils

0 commit comments

Comments
 (0)