Skip to content

Commit db9ae72

Browse files
committed
Add custom object support
1 parent be7379d commit db9ae72

File tree

84 files changed

+724
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+724
-280
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Genbox.FastData.Generator.CPlusPlus.Shared;
3+
4+
namespace Genbox.FastData.Generator.CPlusPlus.Tests;
5+
6+
// ReSharper disable once UnusedType.Global
7+
[SuppressMessage("Maintainability", "CA1515:Consider making public types internal")]
8+
public sealed class CPlusPlusContext
9+
{
10+
public CPlusPlusContext()
11+
{
12+
string rootDir = Path.Combine(Path.GetTempPath(), "FastData", "CPlusPlus");
13+
Directory.CreateDirectory(rootDir);
14+
Compiler = new CPlusPlusCompiler(false, rootDir);
15+
}
16+
17+
public CPlusPlusCompiler Compiler { get; }
18+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Genbox.FastData.Enums;
2+
using Genbox.FastData.Generator.CPlusPlus.Internal.Framework;
3+
using Genbox.FastData.Generator.Extensions;
4+
using Genbox.FastData.Generator.Framework;
5+
using static Genbox.FastData.Generator.Helpers.FormatHelper;
6+
using static Genbox.FastData.InternalShared.Helpers.TestHelper;
7+
8+
namespace Genbox.FastData.Generator.CPlusPlus.Tests;
9+
10+
public class FeatureTests(CPlusPlusContext context) : IClassFixture<CPlusPlusContext>
11+
{
12+
[Fact]
13+
public async Task StructSupportTest()
14+
{
15+
int[] values = [1, 2, 3];
16+
const string id = nameof(StructSupportTest);
17+
18+
string genSource = FastDataGenerator.GenerateKeyed(values, [
19+
new X { Age = 1, Name = "Bob" },
20+
new X { Age = 2, Name = "Billy" },
21+
new X { Age = 3, Name = "Bibi" },
22+
], new FastDataConfig { StructureType = StructureType.Array }, CPlusPlusCodeGenerator.Create(new CPlusPlusCodeGeneratorConfig(id)));
23+
24+
await Verify(genSource)
25+
.UseFileName(id)
26+
.UseDirectory("Features")
27+
.DisableDiff();
28+
29+
CPlusPlusLanguageDef langDef = new CPlusPlusLanguageDef();
30+
TypeMap map = new TypeMap(langDef.TypeDefinitions, GeneratorEncoding.ASCII);
31+
32+
string testSource = $$"""
33+
#include <string>
34+
#include <iostream>
35+
36+
{{genSource}}
37+
38+
int main(int argc, char* argv[])
39+
{
40+
{{FormatList(values, x => $"""
41+
if (!{id}::contains({map.ToValueLabel(x)}))
42+
return false;
43+
""", "\n")}}
44+
45+
return 1;
46+
}
47+
""";
48+
49+
string executable = context.Compiler.Compile(id, testSource);
50+
Assert.Equal(1, RunProcess(executable));
51+
}
52+
53+
internal struct X
54+
{
55+
public int Age { get; set; }
56+
public string Name { get; set; }
57+
}
58+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// This file is auto-generated. Do not edit manually.
2+
// Structure: Array
3+
#pragma once
4+
#include <array>
5+
#include <cstdint>
6+
#include <limits>
7+
#include <string_view>
8+
9+
class StructSupportTest final
10+
{
11+
struct X {
12+
const int32_t Age;
13+
const std::u32string_view Name;
14+
15+
X(int32_t age, std::u32string_view name) : Age(age), Name(name) { }
16+
};
17+
static std::array<X, 3> values;
18+
19+
static constexpr std::array<int32_t, 3> keys = {
20+
1, 2, 3
21+
};
22+
23+
public:
24+
[[nodiscard]]
25+
static bool try_lookup(const int32_t key, X*& value) noexcept
26+
{
27+
if (key < 1 || key > 3)
28+
return false;
29+
30+
for (size_t i = 0; i < 3; i++)
31+
{
32+
if (keys[i] == key)
33+
{
34+
value = &values[i];
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
41+
[[nodiscard]]
42+
static bool contains(const int32_t key) noexcept
43+
{
44+
if (key < 1 || key > 3)
45+
return false;
46+
47+
for (size_t i = 0; i < 3; i++)
48+
{
49+
if (keys[i] == key)
50+
return true;
51+
}
52+
return false;
53+
}
54+
55+
static constexpr size_t item_count = 3;
56+
static constexpr int32_t min_value = 1;
57+
static constexpr int32_t max_value = 3;
58+
59+
public:
60+
StructSupportTest() = delete;
61+
StructSupportTest(const StructSupportTest&) = delete;
62+
StructSupportTest& operator=(const StructSupportTest&) = delete;
63+
StructSupportTest(StructSupportTest&&) = delete;
64+
StructSupportTest& operator=(StructSupportTest&&) = delete;
65+
};
66+
std::array<StructSupportTest::X, 3> StructSupportTest::values = {
67+
X(1, U"Bob"), X(2, U"Billy"), X(3, U"Bibi")
68+
};

Src/FastData.Generator.CPlusPlus.Tests/VectorTests.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
21
using Genbox.FastData.Enums;
32
using Genbox.FastData.Generator.CPlusPlus.Internal.Framework;
4-
using Genbox.FastData.Generator.CPlusPlus.Shared;
53
using Genbox.FastData.Generator.Extensions;
64
using Genbox.FastData.Generator.Framework;
75
using Genbox.FastData.Generators;
@@ -13,7 +11,7 @@
1311

1412
namespace Genbox.FastData.Generator.CPlusPlus.Tests;
1513

16-
public class VectorTests(VectorTests.CPlusPlusContext context) : IClassFixture<VectorTests.CPlusPlusContext>
14+
public class VectorTests(CPlusPlusContext context) : IClassFixture<CPlusPlusContext>
1715
{
1816
[Theory]
1917
[ClassData(typeof(TestVectorTheoryData))]
@@ -51,19 +49,4 @@ int main(int argc, char* argv[])
5149

5250
Assert.Equal(1, RunProcess(executable));
5351
}
54-
55-
[SuppressMessage("Design", "CA1034:Nested types should not be visible")]
56-
[SuppressMessage("Maintainability", "CA1515:Consider making public types internal")]
57-
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
58-
public sealed class CPlusPlusContext
59-
{
60-
public CPlusPlusContext()
61-
{
62-
string rootDir = Path.Combine(Path.GetTempPath(), "FastData", "CPlusPlus");
63-
Directory.CreateDirectory(rootDir);
64-
Compiler = new CPlusPlusCompiler(false, rootDir);
65-
}
66-
67-
public CPlusPlusCompiler Compiler { get; }
68-
}
6952
}

Src/FastData.Generator.CPlusPlus.Tests/Vectors/ArrayStructure_Byte_3.verified.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88

99
class ArrayStructure_Byte_3 final
1010
{
11-
static constexpr std::array<uint8_t, 3> values = {
12-
42, 42, 42
13-
};
11+
12+
static std::array<uint8_t, 3> values;
1413

1514
static constexpr std::array<uint8_t, 3> keys = {
1615
0, 1, std::numeric_limits<uint8_t>::max()
1716
};
1817

1918
public:
2019
[[nodiscard]]
21-
static bool try_lookup(const uint8_t key, uint8_t& value) noexcept
20+
static bool try_lookup(const uint8_t key, uint8_t*& value) noexcept
2221
{
2322
if (key < 0 || key > std::numeric_limits<uint8_t>::max())
2423
return false;
@@ -27,7 +26,7 @@ public:
2726
{
2827
if (keys[i] == key)
2928
{
30-
value = values[i];
29+
value = &values[i];
3130
return true;
3231
}
3332
}
@@ -58,4 +57,7 @@ public:
5857
ArrayStructure_Byte_3& operator=(const ArrayStructure_Byte_3&) = delete;
5958
ArrayStructure_Byte_3(ArrayStructure_Byte_3&&) = delete;
6059
ArrayStructure_Byte_3& operator=(ArrayStructure_Byte_3&&) = delete;
61-
};
60+
};
61+
std::array<uint8_t, 3> ArrayStructure_Byte_3::values = {
62+
42, 42, 42
63+
};

Src/FastData.Generator.CPlusPlus.Tests/Vectors/ArrayStructure_Char_3.verified.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88

99
class ArrayStructure_Char_3 final
1010
{
11-
static constexpr std::array<uint8_t, 3> values = {
12-
42, 42, 42
13-
};
11+
12+
static std::array<uint8_t, 3> values;
1413

1514
static constexpr std::array<char, 3> keys = {
1615
0, 97, 127
1716
};
1817

1918
public:
2019
[[nodiscard]]
21-
static bool try_lookup(const char key, uint8_t& value) noexcept
20+
static bool try_lookup(const char key, uint8_t*& value) noexcept
2221
{
2322
if (key < 0 || key > 127)
2423
return false;
@@ -27,7 +26,7 @@ public:
2726
{
2827
if (keys[i] == key)
2928
{
30-
value = values[i];
29+
value = &values[i];
3130
return true;
3231
}
3332
}
@@ -58,4 +57,7 @@ public:
5857
ArrayStructure_Char_3& operator=(const ArrayStructure_Char_3&) = delete;
5958
ArrayStructure_Char_3(ArrayStructure_Char_3&&) = delete;
6059
ArrayStructure_Char_3& operator=(ArrayStructure_Char_3&&) = delete;
61-
};
60+
};
61+
std::array<uint8_t, 3> ArrayStructure_Char_3::values = {
62+
42, 42, 42
63+
};

Src/FastData.Generator.CPlusPlus.Tests/Vectors/ArrayStructure_Double_4.verified.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88

99
class ArrayStructure_Double_4 final
1010
{
11-
static constexpr std::array<uint8_t, 4> values = {
12-
42, 42, 42, 42
13-
};
11+
12+
static std::array<uint8_t, 4> values;
1413

1514
static constexpr std::array<double, 4> keys = {
1615
std::numeric_limits<double>::lowest(), 0.0, 1.0, std::numeric_limits<double>::max()
1716
};
1817

1918
public:
2019
[[nodiscard]]
21-
static bool try_lookup(const double key, uint8_t& value) noexcept
20+
static bool try_lookup(const double key, uint8_t*& value) noexcept
2221
{
2322
if (key < std::numeric_limits<double>::lowest() || key > std::numeric_limits<double>::max())
2423
return false;
@@ -27,7 +26,7 @@ public:
2726
{
2827
if (keys[i] == key)
2928
{
30-
value = values[i];
29+
value = &values[i];
3130
return true;
3231
}
3332
}
@@ -58,4 +57,7 @@ public:
5857
ArrayStructure_Double_4& operator=(const ArrayStructure_Double_4&) = delete;
5958
ArrayStructure_Double_4(ArrayStructure_Double_4&&) = delete;
6059
ArrayStructure_Double_4& operator=(ArrayStructure_Double_4&&) = delete;
61-
};
60+
};
61+
std::array<uint8_t, 4> ArrayStructure_Double_4::values = {
62+
42, 42, 42, 42
63+
};

Src/FastData.Generator.CPlusPlus.Tests/Vectors/ArrayStructure_Int16_5.verified.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88

99
class ArrayStructure_Int16_5 final
1010
{
11-
static constexpr std::array<uint8_t, 5> values = {
12-
42, 42, 42, 42, 42
13-
};
11+
12+
static std::array<uint8_t, 5> values;
1413

1514
static constexpr std::array<int16_t, 5> keys = {
1615
std::numeric_limits<int16_t>::lowest(), -1, 0, 1, std::numeric_limits<int16_t>::max()
1716
};
1817

1918
public:
2019
[[nodiscard]]
21-
static bool try_lookup(const int16_t key, uint8_t& value) noexcept
20+
static bool try_lookup(const int16_t key, uint8_t*& value) noexcept
2221
{
2322
if (key < std::numeric_limits<int16_t>::lowest() || key > std::numeric_limits<int16_t>::max())
2423
return false;
@@ -27,7 +26,7 @@ public:
2726
{
2827
if (keys[i] == key)
2928
{
30-
value = values[i];
29+
value = &values[i];
3130
return true;
3231
}
3332
}
@@ -58,4 +57,7 @@ public:
5857
ArrayStructure_Int16_5& operator=(const ArrayStructure_Int16_5&) = delete;
5958
ArrayStructure_Int16_5(ArrayStructure_Int16_5&&) = delete;
6059
ArrayStructure_Int16_5& operator=(ArrayStructure_Int16_5&&) = delete;
61-
};
60+
};
61+
std::array<uint8_t, 5> ArrayStructure_Int16_5::values = {
62+
42, 42, 42, 42, 42
63+
};

0 commit comments

Comments
 (0)