include/am-moveable.h
author David Anderson <danderson@mozilla.com>
Tue Aug 27 00:49:33 2013 -0700 (2013-08-27)
changeset 12 cebfc7eeafa5
parent 11 cfe800908837
permissions -rw-r--r--
Added hash table tests.
dvander@10
     1
// vim: set sts=8 ts=2 sw=2 tw=99 et:
dvander@10
     2
//
dvander@10
     3
// Copyright (C) 2013, David Anderson and AlliedModders LLC
dvander@10
     4
// All rights reserved.
dvander@10
     5
// 
dvander@10
     6
// Redistribution and use in source and binary forms, with or without
dvander@10
     7
// modification, are permitted provided that the following conditions are met:
dvander@10
     8
// 
dvander@10
     9
//  * Redistributions of source code must retain the above copyright notice, this
dvander@10
    10
//    list of conditions and the following disclaimer.
dvander@10
    11
//  * Redistributions in binary form must reproduce the above copyright notice,
dvander@10
    12
//    this list of conditions and the following disclaimer in the documentation
dvander@10
    13
//    and/or other materials provided with the distribution.
dvander@10
    14
//  * Neither the name of AlliedModders LLC nor the names of its contributors
dvander@10
    15
//    may be used to endorse or promote products derived from this software
dvander@10
    16
//    without specific prior written permission.
dvander@10
    17
//
dvander@10
    18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
dvander@10
    19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
dvander@10
    20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
dvander@10
    21
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
dvander@10
    22
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
dvander@10
    23
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
dvander@10
    24
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dvander@10
    25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dvander@10
    26
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
dvander@10
    27
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
dvander@10
    28
// POSSIBILITY OF SUCH DAMAGE.
dvander@10
    29
dvander@10
    30
#ifndef _include_amtl_moveable_h_
dvander@10
    31
#define _include_amtl_moveable_h_
dvander@10
    32
dvander@10
    33
namespace ke {
dvander@10
    34
dvander@10
    35
// This is a feature in C++11, but since AM projects do not have access to
dvander@10
    36
// C++11 yet, we provide templates to implement move semantics. A class can
dvander@10
    37
// provide a constructor for (ke::Moveable<T> t) which containers will try
dvander@10
    38
// to use.
dvander@10
    39
//
dvander@10
    40
// When implementing a constructor that takes a Moveable, the object being
dvander@10
    41
// moved should be left in a state that is safe, since its destructor will
dvander@10
    42
// be called even though it has been moved.
dvander@10
    43
dvander@10
    44
template <typename T>
dvander@10
    45
struct Moveable
dvander@10
    46
{
dvander@10
    47
 public:
dvander@10
    48
  explicit Moveable(T &t)
dvander@10
    49
   : t_(t)
dvander@10
    50
  {
dvander@10
    51
  }
dvander@10
    52
dvander@10
    53
  T *operator ->() {
dvander@10
    54
    return &t_;
dvander@10
    55
  }
dvander@10
    56
  operator T &() {
dvander@10
    57
    return t_;
dvander@10
    58
  }
dvander@10
    59
dvander@10
    60
 private:
dvander@10
    61
  T &t_;
dvander@10
    62
};
dvander@10
    63
danderson@11
    64
template <typename T>
danderson@11
    65
static inline Moveable<T>
danderson@11
    66
Move(T &t)
danderson@11
    67
{
danderson@11
    68
  return Moveable<T>(t);
danderson@11
    69
}
danderson@11
    70
dvander@10
    71
} // namespace ke
dvander@10
    72
dvander@10
    73
#endif // _include_amtl_moveable_h_