Friday, April 3, 2015

Custom C++ Types


custom types wiki page

Which is the best smart pointer library for C++, that works everywhere and is fast? The standard smart pointers in C++11 can raise exceptions for some operations and this adds performance overhead. High performance libraries often use custom smart pointers because of this. Each custom smart pointer library has a similar API and behavior, and sometimes it is just the method names that differ. These implementation details get in the way of clear and readable code.

Rusthon solves this problem by allowing you to configure which smart pointers to use, for each block of code. This allows you to write code at a higher level, and control the low level details of translation using a single line with syntax('mytypes.json'). Code in that block will use the custom types and methods you define for: vectors, maps, shared and weak references. Outside of the block it defaults back to standard C++11 types.

Unreal Engine4

This example shows you how to configure the translator to use Unreal Engine smart pointers, string type, and template arrays. The translator configuration is at the top of the markdown, and redefines template and method names. Here is the output of main:

int main() {
 TArray<TSharedRef<Child>> children = {};
 auto p = TSharedRef<Parent>(
  (new Parent())->__init__(1000, children)
 );
 std::cout << TEXT("parent:");
 std::cout << p;std::cout << std::endl;
 auto c1 = make_child(p, 1);
 auto c2 = make_child(p, 20);
 auto c3 = make_child(p, 300);
 std::cout << TEXT("children:") << std::endl;
 std::cout << c1 << std::endl;
 std::cout << c2 << std::endl;
 std::cout << c3 << std::endl;
 std::cout << TEXT("calling foo") << std::endl;
 std::cout << c1->foo() << std::endl;
 std::cout << TEXT("calling bar") << std::endl;
 c1->bar();
 p.Reset();
 std::cout << c1->foo() << std::endl;
 c1->bar();
 return 0;
}

No comments:

Post a Comment