Skip to content

Commit 5aad762

Browse files
shaerthomasshaerthomas
shaerthomas
authored and
shaerthomas
committed
Created 'Apply' Node which will apply a std::function to two arguments, allowing for the user to submit whatever function they want to provide on the operator such as standard ones like std::plus. Also extended the compile time string by giving it a constexpr compatible equality and plus operator (string concating)
1 parent 8c27214 commit 5aad762

File tree

5 files changed

+75
-35
lines changed

5 files changed

+75
-35
lines changed

lookuptable.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
#include "specialtypes.h"
23

34
/*
45
Compile time datastructure used for mapping variable names to values.
@@ -15,29 +16,7 @@ Registering new identifiers (and values)
1516

1617

1718

18-
/*
19-
Compatible string literal compile time type (used for ids)
20-
21-
https://stackoverflow.com/questions/62853609/understanding-user-defined-string-literals-addition-for-c20
22-
*/
23-
template<size_t N>
24-
struct string_literal
25-
{
26-
std::array<char, N> arr_;
27-
28-
constexpr string_literal(const char(&in)[N]) : arr_{}
29-
{
30-
std::copy(in, in + N, arr_.begin());
31-
}
32-
33-
friend std::ostream& operator<<(std::ostream& os, const string_literal& dt)
34-
{
35-
//std::string lol(arr_;
36-
os << std::string(dt.arr_.data());
37-
return os;
38-
}
3919

40-
};
4120

4221

4322

main

-67.2 KB
Binary file not shown.

main.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,28 @@ int main(int argc, char* argv[])
8080
runTests();
8181
/*
8282
TODO.
83-
Make it so that you don't have to explicitly pass in a linkedlist item to Execute
83+
Make it so that you don't have to explicitly pass in a linkedlist item to Execute.
84+
85+
Make it so that the Apply function takes in any amount of arguments
8486
*/
8587

8688

8789

8890
using code = Execute<LinkedListEmptyNode,
8991
Assign<"x", ValNum<6>>,
90-
Assign<"y", BinOpPlus<ValNum<2>, Var<"x">>>,
91-
Assign<"z", BinOpPlus<BinOpPlus<Var<"y">, ValNum<10>>, ValNum<7>>>,
92-
Assign<"a", ValStr<"Hello World!">>>;
92+
Assign<"y", Apply<std::plus<>, ValNum<2>, Var<"x">>>,
93+
Assign<"z", Apply<std::plus<>, Apply<std::multiplies<>, Var<"y">, ValNum<10>>, ValNum<7>>>,
94+
Assign<"text", ValStr<"Hello ">>,
95+
Assign<"text2", Apply<std::plus<>, Var<"text">, ValStr<"World!">>>>;
9396

9497
static_assert(LinkedListGetValue<code::values, "x">::value == 6);
9598
static_assert(LinkedListGetValue<code::values, "y">::value == 8);
96-
static_assert(LinkedListGetValue<code::values, "z">::value == 25);
97-
std::cout << LinkedListGetValue<code::values, "a">::value << std::endl;
98-
//static_assert(LinkedListGetValue<code::values, "z">::value == 25);
99+
static_assert(LinkedListGetValue<code::values, "z">::value == 87);
100+
constexpr string_literal teststring = "Hello World!";
101+
102+
103+
static_assert(LinkedListGetValue<code::values, "text2">::value == teststring);
99104

100-
// std::cout << LinkedListGetValue<code::values, "x">::value << std::endl;
101-
// std::cout << LinkedListGetValue<code::values, "y">::value << std::endl;
102-
// std::cout << LinkedListGetValue<code::values, "z">::value << std::endl;
103105

104106

105107

node.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ struct Var {
4646
};
4747

4848

49-
template<typename A, typename B>
50-
struct BinOpPlus {
49+
template<typename Func, typename A, typename B>
50+
struct Apply {
5151

5252
template<typename SymbolTable>
5353
static const constexpr auto getValue() {
54-
return A::template getValue<SymbolTable>() + B::template getValue<SymbolTable>();
54+
return Func{}(A::template getValue<SymbolTable>(), B::template getValue<SymbolTable>());
5555
}
5656
};
5757

specialtypes.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
3+
/*
4+
Compatible string literal compile time type (used for ids)
5+
6+
https://stackoverflow.com/questions/62853609/understanding-user-defined-string-literals-addition-for-c20
7+
8+
*/
9+
10+
11+
#include <array>
12+
#include <algorithm>
13+
14+
15+
16+
template<size_t N>
17+
struct string_literal
18+
{
19+
std::array<char, N> arr_;
20+
21+
constexpr string_literal(const char(&in)[N]) : arr_{}
22+
{
23+
std::copy(in, in + N, arr_.begin());
24+
}
25+
26+
constexpr string_literal(std::array<char, N> a) : arr_{a}
27+
{
28+
}
29+
30+
friend std::ostream& operator<<(std::ostream& os, const string_literal& dt)
31+
{
32+
os << std::string(dt.arr_.data());
33+
return os;
34+
}
35+
36+
37+
38+
};
39+
40+
template<template <size_t> typename String1, size_t StringSize1,
41+
template <size_t> typename String2, size_t StringSize2>
42+
constexpr bool operator==(const String1<StringSize1>& a, const String2<StringSize2>& b) {
43+
return a.arr_ == b.arr_;
44+
}
45+
46+
template<template <size_t> typename String1, size_t StringSize1,
47+
template <size_t> typename String2, size_t StringSize2>
48+
constexpr string_literal<(StringSize1 + StringSize2)-1> operator+(const String1<StringSize1>& a, const String2<StringSize2>& b) {
49+
50+
std::array<char, StringSize1 + StringSize2-1> temp;
51+
52+
std::copy_n(a.arr_.begin(), StringSize1-1 , std::begin(temp));
53+
std::copy_n(b.arr_.begin(), StringSize2 ,std::begin(temp) + StringSize1 - 1);
54+
55+
56+
string_literal<(StringSize1 + StringSize2)-1> temp2 = temp;
57+
58+
return temp2;
59+
}

0 commit comments

Comments
 (0)