Ejemplo n.º 1
0
 static inline D cast(U &&o) {
   D d;
   if (!o) return d;
   auto t = get_dtable(o);
   d.table_ = t;
   if (std::is_reference<U>::value) {
     auto c = get_table<concept_copy_construct::Table>(
         t->type_info, t, o.table());
     if (d.stored_in_heap()) {
       d.heap_data() = ::operator new(o.table()->size);
     }
     c->copy_construct(d.data(), o.data());
   } else {
     if (d.stored_in_heap()) {
       if (o.stored_in_heap()) {
         d.heap_data() = o.heap_data();
         ((S *)&o)->table_ = nullptr;
       } else {
         d.heap_data() = ::operator new(o.table()->size);
       }
     }
     if (o) {
       t->move_construct(d.data(), (void *)o.data());
     }
   }
   return d;
 }
Ejemplo n.º 2
0
void test_string()
{
  string default_construct;
  string charptr_construct("charptr construct");
  string copy_construct(charptr_construct);
  
  const string conststring("const string");
  string move_construct_const(conststring);

  string normalstring("normal string");

  string moveable_string("movable string");
  string move_construct_nonconst = std::move(moveable_string);
  print(move_construct_nonconst);

  string subscript_nonconst("normal string to be subscripted");
  const string subscript_const("const string to be subscripted");

  print(default_construct);
  print(charptr_construct);
  print(copy_construct);

  print(conststring);
  print(move_construct_const);

  print(normalstring);
  print(move_construct_nonconst);

  print(subscript_nonconst);
  print(subscript_const);

  //test subscripts
  auto test_subscript = [](string subscript) {
    printf("\ntesting subscript...\n");
    printf("%s\n", subscript.c_str());
    for (size_t i = 0; i < subscript.length(); ++i)
      printf("%c", subscript[i]);
    printf("\n");
  };

  auto test_all_subscript = [&test_subscript](initializer_list<string> list) {
    for (string s : list)
      test_subscript(s);
  };

  test_all_subscript({subscript_const, subscript_const});

  auto test_assignment = [](string left, string right) {
    printf("\ntesting assignment...\n");
    printf("left: %s \tright: %s\n", left.c_str(), right.c_str());
    left = right;
    printf("left: %s \tright: %s\n", left.c_str(), right.c_str());
  };
  test_assignment(charptr_construct, subscript_const);

  string init_list({ "hello", " world", " test" });
  print(init_list);
}