~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty

« back to all changes in this revision

Viewing changes to yo/containers/ccandmove.yo

  • Committer: Package Import Robot
  • Author(s): tony mancill, Frank B. Brokken, tony mancill
  • Date: 2012-02-28 00:50:21 UTC
  • mfrom: (1.1.19)
  • Revision ID: package-import@ubuntu.com-20120228005021-sz7nnodntkvgh7qf
Tags: 9.2.1-1
[ Frank B. Brokken ]
* New upstream release (using flexc++, reauthored polymorphic semantic
  values and unrestricted unions). Upstream release 9.2.0 is implied by
  this release.

[ tony mancill ]
* Set Standards-Version to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
tt(Union)'s copy and move constructors suffer from the same problem as
 
2
tt(Union)'s destructor does: the union does not know which is its currently
 
3
active field. But again: tt(Data) does, and by defining `extended' copy and
 
4
move constructors, also receiving a tt(Tag) argument, these extended
 
5
constructors can perform their proper initializations. The tt(Union)'s copy-
 
6
and move-constructors are deleted, and extended copy- and move constructors
 
7
are declared:
 
8
        verb(
 
9
    Union(Union const &other) = delete;
 
10
    Union &operator=(Union const &other) = delete;
 
11
 
 
12
    Union(Union const &other, Tag tag);
 
13
    Union(Union &&tmp, Tag tag);
 
14
        )
 
15
 
 
16
Shortly we'll encounter a situation where we must be able to initialize a
 
17
block of memory using an existing tt(Union) object. This task can be performed
 
18
by tt(copy) members, whose implementations are trivial, and which may be used
 
19
by the above constructors. They can be declared in tt(Union)'s private
 
20
section, and have identical parameter lists as the above constructors:
 
21
        verb(
 
22
    void copy(Union const &other, Tag tag);
 
23
    void copy(Union &&other, Tag tag);
 
24
        )
 
25
 
 
26
The constructors merely have to call these tt(copy) members:
 
27
        verb(
 
28
    inline Data::Union::Union(Union const &other, Tag tag)
 
29
    {
 
30
        copy(other, tag);
 
31
    }
 
32
 
 
33
    inline Data::Union::Union(Union &&tmp, Tag tag)
 
34
    {
 
35
        copy(std::move(tmp), tag);
 
36
    }
 
37
        )
 
38
 
 
39
Interestingly, no `initialization followed by assignment' happens here:
 
40
tt(d_union) has not been initialized in any way by the the time we reach the
 
41
statement blocks of the above constructors. But upon reaching the statement
 
42
blocks, tt(d_union) memory is merely raw memory. This is no problem, as the
 
43
tt(copy) members use placement new to initialize the tt(Union)'s memory:
 
44
        verb(
 
45
    void Data::Union::copy(Union const &other, Tag otag)
 
46
    {
 
47
        if (tag == INT)
 
48
            u_int = other.u_int;
 
49
        else
 
50
            new (this) string(other.u_string);
 
51
    }
 
52
 
 
53
    void Data::Union::copy(Union &&tmp, Tag tag)
 
54
    {
 
55
        if (tag == INT)
 
56
            u_int = tmp.u_int;
 
57
        else
 
58
            new (this) string(std::move(tmp.u_string));
 
59
    }
 
60
        )