-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormats.cpp
96 lines (74 loc) · 3.11 KB
/
Formats.cpp
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
////////////////////////////////////////////////////////////////////////////////
//! \file Formats.cpp
//! \brief The Formats class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Formats.hpp"
#include <Core/Functor.hpp>
#include <Core/Algorithm.hpp>
////////////////////////////////////////////////////////////////////////////////
//! Constants.
const tstring Formats::STRING = TXT("%s");
const tstring Formats::DATETIME = TXT("%t");
const tstring Formats::BYTES = TXT("B");
const tstring Formats::K_BYTES = TXT("KB");
const tstring Formats::M_BYTES = TXT("MB");
const tstring Formats::G_BYTES = TXT("GB");
const tstring Formats::KB_AS_MB = TXT("KB2MB");
const tstring Formats::KB_AS_GB = TXT("KB2GB");
const tstring Formats::MB_AS_GB = TXT("MB2GB");
const tstring Formats::DEFAULT = STRING;
////////////////////////////////////////////////////////////////////////////////
//! Global variables.
//! The default collection of formats.
const Formats Formats::g_default;
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
Formats::Formats()
: m_collection()
{
m_collection.push_back(FormatMapping( TXT("%s"), TXT("String") ));
m_collection.push_back(FormatMapping( TXT("%t"), TXT("Date & Time") ));
m_collection.push_back(FormatMapping( TXT("B"), TXT("Bytes (B)") ));
m_collection.push_back(FormatMapping( TXT("KB"), TXT("Kilobytes (KB)") ));
m_collection.push_back(FormatMapping( TXT("MB"), TXT("Megabytes (MB)") ));
m_collection.push_back(FormatMapping( TXT("GB"), TXT("Gigabytes (GB)") ));
m_collection.push_back(FormatMapping( TXT("KB2MB"), TXT("KB as MB") ));
m_collection.push_back(FormatMapping( TXT("KB2GB"), TXT("KB as GB") ));
m_collection.push_back(FormatMapping( TXT("MB2GB"), TXT("MB as GB") ));
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
Formats::~Formats()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Match a format on its value.
CORE_DEFINE_PREDICATE_EX(FormatEquals, tstring, tstring, format, const Formats::FormatMapping&, rhs)
{
return rhs.first == format;
}
CORE_END_PREDICATE_EX
////////////////////////////////////////////////////////////////////////////////
//! Get the display name for a format.
tstring Formats::getDisplayName(const tstring& format) const
{
ConstIter it = Core::find_if(m_collection, FormatEquals(format));
ASSERT(it != m_collection.end());
return it->second;
}
////////////////////////////////////////////////////////////////////////////////
//! Match a format on its display name.
CORE_DEFINE_PREDICATE_EX(DisplayNameEquals, tstring, tstring, displayName, const Formats::FormatMapping&, rhs)
{
return rhs.second == displayName;
}
CORE_END_PREDICATE_EX
////////////////////////////////////////////////////////////////////////////////
//! Get the format from the display name.
tstring Formats::getFormat(const tstring& displayName) const
{
ConstIter it = Core::find_if(m_collection, DisplayNameEquals(displayName));
ASSERT(it != m_collection.end());
return it->first;
}