1 | #include "calendar_wrapper.hpp" |
---|
2 | #include "type.hpp" |
---|
3 | #include "calendar_type.hpp" |
---|
4 | #include "duration.hpp" |
---|
5 | #include "context.hpp" |
---|
6 | |
---|
7 | namespace xios { |
---|
8 | |
---|
9 | /// ////////////////////// Définitions ////////////////////// /// |
---|
10 | |
---|
11 | CCalendarWrapper::CCalendarWrapper(void) |
---|
12 | : CObjectTemplate<CCalendarWrapper>(), CCalendarWrapperAttributes() |
---|
13 | { /* Ne rien faire de plus */ } |
---|
14 | |
---|
15 | CCalendarWrapper::CCalendarWrapper(const StdString & id) |
---|
16 | : CObjectTemplate<CCalendarWrapper>(id), CCalendarWrapperAttributes() |
---|
17 | { /* Ne rien faire de plus */ } |
---|
18 | |
---|
19 | CCalendarWrapper::~CCalendarWrapper(void) |
---|
20 | {} |
---|
21 | |
---|
22 | //---------------------------------------------------------------- |
---|
23 | |
---|
24 | StdString CCalendarWrapper::GetName(void) { return StdString("calendar_wrapper"); } |
---|
25 | StdString CCalendarWrapper::GetDefName(void) { return StdString("calendar"); } |
---|
26 | ENodeType CCalendarWrapper::GetType(void) { return eCalendarWrapper; } |
---|
27 | |
---|
28 | //---------------------------------------------------------------- |
---|
29 | |
---|
30 | /*! |
---|
31 | \brief Extract the calendar from its wrapper |
---|
32 | \return the calendar |
---|
33 | */ |
---|
34 | boost::shared_ptr<CCalendar> CCalendarWrapper::getCalendar(bool checkValid /*= false*/) const |
---|
35 | { |
---|
36 | if (checkValid && !this->calendar) |
---|
37 | ERROR("CCalendarWrapper::getCalendar(bool checkValid = true)", << "The calendar was accessed before being created!"); |
---|
38 | return this->calendar; |
---|
39 | } |
---|
40 | |
---|
41 | const CDate& CCalendarWrapper::getInitDate() const |
---|
42 | { |
---|
43 | return getCalendar(true)->getInitDate(); |
---|
44 | } |
---|
45 | |
---|
46 | const CDate& CCalendarWrapper::getTimeOrigin() const |
---|
47 | { |
---|
48 | return getCalendar(true)->getTimeOrigin(); |
---|
49 | } |
---|
50 | |
---|
51 | //---------------------------------------------------------------- |
---|
52 | |
---|
53 | void CCalendarWrapper::setInitDate(const CDate& initDate) |
---|
54 | { |
---|
55 | getCalendar(true)->setInitDate(initDate); |
---|
56 | start_date = initDate.toString(); |
---|
57 | } |
---|
58 | |
---|
59 | void CCalendarWrapper::setTimeOrigin(const CDate& timeOrigin) |
---|
60 | { |
---|
61 | getCalendar(true)->setTimeOrigin(timeOrigin); |
---|
62 | time_origin = timeOrigin.toString(); |
---|
63 | } |
---|
64 | |
---|
65 | //---------------------------------------------------------------- |
---|
66 | |
---|
67 | /*! |
---|
68 | \brief Parse the calendar node |
---|
69 | \param [in] node xmld node corresponding to the calendar definition |
---|
70 | */ |
---|
71 | void CCalendarWrapper::parse(xml::CXMLNode& node) |
---|
72 | { |
---|
73 | SuperClass::parse(node); |
---|
74 | |
---|
75 | // Try to create the calendar |
---|
76 | createCalendar(); |
---|
77 | } |
---|
78 | |
---|
79 | /*! |
---|
80 | \brief Try to create the calendar from the parsed attributes |
---|
81 | */ |
---|
82 | void CCalendarWrapper::createCalendar(void) |
---|
83 | { |
---|
84 | // Create the calendar if possible |
---|
85 | if (calendar) |
---|
86 | { |
---|
87 | ERROR("CCalendarWrapper::createCalendar(void)", |
---|
88 | << "Error: the calendar can only be defined once!"); |
---|
89 | } |
---|
90 | else if (!type.isEmpty()) |
---|
91 | { |
---|
92 | #define DECLARE_CALENDAR(MType, eType) \ |
---|
93 | if (type.getValue() == type_attr::eType) \ |
---|
94 | calendar = boost::shared_ptr<CCalendar>(new C##MType##Calendar()); |
---|
95 | #include "calendar_type.conf" |
---|
96 | #undef DECLARE_CALENDAR |
---|
97 | |
---|
98 | if (!calendar) |
---|
99 | ERROR("CCalendarWrapper::parse(xml::CXMLNode& node)", |
---|
100 | << "[ type = " << type.getStringValue() << " ] " |
---|
101 | << "The calendar is not properly handled!"); |
---|
102 | |
---|
103 | // Set the timestep is available |
---|
104 | if (!timestep.isEmpty()) |
---|
105 | calendar->setTimeStep(timestep.getValue()); |
---|
106 | |
---|
107 | // Parse and set the start date if available |
---|
108 | if (!start_date.isEmpty()) |
---|
109 | calendar->setInitDate(CDate::FromString(start_date.getValue(), *calendar)); |
---|
110 | |
---|
111 | // Parse and set the time origin if available |
---|
112 | if (!time_origin.isEmpty()) |
---|
113 | calendar->setTimeOrigin(CDate::FromString(time_origin.getValue(), *calendar)); |
---|
114 | |
---|
115 | // Notify the context about the calendar |
---|
116 | CContext* context = CContext::getCurrent(); |
---|
117 | if (!context) |
---|
118 | ERROR("CCalendarWrapper::createCalendar(void)", << "Impossible to set the calendar: no current context available."); |
---|
119 | context->setCalendar(calendar); |
---|
120 | } |
---|
121 | else if (!start_date.isEmpty() || !time_origin.isEmpty()) |
---|
122 | { |
---|
123 | ERROR("CCalendarWrapper::parse(xml::CXMLNode& node)", |
---|
124 | << "The calendar type must be set before defining the start date or the time origin!"); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | /*! |
---|
129 | \brief Try to update the timestep of the calendar with the corresponding attribute |
---|
130 | */ |
---|
131 | void CCalendarWrapper::updateTimestep(void) |
---|
132 | { |
---|
133 | if (timestep.isEmpty()) |
---|
134 | { |
---|
135 | ERROR("CCalendarWrapper::updateTimestep(void)", |
---|
136 | << "Error: the timestep needs to be defined!"); |
---|
137 | } |
---|
138 | else if (calendar) |
---|
139 | calendar->setTimeStep(timestep.getValue()); |
---|
140 | } |
---|
141 | } |
---|