Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #ifndef INCLUDED_LOTUSWORDPRO_SOURCE_FILTER_CLONE_HXX
11 : #define INCLUDED_LOTUSWORDPRO_SOURCE_FILTER_CLONE_HXX
12 :
13 : namespace detail
14 : {
15 :
16 : template<typename T>
17 : struct has_clone
18 : {
19 : template<typename U, U x>
20 : struct test;
21 :
22 : typedef char yes;
23 : typedef struct { char a[2]; } no;
24 :
25 : template<typename U>
26 : static yes& check_sig(U*, test<U* (U::*)() const, &U::clone>* = 0);
27 : template<typename U>
28 : static no& check_sig(...);
29 :
30 : enum
31 : {
32 : value = sizeof(check_sig<T>(0)) == sizeof(yes)
33 : };
34 : };
35 :
36 : template<typename T, bool HasClone>
37 : struct cloner
38 : {
39 270 : static T* clone(T* const other)
40 : {
41 270 : return new T(*other);
42 : }
43 : };
44 :
45 : template<typename T>
46 : struct cloner<T, true>
47 : {
48 448 : static T* clone(T* const other)
49 : {
50 448 : return other->clone();
51 : }
52 : };
53 :
54 : }
55 :
56 : /** Creates a new copy of the passed object.
57 : If other is 0, just returns 0. Otherwise, if other has function
58 : named clone with signature T* (T::*)() const, the function is called.
59 : Otherwise, copy constructor is used.
60 :
61 : @returns 0 or newly allocated object
62 : */
63 : template<typename T>
64 718 : T* clone(T* const other)
65 : {
66 718 : return other ? ::detail::cloner<T, ::detail::has_clone<T>::value>::clone(other) : 0;
67 : }
68 :
69 : #endif // INCLUDED_LOTUSWORDPRO_SOURCE_FILTER_CLONE_HXX
70 :
71 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|