Skip to content

Commit

Permalink
cpp example (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrissan authored May 31, 2024
1 parent cb8b0cc commit b56954d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ go.work
/target
/.tmp

/.idea
/.idea

/internal/tlcodegen/test/gen/test_cpp
internal/tlcodegen/test/gen/cpp/
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ qtpl:
qtc -dir=./internal -skipLineComments; \
fi

# TODO: in progress...
#.PHONY: cpp
#cpp: build
# @./target/bin/tlgen -language=cpp -v \
# --outdir=./test/cpp \
# --basicPkgPath=gitlab.mvk.com/go/vkgo/pkg/basictl \
# ./test/cpp.tl
# g++ -o test/test_cpp test/test_cpp.cpp test/cpp/all.cpp -std=c++17 -O3 -Wno-noexcept-type -g -Wall -Wextra -Werror=return-type -Wno-unused-parameter
.PHONY: cpp
cpp: build
@./target/bin/tlgen -language=cpp -v \
--outdir=./$(GEN_PATH)/cpp \
--generateRPCCode=false \
--basicPkgPath=$(BASIC_TL_PATH) \
./$(TLS_PATH)/cpp.tl
g++ -o $(GEN_PATH)/test_cpp $(GEN_PATH)/test_cpp.cpp $(GEN_PATH)/cpp/all.cpp -std=c++17 -O3 -Wno-noexcept-type -g -Wall -Wextra -Werror=return-type -Wno-unused-parameter
40 changes: 40 additions & 0 deletions internal/tlcodegen/test/gen/test_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include "cpp/a.top2.hpp"
#include "cpp/a.top3.hpp"

std::string to_hex(const uint8_t *data, size_t count) {
static const char hexdigits[] = "0123456789abcdef";

std::string result(count * 2, char());
for (size_t i = 0; i != count; ++i) {
uint8_t ch = data[i];
result[i * 2] = hexdigits[(ch >> 4) & 0xf];
result[i * 2 + 1] = hexdigits[ch & 0xf];
}
return result;
}

int main() {
basictl::tl_ostream_string str;

tl2::a::Top2 top2;

top2.write(str);
auto & buf = str.get_buffer();
std::cout << top2.tl_name() << ": " << to_hex(reinterpret_cast<const uint8_t *>(buf.data()), buf.size()) << std::endl;

tl2::a::Top3 top3;
top3.n = 2;
// top3.a.a = "Hi!";
top3.c.b.a.a.push_back(5);
top3.c.b.a.a.push_back(7);

basictl::tl_ostream_string str2;

top3.write(str2);

auto & buf2 = str2.get_buffer();
std::cout << top3.tl_name() << ": " << to_hex(reinterpret_cast<const uint8_t *>(buf2.data()), buf2.size()) << std::endl;

return 0;
}
7 changes: 7 additions & 0 deletions internal/tlcodegen/test/tls/cpp.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

pair {X:Type} {Y:Type} a:X b:Y = Pair X Y;

a.inner {I:#} a:I*[int] = a.Inner I;
a.middle {W:#} {P:Type} {Q:Type} a:(a.inner W) b:(pair P Q) = a.Middle W P Q;
a.top2 n:# m:# c:(a.middle m (pair (a.inner n) (a.inner n)) (a.Inner 3)) d:(a.Inner 4) = a.Top2;
a.top3 n:# m:# c:(a.middle 5 (a.inner n) (a.Inner 3)) = a.Top3;
1 change: 1 addition & 0 deletions internal/tlcodegen/tlgen_kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (gen *Gen2) getType(lrc LocalResolveContext, t tlast.TypeRef, unionParent *
exist, ok := gen.generatedTypes[canonicalName]
if !ok {
// log.Printf("adding canonical type: %s\n", canonicalName)
//log.Printf(" half resolved type: %v\n", halfResolved)
gen.generatedTypes[canonicalName] = kernelType
gen.generatedTypesList = append(gen.generatedTypesList, kernelType)
// We added our type already, so others can reference it
Expand Down
11 changes: 7 additions & 4 deletions internal/tlcodegen/type_rw_tuple_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ func (trw *TypeRWBrackets) cppTypeStringInNamespaceHalfResolved(bytesVersion boo
// trw.dictKeyField.t.CPPTypeStringInNamespace(bytesVersion, hppInc, trw.dictKeyField.resolvedType, halfResolve),
// trw.dictValueField.t.CPPTypeStringInNamespace(bytesVersion, hppInc, trw.dictValueField.resolvedType, halfResolve))
//}
if trw.vectorLike || trw.dynamicSize {
if trw.vectorLike {
return fmt.Sprintf("std::vector<%s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]))
}
if halfResolved.Args[1].Name != "" {
return fmt.Sprintf("std::array<%s, %s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]), halfResolved.Args[1].Name)
if trw.dynamicSize {
return fmt.Sprintf("std::vector<%s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[1]))
}
if halfResolved.Args[0].Name != "" {
return fmt.Sprintf("std::array<%s, %s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[1]), halfResolved.Args[0].Name)
}
return fmt.Sprintf("std::array<%s, %d>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]), trw.size)
return fmt.Sprintf("std::array<%s, %d>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[1]), trw.size)
}

func (trw *TypeRWBrackets) cppDefaultInitializer(halfResolved HalfResolvedArgument, halfResolve bool) string {
Expand Down

0 comments on commit b56954d

Please sign in to comment.