~kyrofa/unity-scope-libertine/add_debian_packaging

« back to all changes in this revision

Viewing changes to internal/launchpad.net/go-unityscopes/v2/smartptr_helper.h

  • Committer: Kyle Fazzari
  • Date: 2015-07-27 18:38:30 UTC
  • Revision ID: kyle@canonical.com-20150727183830-390on30ba491p1aq
Vendor dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef UNITYSCOPE_SMARTPTR_HELPER_H
 
2
#define UNITYSCOPE_SMARTPTR_HELPER_H
 
3
 
 
4
#include <memory>
 
5
 
 
6
#include "shim.h"
 
7
 
 
8
namespace gounityscopes {
 
9
namespace internal {
 
10
 
 
11
template<typename T> inline void init_const_ptr(SharedPtrData data,  std::shared_ptr<T const> v) {
 
12
    typedef std::shared_ptr<T const> Ptr;
 
13
    static_assert(sizeof(SharedPtrData) >= sizeof(Ptr),
 
14
                  "std::shared_ptr is larger than expected");
 
15
    Ptr *ptr = new(reinterpret_cast<void*>(data)) Ptr();
 
16
    *ptr = v;
 
17
}
 
18
 
 
19
template<typename T> inline void init_ptr(SharedPtrData data, std::shared_ptr<T> v) {
 
20
    typedef std::shared_ptr<T> Ptr;
 
21
    static_assert(sizeof(SharedPtrData) >= sizeof(Ptr),
 
22
                  "std::shared_ptr is larger than expected");
 
23
    Ptr *ptr = new(reinterpret_cast<void*>(data)) Ptr();
 
24
    *ptr = v;
 
25
}
 
26
 
 
27
template<typename T> inline std::shared_ptr<T> get_ptr(SharedPtrData data) {
 
28
    typedef std::shared_ptr<T> Ptr;
 
29
    Ptr *ptr = reinterpret_cast<Ptr*>(data);
 
30
    return *ptr;
 
31
}
 
32
 
 
33
template<typename T> inline void destroy_ptr(SharedPtrData data) {
 
34
    typedef std::shared_ptr<T> Ptr;
 
35
    if (!(data[0] == 0 && data[1] == 0)) {
 
36
        Ptr *ptr = reinterpret_cast<Ptr*>(data);
 
37
        ptr->~Ptr();
 
38
    }
 
39
    data[0] = data[1] = 0;
 
40
}
 
41
 
 
42
template<typename T> inline void copy_ptr(SharedPtrData dest_data, SharedPtrData src_data) {
 
43
    typedef std::shared_ptr<T> Ptr;
 
44
    Ptr *dest = reinterpret_cast<Ptr*>(dest_data);
 
45
    Ptr *src = reinterpret_cast<Ptr*>(src_data);
 
46
    *dest = *src;
 
47
}
 
48
 
 
49
}
 
50
}
 
51
 
 
52
#endif