aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/shared/ios_prop.hpp
blob: 0a820e126bfba8783dba71839afdfb6c0e2abc29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_SRC_LOCALE_IOS_PROP_HPP
#define BOOST_SRC_LOCALE_IOS_PROP_HPP
#include <ios>

namespace boost {
    namespace locale {
        namespace impl {
       
            template<typename Property> 
            class ios_prop {
            public:
                static void set(Property const &prop,std::ios_base &ios)
                {
                    int id=get_id();
                    if(ios.pword(id)==0) {
                        ios.pword(id) = new Property(prop);
                        ios.register_callback(callback,id);
                    }
                    else if(ios.pword(id)==invalid) {
                        ios.pword(id) = new Property(prop);
                    }
                    else {
                        *static_cast<Property *>(ios.pword(id))=prop;
                    }
                }
                
                static Property &get(std::ios_base &ios)
                {
                    int id=get_id();
                    if(!has(ios))
                        set(Property(),ios);
                    return *static_cast<Property *>(ios.pword(id));
                }
                
                static bool has(std::ios_base &ios)
                {
                    int id=get_id();
                    if(ios.pword(id)==0 || ios.pword(id)==invalid)
                        return false;
                    return true;
                }

                static void unset(std::ios_base &ios)
                {
                    if(has(ios)) {
                        int id=get_id();
                        Property *p=static_cast<Property *>(ios.pword(id));
                        delete p;
                        ios.pword(id)=invalid;
                    }
                }
                static void global_init()
                {
                    get_id();
                }
            private:
                static void * const invalid;
                
                static void callback(std::ios_base::event ev,std::ios_base &ios,int id)
                {
                    switch(ev) {
                    case std::ios_base::erase_event:
                        if(!has(ios))
                            break;
                        delete reinterpret_cast<Property *>(ios.pword(id));
                        break;
                    case std::ios_base::copyfmt_event:
                        if(ios.pword(id)==invalid || ios.pword(id)==0)
                            break;
                        ios.pword(id)=new Property(*reinterpret_cast<Property *>(ios.pword(id)));
                        break;
                    case std::ios_base::imbue_event:
                        if(ios.pword(id)==invalid || ios.pword(id)==0)
                            break;
                        reinterpret_cast<Property *>(ios.pword(id))->on_imbue(); 
                        break;
                        
                    default: ;
                    }
                }
                static int get_id()
                {
                    static int id = std::ios_base::xalloc();
                    return id;
                }
            };

            template<typename Property>
            void * const ios_prop<Property>::invalid = (void *)(-1);



        }
    }
}



#endif

// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4