Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
059934fd
Commit
059934fd
authored
Dec 16, 2012
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename Active Formatter to TempFormatter.
parent
e0f85c44
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
29 deletions
+26
-29
README.rst
README.rst
+3
-3
format.h
format.h
+12
-15
format_test.cc
format_test.cc
+11
-11
No files found.
README.rst
View file @
059934fd
...
...
@@ -60,7 +60,7 @@ An object of any user-defined type for which there is an overloaded
std::string s = str(fmt::Format("The date is {0}") << Date(2012, 12, 9));
// s == "The date is 2012-12-9"
You can use ``fmt::
Active
Formatter`` to create your own functions
You can use ``fmt::
Temp
Formatter`` to create your own functions
similar to ``fmt::Format`` and ``fmt::Print`` with an arbitrary action
performed when formatting is complete:
...
...
@@ -73,8 +73,8 @@ performed when formatting is complete:
};
// Formats an error message and prints it to std::cerr.
fmt::
Active
Formatter<PrintError> ReportError(const char *format) {
return fmt::
Active
Formatter<PrintError>(format);
fmt::
Temp
Formatter<PrintError> ReportError(const char *format) {
return fmt::
Temp
Formatter<PrintError>(format);
}
ReportError("File not found: {0}") << path;
...
...
format.h
View file @
059934fd
...
...
@@ -303,9 +303,6 @@ class Formatter {
std
::
string
str
()
const
{
return
std
::
string
(
&
buffer_
[
0
],
buffer_
.
size
());
}
}
;
template
<
typename
Action
>
class
ActiveFormatter
;
namespace
internal
{
// This is a transient object that normally exists only as a temporary
...
...
@@ -410,19 +407,19 @@ inline internal::ArgInserter Formatter::operator()(const char *format) {
}
// A formatter with an action performed when formatting is complete.
//
This is a transient object that normally exists only as a temporary
//
returned by one of the formatting functions
.
//
Objects of this class normally exist only as temporaries returned
//
by one of the formatting functions, thus the name
.
template
<
typename
Action
>
class
Active
Formatter
:
public
internal
::
ArgInserter
{
class
Temp
Formatter
:
public
internal
::
ArgInserter
{
private:
Formatter
formatter_
;
Action
action_
;
// Forbid copying other than from a temporary. Do not implement.
ActiveFormatter
(
Active
Formatter
&
);
TempFormatter
(
Temp
Formatter
&
);
// Do not implement.
ActiveFormatter
&
operator
=
(
const
Active
Formatter
&
);
TempFormatter
&
operator
=
(
const
Temp
Formatter
&
);
struct
Proxy
{
const
char
*
format
;
...
...
@@ -436,17 +433,17 @@ class ActiveFormatter : public internal::ArgInserter {
// Action should be an unary function object that takes a const
// reference to Formatter as an argument. See Ignore and Write
// for examples of action classes.
explicit
Active
Formatter
(
const
char
*
format
,
Action
a
=
Action
())
explicit
Temp
Formatter
(
const
char
*
format
,
Action
a
=
Action
())
:
action_
(
a
)
{
Init
(
formatter_
,
format
);
}
Active
Formatter
(
const
Proxy
&
p
)
Temp
Formatter
(
const
Proxy
&
p
)
:
ArgInserter
(
0
),
action_
(
p
.
action
)
{
Init
(
formatter_
,
p
.
format
);
}
~
Active
Formatter
()
{
~
Temp
Formatter
()
{
if
(
formatter
())
action_
(
*
Format
());
}
...
...
@@ -466,8 +463,8 @@ struct Ignore {
// Formats a string.
// Example:
// std::string s = str(Format("Elapsed time: {0:.2f} seconds") << 1.23);
inline
Active
Formatter
<
Ignore
>
Format
(
const
char
*
format
)
{
return
Active
Formatter
<
Ignore
>
(
format
);
inline
Temp
Formatter
<
Ignore
>
Format
(
const
char
*
format
)
{
return
Temp
Formatter
<
Ignore
>
(
format
);
}
// A formatting action that writes formatted output to stdout.
...
...
@@ -480,8 +477,8 @@ struct Write {
// Formats a string and prints it to stdout.
// Example:
// Print("Elapsed time: {0:.2f} seconds") << 1.23;
inline
Active
Formatter
<
Write
>
Print
(
const
char
*
format
)
{
return
Active
Formatter
<
Write
>
(
format
);
inline
Temp
Formatter
<
Write
>
Print
(
const
char
*
format
)
{
return
Temp
Formatter
<
Write
>
(
format
);
}
}
...
...
format_test.cc
View file @
059934fd
...
...
@@ -703,20 +703,20 @@ struct CountCalls {
}
};
TEST
(
Active
FormatterTest
,
Action
)
{
TEST
(
Temp
FormatterTest
,
Action
)
{
int
num_calls
=
0
;
{
fmt
::
Active
Formatter
<
CountCalls
>
af
(
"test"
,
CountCalls
(
num_calls
));
fmt
::
Temp
Formatter
<
CountCalls
>
af
(
"test"
,
CountCalls
(
num_calls
));
EXPECT_EQ
(
0
,
num_calls
);
}
EXPECT_EQ
(
1
,
num_calls
);
}
TEST
(
Active
FormatterTest
,
ActionNotCalledOnError
)
{
TEST
(
Temp
FormatterTest
,
ActionNotCalledOnError
)
{
int
num_calls
=
0
;
{
EXPECT_THROW
(
fmt
::
Active
Formatter
<
CountCalls
>
af
(
"{0"
,
CountCalls
(
num_calls
)),
fmt
::
Temp
Formatter
<
CountCalls
>
af
(
"{0"
,
CountCalls
(
num_calls
)),
FormatError
);
}
EXPECT_EQ
(
0
,
num_calls
);
...
...
@@ -726,12 +726,12 @@ TEST(ActiveFormatterTest, ActionNotCalledOnError) {
// require an accessible copy constructor when binding a temporary to
// a const reference.
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
TEST
(
Active
FormatterTest
,
ArgLifetime
)
{
TEST
(
Temp
FormatterTest
,
ArgLifetime
)
{
// The following code is for testing purposes only. It is a definite abuse
// of the API and shouldn't be used in real applications.
const
fmt
::
Active
Formatter
<
fmt
::
Ignore
>
&
af
=
fmt
::
Format
(
"{0}"
);
const_cast
<
fmt
::
Active
Formatter
<
fmt
::
Ignore
>&>
(
af
)
<<
std
::
string
(
"test"
);
// String object passed as an argument to
Active
Formatter has
const
fmt
::
Temp
Formatter
<
fmt
::
Ignore
>
&
af
=
fmt
::
Format
(
"{0}"
);
const_cast
<
fmt
::
Temp
Formatter
<
fmt
::
Ignore
>&>
(
af
)
<<
std
::
string
(
"test"
);
// String object passed as an argument to
Temp
Formatter has
// been destroyed, but ArgInserter dtor hasn't been called yet.
// But that's OK since the Arg's dtor takes care of this and
// calls Format.
...
...
@@ -744,11 +744,11 @@ struct PrintError {
}
};
fmt
::
Active
Formatter
<
PrintError
>
ReportError
(
const
char
*
format
)
{
return
fmt
::
Active
Formatter
<
PrintError
>
(
format
);
fmt
::
Temp
Formatter
<
PrintError
>
ReportError
(
const
char
*
format
)
{
return
fmt
::
Temp
Formatter
<
PrintError
>
(
format
);
}
TEST
(
Active
FormatterTest
,
Example
)
{
TEST
(
Temp
FormatterTest
,
Example
)
{
std
::
string
path
=
"somefile"
;
ReportError
(
"File not found: {0}"
)
<<
path
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment