Skip to content

Commit 7f0350f

Browse files
Move some code into source files to reduce compile time; #86 (#136)
--------- Co-authored-by: Patrick Urbanke <patrick@getml.com>
1 parent 1fa2391 commit 7f0350f

13 files changed

Lines changed: 427 additions & 380 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if(REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
4747
add_library(reflectcpp STATIC)
4848
endif()
4949

50-
target_sources(reflectcpp PRIVATE src/yyjson.c)
50+
target_sources(reflectcpp PRIVATE src/reflectcpp.cpp src/yyjson.c)
5151
set_source_files_properties(src/yyjson.c PROPERTIES LANGUAGE CXX)
5252

5353
target_compile_features(reflectcpp PUBLIC cxx_std_20)

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 getML
3+
Copyright (c) 2023-2024 Code17 GmbH
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Design principles for reflect-cpp include:
1818
- Close integration with containers from the C++ standard library
1919
- Close adherence to C++ idioms
2020
- Out-of-the-box support for JSON
21-
- Simple installation: If no JSON support is required, reflect-cpp is header-only. For JSON support, only a single source file needs to be compiled.
21+
- Simple installation
2222
- Simple extendability to other serialization formats
2323
- Simple extendability to custom classes
2424

@@ -520,17 +520,14 @@ The following compilers are supported:
520520
- Clang 14.0 or higher
521521
- MSVC 17.8 (19.38) or higher
522522
523-
### Option 1: Header-only
523+
### Option 1: Include source files into your own build
524524
525-
If you **do not** need JSON support or you want to link YYJSON yourself, then reflect-cpp is header-only. Simply copy the contents of the folder `include` into your source repository or add it to your include path.
526-
527-
### Option 2: Include source files into your own build
528-
529-
Simply copy the contents of the folder `include` into your source repository or add it to your include path and also add `src/yyjson.c` to your source files for compilation.
525+
Simply copy the contents of the folder `include` into your source repository or add it to your include path and also add `src/reflectcpp.cpp` and `src/yyjson.c` to your source files for compilation.
526+
If you want to link to your own version of yyjson, then only copy `src/reflectcpp.cpp`.
530527
531528
If you need support for other serialization formats like flexbuffers or XML, you should also include and link the respective libraries, as listed in the section on serialization formats.
532529
533-
### Option 3: Compilation using cmake
530+
### Option 2: Compilation using cmake
534531
535532
This will simply compile YYJSON, which is the JSON library underlying reflect-cpp. You can then include reflect-cpp in your project and link to the binary
536533
to get reflect-cpp with JSON support.
@@ -541,7 +538,7 @@ cmake --build build -j 4 # gcc, clang
541538
cmake --build build --config Release -j 4 # MSVC
542539
```
543540

544-
### Option 4: Compilation using cmake and vcpkg
541+
### Option 3: Compilation using cmake and vcpkg
545542

546543
If you want serialization formats other than JSON, you can either install them manually or use vcpkg.
547544

@@ -636,7 +633,7 @@ To run the tests, do the following:
636633

637634
### Make sure includes are relative
638635

639-
In order for the library to be able to function header-only, we need internal includes to be relative and not depend on any externally set include directory.
636+
We need internal includes to be relative and not depend on any externally set include directory.
640637

641638
That is, for example, if you are within any file in `rfl/internal`, prefer
642639
```cpp

include/rfl/Generic.hpp

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class Generic {
1717
using ReflectionType =
1818
std::variant<bool, int, double, std::string, Object, Array>;
1919

20-
Generic() : value_(false) {}
20+
Generic();
2121

22-
Generic(Generic&& _other) noexcept = default;
22+
Generic(Generic&& _other) noexcept;
2323

24-
Generic(const Generic& _other) = default;
24+
Generic(const Generic& _other);
2525

26-
Generic(const ReflectionType& _value) : value_(_value) {}
26+
Generic(const ReflectionType& _value);
2727

28-
Generic(ReflectionType&& _value) noexcept : value_(std::move(_value)) {}
28+
Generic(ReflectionType&& _value) noexcept;
2929

3030
template <class T,
3131
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
@@ -37,22 +37,16 @@ class Generic {
3737
bool>::type = true>
3838
Generic(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
3939

40-
~Generic() = default;
40+
~Generic();
4141

4242
/// Returns the underlying object.
4343
const ReflectionType& get() const { return value_; }
4444

4545
/// Assigns the underlying object.
46-
auto& operator=(const ReflectionType& _value) {
47-
value_ = _value;
48-
return *this;
49-
}
46+
Generic& operator=(const ReflectionType& _value);
5047

5148
/// Assigns the underlying object.
52-
auto& operator=(ReflectionType&& _value) noexcept {
53-
value_ = std::move(_value);
54-
return *this;
55-
}
49+
Generic& operator=(ReflectionType&& _value) noexcept;
5650

5751
/// Assigns the underlying object.
5852
template <class T,
@@ -64,81 +58,37 @@ class Generic {
6458
}
6559

6660
/// Assigns the underlying object.
67-
Generic& operator=(const Generic& _other) = default;
61+
Generic& operator=(const Generic& _other);
6862

6963
/// Assigns the underlying object.
70-
Generic& operator=(Generic&& _other) = default;
64+
Generic& operator=(Generic&& _other);
7165

7266
/// Returns the underlying object, necessary for the serialization to work.
7367
const ReflectionType& reflection() const { return value_; };
7468

7569
/// Casts the underlying value to an rfl::Generic::Array or returns an
7670
/// rfl::Error, if the underlying value is not an rfl::Generic::Array.
77-
Result<Array> to_array() const noexcept {
78-
if (const auto* ptr = std::get_if<Array>(&value_)) {
79-
return *ptr;
80-
} else {
81-
return Error(
82-
"rfl::Generic: Could not cast the underlying value to an "
83-
"rfl::Generic::Array.");
84-
}
85-
}
71+
Result<Array> to_array() const noexcept;
8672

8773
/// Casts the underlying value to a boolean or returns an rfl::Error, if the
8874
/// underlying value is not a boolean.
89-
Result<bool> to_bool() const noexcept {
90-
if (const auto* ptr = std::get_if<bool>(&value_)) {
91-
return *ptr;
92-
} else {
93-
return Error(
94-
"rfl::Generic: Could not cast the underlying value to a boolean.");
95-
}
96-
}
75+
Result<bool> to_bool() const noexcept;
9776

9877
/// Casts the underlying value to a double or returns an rfl::Error, if the
9978
/// underlying value is not a double.
100-
Result<double> to_double() const noexcept {
101-
if (const auto* ptr = std::get_if<double>(&value_)) {
102-
return *ptr;
103-
} else {
104-
return Error(
105-
"rfl::Generic: Could not cast the underlying value to a double.");
106-
}
107-
}
79+
Result<double> to_double() const noexcept;
10880

10981
/// Casts the underlying value to an integer or returns an rfl::Error, if the
11082
/// underlying value is not an integer.
111-
Result<int> to_int() const noexcept {
112-
if (const auto* ptr = std::get_if<int>(&value_)) {
113-
return *ptr;
114-
} else {
115-
return Error(
116-
"rfl::Generic: Could not cast the underlying value to an integer.");
117-
}
118-
}
83+
Result<int> to_int() const noexcept;
11984

12085
/// Casts the underlying value to an rfl::Generic::Object or returns an
12186
/// rfl::Error, if the underlying value is not an rfl::Generic::Object.
122-
Result<Object> to_object() const noexcept {
123-
if (const auto* ptr = std::get_if<Object>(&value_)) {
124-
return *ptr;
125-
} else {
126-
return Error(
127-
"rfl::Generic: Could not cast the underlying value to an "
128-
"rfl::Generic::Object.");
129-
}
130-
}
87+
Result<Object> to_object() const noexcept;
13188

13289
/// Casts the underlying value to a string or returns an rfl::Error, if the
13390
/// underlying value is not a string.
134-
Result<std::string> to_string() const noexcept {
135-
if (const auto* ptr = std::get_if<std::string>(&value_)) {
136-
return *ptr;
137-
} else {
138-
return Error(
139-
"rfl::Generic: Could not cast the underlying value to a string.");
140-
}
141-
}
91+
Result<std::string> to_string() const noexcept;
14292

14393
private:
14494
ReflectionType value_;

include/rfl/Result.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
#include <array>
55
#include <iostream>
66
#include <optional>
7-
#include <ranges>
8-
#include <span>
97
#include <stdexcept>
108
#include <string>
11-
#include <tuple>
129
#include <type_traits>
13-
#include <variant>
14-
#include <vector>
1510

1611
#include "internal/is_array.hpp"
1712
#include "internal/to_std_array.hpp"

include/rfl/Size.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include "Ref.hpp"
77
#include "Result.hpp"
8-
#include "parsing/Parser.hpp"
98
#include "parsing/schema/ValidationType.hpp"
109

1110
namespace rfl {

include/rfl/internal/Memoization.hpp

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)