1 | |
---|
2 | // Copyright Eóin O'Callaghan 2006 - 2008. |
---|
3 | // Distributed under the Boost Software License, Version 1.0. |
---|
4 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | #pragma once |
---|
8 | |
---|
9 | #include "global/txml_ini_adapter.hpp" |
---|
10 | #include "global/txml_oarchive.hpp" |
---|
11 | #include "global/txml_iarchive.hpp" |
---|
12 | |
---|
13 | #include "halEvent.hpp" |
---|
14 | |
---|
15 | namespace hal |
---|
16 | { |
---|
17 | |
---|
18 | template <class T> |
---|
19 | class IniBase |
---|
20 | { |
---|
21 | public: |
---|
22 | IniBase(boost::filesystem::path location, std::string name, hal::ini_file& ini = hal::ini()) : |
---|
23 | adapter_(location, ini), |
---|
24 | name_(name) |
---|
25 | {} |
---|
26 | |
---|
27 | IniBase(std::string name, hal::ini_file& ini = hal::ini()) : |
---|
28 | adapter_(boost::filesystem::path(""), ini), |
---|
29 | name_(name) |
---|
30 | {} |
---|
31 | |
---|
32 | ~IniBase() |
---|
33 | { |
---|
34 | TXML_LOG(L"~IniBase()"); |
---|
35 | } |
---|
36 | |
---|
37 | void save_to_ini() |
---|
38 | { |
---|
39 | std::stringstream xml_data; |
---|
40 | { |
---|
41 | xml::txml_oarchive oxml(xml_data); |
---|
42 | T* pT = static_cast<T*>(this); |
---|
43 | oxml << boost::serialization::make_nvp(name_.c_str(), *pT); |
---|
44 | } |
---|
45 | |
---|
46 | adapter_.save_stream_data(xml_data); |
---|
47 | } |
---|
48 | |
---|
49 | template<typename P> |
---|
50 | void save_standalone(const P& location) |
---|
51 | { |
---|
52 | fs::ofstream ofs(location); |
---|
53 | |
---|
54 | xml::txml_oarchive oxml(ofs); |
---|
55 | T* pT = static_cast<T*>(this); |
---|
56 | oxml << boost::serialization::make_nvp(name_.c_str(), *pT); |
---|
57 | } |
---|
58 | |
---|
59 | template<typename P> |
---|
60 | bool load_standalone(const P& location) |
---|
61 | { |
---|
62 | try |
---|
63 | { |
---|
64 | fs::ifstream ifs(location); |
---|
65 | |
---|
66 | xml::txml_iarchive ixml(ifs); |
---|
67 | |
---|
68 | T* pT = static_cast<T*>(this); |
---|
69 | ixml >> boost::serialization::make_nvp(name_.c_str(), *pT); |
---|
70 | |
---|
71 | return true; |
---|
72 | |
---|
73 | } |
---|
74 | catch (const std::exception& e) |
---|
75 | { |
---|
76 | hal::event_log.post(boost::shared_ptr<hal::EventDetail>( |
---|
77 | new hal::EventXmlException(hal::from_utf8(e.what()), L"load_standalone"))); |
---|
78 | |
---|
79 | return false; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | bool load_from_ini() |
---|
84 | { |
---|
85 | std::stringstream xml_data; |
---|
86 | adapter_.load_stream_data(xml_data); |
---|
87 | |
---|
88 | try |
---|
89 | { |
---|
90 | |
---|
91 | xml::txml_iarchive ixml(xml_data); |
---|
92 | |
---|
93 | T* pT = static_cast<T*>(this); |
---|
94 | ixml >> boost::serialization::make_nvp(name_.c_str(), *pT); |
---|
95 | |
---|
96 | } |
---|
97 | catch (const std::exception& e) |
---|
98 | { |
---|
99 | hal::event_log.post(boost::shared_ptr<hal::EventDetail>( |
---|
100 | new hal::EventXmlException(hal::from_utf8(e.what()), hal::from_utf8(name_)))); |
---|
101 | |
---|
102 | return false; |
---|
103 | } |
---|
104 | |
---|
105 | return true; |
---|
106 | } |
---|
107 | |
---|
108 | private: |
---|
109 | hal::txml_ini_adapter adapter_; |
---|
110 | std::string name_; |
---|
111 | }; |
---|
112 | |
---|
113 | } |
---|
114 | |
---|