String stream wrapper operator help needed - c++

I found this beautiful little helper on this site:
Howto throw std::exceptions with variable messages?
class Formatter
{
public:
Formatter() {}
~Formatter() {}
template <typename Type>
Formatter & operator << (const Type & value)
{
stream_ << value;
return *this;
}
std::string str() const { return stream_.str(); }
operator std::string () const { return stream_.str(); }
enum ConvertToString
{
to_str
};
std::string operator >> (ConvertToString) { return stream_.str(); }
private:
std::stringstream stream_;
Formatter(const Formatter &);
Formatter & operator = (Formatter &);
};
It can be used like
std::string foo(Formatter() << "foo" << 42);
To allow enabling/disabling of the stream input I extended the class with
bool enabled;
enum class status {
active, inactive
};
Formatter& operator << (status s)
{
if (s == status::active) enabled = true;
if (s == status::inactive) enabled = false;
return *this;
}
and changed the input template to if (enabled) stream_ << value;
Now I can do
std::string optional = "";
std::string foo(Formatter()
<< "foo"
<< (optional.empty() ? Formatter::status::inactive : Formatter::status::active)
<< "bar" << optional
<< Formatter::status::active
<< "!");
Now I´m looking for a way to get rid of status and use Formatter::inactive directly instead.

Related

Parsing a string to create a geometry

What is the algorithm for developing a string parser to create a geometry? The geometry is generated in 2 steps: at the first step, we create primitives; at the second, we combine primitives into objects.
The syntax is presented in the string below.
string str="[GEOMETRY]
PRIMITIVE1=SPHERE(RADIUS=5.5);
PRIMITIVE2=BOX(A=-5.2, B=7.3);
//...
OBJECT1=PRIMITIVE2*(-PRIMITIVE1);
//..."
class PRIMITIVE{
int number;
public:
Primitive& operator+ (Primitive& primitive) {}; //overloading arithmetic operations
Primitive& operator* (Primitive& primitive) {};
Primitive& operator- (Primitive& primitive) {};
virtual bool check_in_point_inside_primitive = 0;
};
class SPHERE:public PRIMITIVE{
double m_radius;
public:
SPHERE(double radius): m_radius(radius) {}; //In which part of the parser to create objects?
bool check_in_point_inside_sphere(Point& point){};
};
class BOX:public PRIMITIVE{
double m_A;
double m_B;
public:
BOX(double A, double B): m_A(A), m_B(B) {};
bool check_in_point_inside_box(Point& point){};
};
class OBJECT{
int number;
PRIMITIVE& primitive;
public:
OBJECT(){};
bool check_in_point_inside_object(Primitive& PRIMITIVE1, Primitive& PRIMITIVE2, Point& point){
//>How to construct a function from an expression 'PRIMITIVE2*(-PRIMITIVE1)' when parsing?
}
};
How to analyze the string PRIMITIVE1=SPHERE(RADIUS=5.5) and pass a parameter to the constructor of SPHERE()? How to identify this object with the name PRIMITIVE 1 to call to it in OBJECT? Is it possible to create a pair<PRIMITIVE1,SPHERE(5.5)> and store all primitives in map?
How to parse the string of the OBJECT1 and to construct a function from an expression PRIMITIVE2*(-PRIMITIVE1) inside an OBJECT1? This expression will be required multiple times when determining the position of each point relative to the object.
How to use boost::spirit for this task? Tokenize a string using boost::spirit::lex, and then develop rules using boost::spirit::qi?
As a finger exercise, and despite the serious problems I see with the chosen virtual type hierarchy, let's try to make a value-oriented container of Primitives that can be indexed by their id (ById):
Live On Coliru
#include <boost/intrusive/set.hpp>
#include <boost/poly_collection/base_collection.hpp>
#include <iostream>
namespace bi = boost::intrusive;
struct Point {
};
using IndexHook = bi::set_member_hook<bi::link_mode<bi::auto_unlink>>;
class Primitive {
int _id;
public:
struct ById {
bool operator()(auto const&... oper) const { return std::less<>{}(access(oper)...); }
private:
static int access(int id) { return id; }
static int access(Primitive const& p) { return p._id; }
};
IndexHook _index;
Primitive(int id) : _id(id) {}
virtual ~Primitive() = default;
int id() const { return _id; }
Primitive& operator+= (Primitive const& primitive) { return *this; } //overloading arithmetic operations
Primitive& operator*= (Primitive const& primitive) { return *this; }
Primitive& operator-= (Primitive const& primitive) { return *this; }
virtual bool check_in_point_inside(Point const&) const = 0;
};
using Index =
bi::set<Primitive, bi::constant_time_size<false>,
bi::compare<Primitive::ById>,
bi::member_hook<Primitive, IndexHook, &Primitive::_index>>;
class Sphere : public Primitive {
double _radius;
public:
Sphere(int id, double radius)
: Primitive(id)
, _radius(radius) {} // In which part of the parser to create objects?
bool check_in_point_inside(Point const& point) const override { return false; }
};
class Box : public Primitive {
double _A;
double _B;
public:
Box(int id, double A, double B) : Primitive(id), _A(A), _B(B) {}
bool check_in_point_inside(Point const& point) const override { return false; }
};
class Object{
int _id;
Primitive& _primitive;
public:
Object(int id, Primitive& p) : _id(id), _primitive(p) {}
bool check_in_point_inside_object(Primitive const& p1, Primitive const& p2,
Point const& point) const
{
//>How to construct a function from an expression
//'PRIMITIVE2*(-PRIMITIVE1)' when parsing?
return false;
}
};
using Primitives = boost::poly_collection::base_collection<Primitive>;
int main() {
Primitives test;
test.insert(Sphere{2, 4.0});
test.insert(Sphere{4, 4.0});
test.insert(Box{2, 5, 6});
test.insert(Sphere{1, 4.0});
test.insert(Box{3, 5, 6});
Index idx;
for (auto& p : test)
if (not idx.insert(p).second)
std::cout << "Duplicate id " << p.id() << " not indexed\n";
for (auto& p : idx)
std::cout << typeid(p).name() << " " << p.id() << "\n";
std::cout << "---\n";
for (auto& p : test)
std::cout << typeid(p).name() << " " << p.id() << "\n";
}
Prints
Duplicate id 2 not indexed
6Sphere 1
3Box 2
3Box 3
6Sphere 4
---
3Box 2
3Box 3
6Sphere 2
6Sphere 4
6Sphere 1
So far so good. This is an important building block to prevent all manner of pain when dealing with virtual types in Spirit grammars¹
PS: I've since dropped the idea of intrusive_set. It doesn't work because the base_container moves items around on reallocation, and that unlinks the items from their intrusive set.
Instead, see below for an approach that doesn't try to resolve ids during the parse.
Parsing primitives
We get the ID from the PRIMITIVE1. We could store it somewhere before naturally parsing the primitives themselves, then set the id on it on commit.
Let's start with defining a State object for the parser:
struct State {
Ast::Id next_id;
Ast::Primitives primitives;
Ast::Objects objects;
template <typename... T> void commit(boost::variant<T...>& val) {
boost::apply_visitor([this](auto& obj) { commit(obj); }, val);
}
template <typename T> void commit(T& primitiveOrExpr) {
auto id = std::exchange(next_id, 0);
if constexpr (std::is_base_of_v<Ast::Primitive, T>) {
primitiveOrExpr.id = id;
primitives.insert(std::move(primitiveOrExpr));
} else {
objects.push_back(Ast::Object{id, std::move(primitiveOrExpr)});
}
}
};
As you can see, we just have a place to store the primitives, objects. And then there is the temporary storage for our next_id while we're still parsing the next entity.
The commit function helps sorting the products of the parser rules. As it happens, they can be variant, which is why we have the apply_visitor dispatch for commit on a variant.
Again, as the footnote¹ explains, Spirit's natural attribute synthesis favors static polymorphism.
The semantic actions we need are now:
static inline auto& state(auto& ctx) { return get<State>(ctx); }
auto draft = [](auto& ctx) { state(ctx).next_id = _attr(ctx); };
auto commit = [](auto& ctx) { state(ctx).commit(_attr(ctx)); };
Now let's jump ahead to the primitives:
auto sphere = as<Ast::Sphere>(eps >> "sphere" >>'(' >> param("radius") >> ')');
auto box = as<Ast::Box>(eps >> "box" >> '(' >> param('a') >> ',' >> param('b') >> ')');
auto primitive =
("primitive" >> uint_[draft] >> '=' >> (sphere | box)[commit]) > ';';
That's still cheating a little, as I've used the param helper to reduce typing:
auto number = as<Ast::Number>(double_, "number");
auto param(auto name, auto p) { return eps >> omit[name] >> '=' >> p; }
auto param(auto name) { return param(name, number); }
As you can see I've already assumed most parameters will have numerical nature.
What Are Objects Really?
Looking at it for a while, I concluded that really an Object is defined as an id number (OBJECT1, OBJECT2...) which is tied to an expression. The expression can reference primitives and have some unary and binary operators.
Let's sketch an AST for that:
using Number = double;
struct RefPrimitive { Id id; };
struct Binary;
struct Unary;
using Expr = boost::variant< //
Number, //
RefPrimitive, //
boost::recursive_wrapper<Unary>, //
boost::recursive_wrapper<Binary> //
>;
struct Unary { char op; Expr oper; };
struct Binary { Expr lhs; char op; Expr rhs; };
struct Object { Id id; Expr expr; };
Now To Parse Into That Expression AST
It's really 1:1 rules for each Ast node type. E.g.:
auto ref_prim = as<Ast::RefPrimitive>(lexeme["primitive" >> uint_]);
Now many of the expression rules can recurse, so we need declared rules with definitions via BOOST_SPIRIT_DEFINE:
// object expression grammar
rule<struct simple_tag, Ast::Expr> simple{"simple"};
rule<struct unary_tag, Ast::Unary> unary{"unary"};
rule<struct expr_tag, Ast::Expr> expr{"expr"};
rule<struct term_tag, Ast::Expr> term{"term"};
rule<struct factor_tag, Ast::Expr> factor{"factor"};
As you can tell, some of these are not 1:1 with the Ast nodes, mainly because of the recursion and the difference in operator precedence (term vs factor vs. simple). It's easier to see with the rule definition:
auto unary_def = char_("-+") >> simple;
auto simple_def = ref_prim | unary | '(' >> expr >> ")";
auto factor_def = simple;
auto term_def = factor[assign] >> *(char_("*/") >> term)[make_binary];
auto expr_def = term[assign] >> *(char_("-+") >> expr)[make_binary];
Because none of the rules actually expose a Binary, automatic attribute propagation is not convenient there². Instead, we use assign and make_binary semantic actions:
auto assign = [](auto& ctx) { _val(ctx) = _attr(ctx); };
auto make_binary = [](auto& ctx) {
using boost::fusion::at_c;
auto& attr = _attr(ctx);
auto op = at_c<0>(attr);
auto& rhs = at_c<1>(attr);
_val(ctx) = Ast::Binary { _val(ctx), op, rhs };
};
Finally, let's tie the defintions to the declared rules (using their tag types):
BOOST_SPIRIT_DEFINE(simple, unary, expr, term, factor)
All we need is a similar line to primitive:
auto object =
("object" >> uint_[draft] >> '=' >> (expr)[commit]) > ';';
And we can finish up by defining each line as a primitive|object:
auto line = primitive | object;
auto file = no_case[skip(ws_comment)[*eol >> "[geometry]" >> (-line % eol) >> eoi]];
At the top level we expect the [GEOMETRY] header, specify that we want to be case insensitive and ... that ws_comment is to be skipped³:
auto ws_comment = +(blank | lexeme["//" >> *(char_ - eol) >> eol]);
This allows us to ignore the // comments as well.
Live Demo Time
Live On Compiler Explorer
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/fusion/adapted.hpp>
#include <boost/poly_collection/base_collection.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <list>
#include <map>
namespace x3 = boost::spirit::x3;
namespace Ast {
using Id = uint32_t;
struct Point { }; // ?? where does this belong?
struct Primitive {
Id id;
virtual ~Primitive() = default;
};
struct Sphere : Primitive { double radius; };
struct Box : Primitive { double a, b; };
using Number = double;
struct RefPrimitive { Id id; };
struct Binary;
struct Unary;
using Expr = boost::variant< //
Number, //
RefPrimitive, //
boost::recursive_wrapper<Unary>, //
boost::recursive_wrapper<Binary> //
>;
struct Unary { char op; Expr oper; };
struct Binary { Expr lhs; char op; Expr rhs; };
struct Object { Id id; Expr expr; };
using Primitives = boost::poly_collection::base_collection<Primitive>;
using Objects = std::list<Object>;
using Index = std::map<Id, std::reference_wrapper<Primitive const>>;
std::ostream& operator<<(std::ostream& os, Primitive const& p) {
return os << boost::core::demangle(typeid(p).name()) << " "
<< "(id: " << p.id << ")";
}
std::ostream& operator<<(std::ostream& os, Object const& o) {
return os << "object(id:" << o.id << ", expr:" << o.expr << ")";
}
std::ostream& operator<<(std::ostream& os, RefPrimitive ref) {
return os << "reference(prim:" << ref.id << ")";
}
std::ostream& operator<<(std::ostream& os, Binary const& b) {
return os << '(' << b.lhs << b.op << b.rhs << ')';
}
std::ostream& operator<<(std::ostream& os, Unary const& u) {
return os << '(' << u.op << u.oper << ')';
}
} // namespace Ast
BOOST_FUSION_ADAPT_STRUCT(Ast::Primitive, id)
BOOST_FUSION_ADAPT_STRUCT(Ast::Sphere, radius)
BOOST_FUSION_ADAPT_STRUCT(Ast::Box, a, b)
BOOST_FUSION_ADAPT_STRUCT(Ast::Object, id)
BOOST_FUSION_ADAPT_STRUCT(Ast::RefPrimitive, id)
BOOST_FUSION_ADAPT_STRUCT(Ast::Unary, op, oper)
namespace Parser {
using namespace x3;
struct State {
Ast::Id next_id;
Ast::Primitives primitives;
Ast::Objects objects;
template <typename... T> void commit(boost::variant<T...>& val) {
boost::apply_visitor([this](auto& obj) { commit(obj); }, val);
}
template <typename T> void commit(T& val) {
auto id = std::exchange(next_id, 0);
if constexpr (std::is_base_of_v<Ast::Primitive, T>) {
val.id = id;
primitives.insert(std::move(val));
} else {
objects.push_back(Ast::Object{id, std::move(val)});
}
}
};
static inline auto& state(auto& ctx) { return get<State>(ctx); }
auto draft = [](auto& ctx) { state(ctx).next_id = _attr(ctx); };
auto commit = [](auto& ctx) { state(ctx).commit(_attr(ctx)); };
template <typename T>
auto as = [](auto p, char const* name = "as") {
return rule<struct _, T>{name} = p;
};
auto ws_comment = +(blank | lexeme["//" >> *(char_ - eol) >> (eol | eoi)]);
auto number = as<Ast::Number>(double_, "number");
auto param(auto name, auto p) { return eps >> omit[name] >> '=' >> p; }
auto param(auto name) { return param(name, number); }
auto sphere = as<Ast::Sphere>(eps >> "sphere" >>'(' >> param("radius") >> ')');
auto box = as<Ast::Box>(eps >> "box" >> '(' >> param('a') >> ',' >> param('b') >> ')');
auto primitive =
("primitive" >> uint_[draft] >> '=' >> (sphere | box)[commit]) > ';';
auto ref_prim = as<Ast::RefPrimitive>(lexeme["primitive" >> uint_], "ref_prim");
// object expression grammar
rule<struct simple_tag, Ast::Expr> simple{"simple"};
rule<struct unary_tag, Ast::Unary> unary{"unary"};
rule<struct expr_tag, Ast::Expr> expr{"expr"};
rule<struct term_tag, Ast::Expr> term{"term"};
rule<struct factor_tag, Ast::Expr> factor{"factor"};
auto assign = [](auto& ctx) { _val(ctx) = _attr(ctx); };
auto make_binary = [](auto& ctx) {
using boost::fusion::at_c;
auto& attr = _attr(ctx);
auto op = at_c<0>(attr);
auto& rhs = at_c<1>(attr);
_val(ctx) = Ast::Binary { _val(ctx), op, rhs };
};
auto unary_def = char_("-+") >> simple;
auto simple_def = ref_prim | unary | '(' >> expr >> ")";
auto factor_def = simple;
auto term_def = factor[assign] >> *(char_("*/") >> term)[make_binary];
auto expr_def = term[assign] >> *(char_("-+") >> expr)[make_binary];
BOOST_SPIRIT_DEFINE(simple, unary, expr, term, factor)
auto object =
("object" >> uint_[draft] >> '=' >> (expr)[commit]) > ';';
auto line = primitive | object;
auto file = no_case[skip(ws_comment)[*eol >> "[geometry]" >> (-line % eol) >> eoi]];
} // namespace Parser
int main() {
for (std::string const input :
{
R"(
[geometry]
primitive1=sphere(radius=5.5);
primitive2=box(a=-5.2, b=7.3);
//...
object1=primitive2*(-primitive1);
//...)",
R"(
[GEOMETRY]
PRIMITIVE1=SPHERE(RADIUS=5.5);
PRIMITIVE2=BOX(A=-5.2, B=7.3);
//...
OBJECT1=PRIMITIVE2*(-PRIMITIVE1);
//...)",
}) //
{
Parser::State state;
bool ok = parse(begin(input), end(input),
x3::with<Parser::State>(state)[Parser::file]);
std::cout << "Parse success? " << std::boolalpha << ok << "\n";
Ast::Index index;
for (auto& p : state.primitives)
if (auto[it,ok] = index.emplace(p.id, p); not ok) {
std::cout << "Duplicate id " << p
<< " (conflicts with existing " << it->second.get()
<< ")\n";
}
std::cout << "Primitives by ID:\n";
for (auto& [id, prim] : index)
std::cout << " - " << prim << "\n";
std::cout << "Objects in definition order:\n";
for (auto& obj: state.objects)
std::cout << " - " << obj << "\n";
}
}
Prints
Parse success? true
Primitives by ID:
- Ast::Sphere (id: 1)
- Ast::Box (id: 2)
Objects in definition order:
- object(id:1, expr:(reference(prim:2)*(-reference(prim:1))))
Parse success? true
Primitives by ID:
- Ast::Sphere (id: 1)
- Ast::Box (id: 2)
Objects in definition order:
- object(id:1, expr:(reference(prim:2)*(-reference(prim:1))))
¹ How can I use polymorphic attributes with boost::spirit::qi parsers?
² and insisting on that leads to classical in-efficiency with rules that cause a lot of backtracking
³ outside of lexemes

File reading error in Reader class implementation

I'm trying to implement a Reader class for my own hobby programming language which I'm working on. The job of the reader would be very simple, to read the source file, and remove comments.
Here's the definition of the Reader class:
// reader.hh
// Contains Reader Class specifications
#ifndef PHI_SRC_FRONTEND_READER_HH
#define PHI_SRC_FRONTEND_READER_HH
#include "errhandler.hh"
class Reader
{
public:
Reader() = default;
auto val() const -> String const & { return val_; }
void read(String const &filename);
explicit operator bool() const { return success; }
auto operator!() const -> bool { return !success; }
friend auto operator==(Reader const &lhs, Reader const &rhs) -> bool
{
return lhs.val_ == rhs.val_;
}
friend auto operator!=(Reader const &lhs, Reader const &rhs) -> bool
{
return lhs.val_ != rhs.val_;
}
friend auto operator<<(std::ostream &stream, Reader const &read) -> std::ostream &
{
return stream << read.val_;
}
private:
String val_;
bool success;
};
#endif
Previously, I used a very simple placeholder for the Reader's read function. It basically copied everything inside the file using an istreambuf_iterator
void Reader::read(String const &filename)
{
val_.clear();
success = true; // success flag, true by default
auto file = std::ifstream{filename};
if(!file)
{
log_error(Error{Error::Type::ReadError, "Could not open file"});
success = false;
}
val_.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
// Read entire file into val_
}
This worked fine, it passed the unit tests, and I manually checked the output as well, which was also fine.
But this was just a placeholder, the actual Reader needed to remove comments. Which made me implement this:
// reader.cc
// Contains Reader Class Implementation
// Work In Progress, placeholders being used for now
#include <fstream>
#include <sstream>
#include "reader.hh"
void Reader::read(String const &filename)
{
val_.clear();
success = true; // success flag, true by default
auto inStringLiteral = false;
// Variable to determine if the reader is currently reading a string literal
// (enclosed in double quotes)
// In order to not mistake '//' inside literals as comments"
auto file = std::ifstream{filename};
if(!file)
{
log_error(Error{Error::Type::ReadError, "Cannot open file: " + filename});
success = false;
return;
}
for (unsigned char c; file >> c; )
{
// ASCII characters only use 7 bits, which is up to 127
// So value of an ascii char must be lesser than 128
if (c < 128)
{
if(c == '"')
{
inStringLiteral = !inStringLiteral; // flip the value of the boolean
}
if(!inStringLiteral && c == '/')
{
// If we're not inside a string literal enclosed in quotes, and find a backslash
// Peek at the next character to check if it is a backslash
// In case two consecutive backslashes are found, treat it as a comment and
// ignore everything until the end of line
if(file >> c)
{
if(c == '/')
{
// keep reading until a newline is found
while(file >> c && c != '\n')
{
}
}
else
{
c = '/';
file.unget();
}
}
else
{
c = '/';
}
}
val_ += c;
}
else
{
log_error(Error{Error::Type::ReadError, "Unrecognized character(s) found in file: " + filename});
success = false;
return;
}
}
}
However, this weirdly causes the Unit Tests to fail.. I've compared the outputs of both versions of the read function, both have (apparently) the exact same output. Note I haven't actually checked for equality of the strings, but they do look the same. I've tried to find the reason for the error a lot but have failed....
Here's the Unit Test I'm using for the Reader (using GoogleTest):
#include <gtest/gtest.h>
#include "frontend/reader.hh"
TEST(ReaderTest, BaseTestCase)
{
auto TestReader = Reader{};
auto const ExpectedOutput = String{
R"delim(Int test = 0;
String test2 = "abcdefgh";
Float test3 = 0.9876;
)delim"};
TestReader.read("TestFiles/ReaderTest_BaseTestCase.phi");
ASSERT_FALSE(!TestReader);
ASSERT_EQ(TestReader.val(), ExpectedOutput);
// If Reader Base Test Case fails, no need to continue next tests
}
TEST(ReaderTest, Should_Fail_When_FileDoesNotExist)
{
auto TestReader = Reader{};
TestReader.read("Non_existent_test_file.txt");
EXPECT_TRUE(!TestReader);
}
As I mentioned before, it worked quite fine for the first placeholder version, but the actual read function doesn't seem to pass the tests.... The weird thing is, the sample file doesn't even have any comments, here's the sample file the Reader reads:
Int test = 0;
String test2 = "abcdefgh";
Float test3 = 0.9876;
(Yes, that's literally it. Also as I mentioned before, the language the Reader is reading is not C++, but rather a homemade language I'm working on reading, but that's probably irrelevant for this question).
Oh and in case you need to compile this, you'd need to implementation and definition of errhandler (errhandler.hh and errhandler.cc), I'll put them here as well:
Declaration (errhandler.hh):
// errhandler.hh
// Contains Phi Error Handling specifications
// Mostly complete, minor changes still might be made though
#ifndef PHI_SRC_FRONTEND_ERRHANDLER_HH
#define PHI_SRC_FRONTEND_ERRHANDLER_HH
#include <iostream>
#include "utils.hh"
class Error
{
public:
enum class Type : unsigned char
{
ReadError, LexError, ParseError, SemanticError, InterpretError
};
Error() = delete;
Error(Type type__, String const &val__) : type_(type__), val_(val__) {}
auto type() const -> Type { return type_; }
auto val() const -> String { return val_; }
friend auto operator<<(std::ostream &stream, Error const &error) -> std::ostream&
{
return stream << error.val();
}
private:
Type type_;
String val_;
};
class ErrorLog
{
public:
using iterator = Vector<Error>::iterator;
using const_iterator = Vector<Error>::const_iterator;
using reverse_iterator = Vector<Error>::reverse_iterator;
using const_reverse_iterator = Vector<Error>::const_reverse_iterator;
void push(Error const &error) { errors.push_back(error); }
void pop() { errors.pop_back(); }
auto size() const -> Size { return errors.size(); }
auto operator[](Size index) -> Error& { return errors[index]; }
auto operator[](Size index) const -> Error const& { return errors[index]; }
auto begin() -> iterator { return errors.begin(); }
auto end() -> iterator { return errors.end(); }
auto cbegin() const -> const_iterator { return errors.cbegin(); }
auto cend() const -> const_iterator { return errors.cend(); }
auto rbegin() -> reverse_iterator { return errors.rbegin(); }
auto rend() -> reverse_iterator { return errors.rend(); }
auto crbegin() -> const_reverse_iterator { return errors.crbegin(); }
auto crend() -> const_reverse_iterator { return errors.crend(); }
friend auto operator<<(std::ostream &stream, ErrorLog const &error_log) -> std::ostream&
{
for (Size i = 0; i < error_log.size(); i++)
stream << error_log[i];
return stream;
}
private:
Vector<Error> errors;
};
void log_error(Error const &error);
void show_errors(std::ostream& stream);
extern ErrorLog errlog;
// The global error log to be used by every part of the Phi system
// To be declared in main()
#endif
Definition (errhandler.cc):
// errhandler.cc
// Contains Phi Error Handling implementation
// Work In Progress, placeholders are temporarily being used
#include "errhandler.hh"
void log_error(Error const& error)
{
errlog.push(error);
}
void show_errors(std::ostream& stream)
{
stream << errlog;
}
And you also, finally, need the utils header, containing the utilities
// utils.hh
// Contains globally used utility functions, templates, using declarations, etc.
#ifndef PHI_SRC_UTILS_HH
#define PHI_SRC_UTILS_HH
#include <string>
#include <vector>
#include <limits>
#define UNUSED(x) (void)x
template <typename T>
using Vector = std::vector<T>;
using String = std::string;
using Size = std::size_t;
class bad_narrow_cast : public std::bad_cast
{
public:
bad_narrow_cast(const char* message) : what_(message)
{
}
char const *what() { return what_; }
private:
char const *what_;
};
template <typename Target, typename Base> static inline
typename std::enable_if<std::numeric_limits<Target>::is_specialized,
Target>::type narrow_cast(Base const &base)
{
if(base > static_cast<Base>(std::numeric_limits<Target>::max()) ||
base < static_cast<Base>(std::numeric_limits<Target>::min()))
{
throw(bad_narrow_cast((String() + "Invalid narrowing conversation from type " +
typeid(Target).name() + " to type " + typeid(Base).name()).c_str()));
}
return static_cast<Target>(base);
}
template <typename Target, typename Base> static inline
typename std::enable_if<!std::numeric_limits<Target>::is_specialized,
Target>::type narrow_cast(Base const &base)
{
Target target = static_cast<Target>(base);
Base narrowed_base = static_cast<Base>(target);
if (base == narrowed_base)
return target;
throw(bad_narrow_cast((String() + "Invalid narrowing conversation from type " +
typeid(Target).name() + " to type " + typeid(Base).name()).c_str()));
}
#endif
This question really halted my progress on the project. Helping me solve it would be really helpful
So, people in the comments really helped me, and suggested me to use >> for reading characters from the stream instead of eof() and get(), as eof() is unreliable.. But even that didn't solve the problem. Until I, through some googling, figured it out myself, I had to use std::noskipws, in order to make the >> operator not skip whitespaces, and then it worked. Thanks for all the help, I really appreciate it

Dynamically switching symbol tables in x3

Given the following x3 grammar that parses correctly, I want to add validation of parameters, qualifiers, and properties. This would seem to indicate some method of dynamically switching which symbol table is being used within the various rules. What is the best way to implement this? It would seem to be some mixture of semantic actions and attributes, but it is not clear to me how.
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <map>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/variant.hpp>
#include <boost/fusion/adapted/struct.hpp>
namespace x3 = boost::spirit::x3;
namespace scl
{
//
// This will take a symbol value and return the string associated with that value. From an example by sehe
// TODO: There is probably a better C++14/17 way to do this with the symbol.for_each operator and a lambda,
// but I haven't figured it out yet
//
template <typename T>
struct SYMBOL_LOOKUP
{
SYMBOL_LOOKUP (T Symbol, std::string& String) : _sought (Symbol), _found (String)
{
}
void operator () (std::basic_string <char> s, T ct)
{
if (_sought == ct)
{
_found = s;
}
}
std::string found () const { return _found; }
private:
T _sought;
std::string& _found;
};
//
// This section describes the valid verbs, the parameters that are valid for each verb, and
// the qualifiers that are valid for each verb or parameter of a verb.
// TODO: There is probably some complicated C++11/14/17 expression template for generating all
// of this as a set of linked tables, where each verb points to a parameter table, which points
// to a qualifier table, but that is currently beyond my ability to implement, so each structure
// is implemented discretely
//
//
// Legal verbs
//
enum class VERBS
{
load, //
set, //
show, //
};
struct VALID_VERBS : x3::symbols <VERBS>
{
VALID_VERBS ()
{
add
("load", VERBS::load) //
("set", VERBS::set) //
("show", VERBS::show) //
;
}
} const valid_verbs;
//
// LOAD parameter 1
//
enum class LOAD_PARAMETER1
{
dll, // LOAD DLL <file-spec>
pdb, // LOAD PDB <file-spec>
};
struct VALID_LOAD_PARAMETER1 : x3::symbols <LOAD_PARAMETER1>
{
VALID_LOAD_PARAMETER1 ()
{
add
("dll", LOAD_PARAMETER1::dll) //
("pdb", LOAD_PARAMETER1::pdb) //
;
}
} const valid_load_parameter1;
//
// SET parameter 1
//
enum class SET_PARAMETER1
{
debug, // SET DEBUG {/ON | /OFF}
trace, // SET TRACE {/ON | OFF}
};
struct VALID_SET_PARAMETER1 : x3::symbols <SET_PARAMETER1>
{
VALID_SET_PARAMETER1 ()
{
add
("debug", SET_PARAMETER1::debug) //
("trace", SET_PARAMETER1::trace) //
;
}
} const valid_set_parameter1;
//
// SET qualifiers
//
enum class SET_QUALIFIERS
{
off, //
on //
};
struct VALID_SET_QUALIFIERS : x3::symbols <SET_QUALIFIERS>
{
VALID_SET_QUALIFIERS ()
{
add
("off", SET_QUALIFIERS::off) //
("on", SET_QUALIFIERS::on) //
;
}
} const valid_set_qualifiers;
//
// SHOW parameter 1
//
enum class SHOW_PARAMETER1
{
debug, // SHOW DEBUG
module, // SHOW MODULE <wildcard-expression> [/SYMBOLS]
symbols, // SHOW SYMBOLS *{/ALL /FULL /OUT=<file-spec> /TYPE=(+{all,exports,imports})} [wild-card-expression]
trace, // SHOW TRACE
};
struct VALID_SHOW_PARAMETER1 : x3::symbols <SHOW_PARAMETER1>
{
VALID_SHOW_PARAMETER1 ()
{
add
("debug", SHOW_PARAMETER1::debug) //
("module", SHOW_PARAMETER1::module) //
("symbols", SHOW_PARAMETER1::symbols) //
("trace", SHOW_PARAMETER1::trace) //
;
}
} const valid_show_parameter1;
//
// SHOW qualifiers
//
enum class SHOW_QUALIFIERS
{
all, // Display all objects of the specified type
full, // Display all information about the specified object(s)
out, // Write output to the specified file (/out=<file spec>)
type, // List of properties to display
};
struct VALID_SHOW_QUALIFIERS : x3::symbols <SHOW_QUALIFIERS>
{
VALID_SHOW_QUALIFIERS ()
{
add
("all", SHOW_QUALIFIERS::all) //
("full", SHOW_QUALIFIERS::full) //
("out", SHOW_QUALIFIERS::out) //
("type", SHOW_QUALIFIERS::type) // Valid properties in VALID_SHOW_TYPE_PROPERTIES
;
}
} const valid_show_qualifiers;
//
// SHOW /TYPE=(property_list)
//
enum class SHOW_TYPE_PROPERTIES
{
all, //
exports, //
imports, //
};
struct VALID_SHOW_TYPE_PROPERTIES : x3::symbols <SHOW_TYPE_PROPERTIES>
{
VALID_SHOW_TYPE_PROPERTIES ()
{
add
("all", SHOW_TYPE_PROPERTIES::all) //
("exports", SHOW_TYPE_PROPERTIES::exports) //
("imports", SHOW_TYPE_PROPERTIES::imports) //
;
}
} const valid_show_type_properties;
//
// Convert a verb value to its string representation
//
std::string to_string (const VERBS Verb)
{
std::string result;
SYMBOL_LOOKUP <VERBS> lookup (Verb, result);
//
// Loop through all the entries in the symbol table looking for the specified value
// Is there a better way to use this for_each with a lambda?
//
valid_verbs.for_each (lookup);
return result;
} // End to_string
} // End namespace scl
namespace scl_ast
{
struct KEYWORD : std::string
{
using std::string::string;
using std::string::operator=;
};
struct NIL
{
};
using VALUE = boost::variant <NIL, std::string, int, double, KEYWORD>;
struct PROPERTY
{
KEYWORD name;
VALUE value;
};
struct QUALIFIER
{
enum KIND
{
positive,
negative
} kind;
std::string identifier;
std::vector <PROPERTY> properties;
};
struct PARAMETER
{
KEYWORD keyword;
std::vector <QUALIFIER> qualifiers;
};
struct COMMAND
{
scl::VERBS verb;
std::vector <QUALIFIER> qualifiers;
std::vector <PARAMETER> parameters;
};
//
// Overloads for printing the AST to the console
//
#pragma region debug
static inline std::ostream& operator<< (std::ostream& os, VALUE const& v)
{
struct
{
std::ostream& _os;
void operator() (std::string const& s) const { _os << std::quoted (s); }
void operator() (int i) const { _os << i; }
void operator() (double d) const { _os << d; }
void operator() (KEYWORD const& kwv) const { _os << kwv; }
void operator() (NIL) const { }
} vis { os };
boost::apply_visitor (vis, v);
return os;
}
static inline std::ostream& operator<< (std::ostream& os, PROPERTY const& prop)
{
os << prop.name;
if (prop.value.which ())
{
os << "=" << prop.value;
}
return os;
}
static inline std::ostream& operator<< (std::ostream& os, QUALIFIER const& q)
{
os << "/" << (q.kind == QUALIFIER::negative ? "no" : "") << q.identifier;
if (!q.properties.empty ())
{
os << "=(";
}
for (auto const& prop : q.properties)
{
os << prop << " ";
}
if (!q.properties.empty ())
{
os << ")";
}
return os;
}
static inline std::ostream& operator<< (std::ostream& os, std::vector <QUALIFIER> const& qualifiers)
{
for (auto const& qualifier : qualifiers)
{
os << " " << qualifier;
}
return os;
}
static inline std::ostream& operator<< (std::ostream& os, PARAMETER const& p)
{
return os << p.keyword << " " << p.qualifiers;
}
static inline std::ostream& operator<< (std::ostream& os, COMMAND const& cmd)
{
os << scl::to_string (cmd.verb) << cmd.qualifiers;
for (auto& param : cmd.parameters)
{
os << " " << param;
}
return os;
}
#pragma endregion debug
}; // End namespace scl_ast
BOOST_FUSION_ADAPT_STRUCT (scl_ast::PROPERTY, name, value);
BOOST_FUSION_ADAPT_STRUCT (scl_ast::QUALIFIER, kind, identifier, properties);
BOOST_FUSION_ADAPT_STRUCT (scl_ast::PARAMETER, keyword, qualifiers);
BOOST_FUSION_ADAPT_STRUCT (scl_ast::COMMAND, verb, qualifiers, parameters);
//
// Grammar for simple command language
//
namespace scl
{
using namespace x3;
auto const param = rule <struct _keyword, scl_ast::KEYWORD> { "param" }
= lexeme [+char_ ("a-zA-Z0-9$_.\\*?+-")];
auto const identifier
= lexeme [+char_ ("a-zA-Z0-9_")];
auto const quoted_string
= lexeme ['"' >> *('\\' > char_ | ~char_ ('"')) >> '"'];
auto const property_value
= quoted_string
| real_parser <double, x3::strict_real_policies <double>> {}
| int_
| param;
auto const property = rule <struct _property, scl_ast::PROPERTY> { "property" }
= identifier >> -('=' >> property_value);
auto const property_list = rule <struct _property_list, std::vector <scl_ast::PROPERTY>> { "property_list" }
= '(' >> property % ',' >> ')';
auto const qual
= attr (scl_ast::QUALIFIER::positive) >> lexeme ['/' >> identifier] >> -( '=' >> (property_list | repeat (1) [property]));
auto const neg_qual
= attr (scl_ast::QUALIFIER::negative) >> lexeme [no_case ["/no"] >> identifier] >> repeat (0) [property]; // Negated qualifiers never have properties (repeat(0) keeps the compiler happy)
auto const qualifier
= neg_qual | qual;
auto const verb
= no_case [valid_verbs]; // Uses static list of allowed verbs
auto const parameter = rule <struct _parameter, scl_ast::PARAMETER> { "parameter" }
= param >> *qualifier;
auto const command = rule <struct _command, scl_ast::COMMAND> { "command" }
= skip (blank) [verb >> *qualifier >> *parameter];
}; // End namespace scl
int
main ()
{
std::vector <std::string> input =
{
"load dll test.dll",
"LOAD pdb test.pdb",
"set debug /on",
"show debug",
"SHOW module test.dll/symbols",
"show symbols/type=export test*",
"show symbols test.dll/type=(import,export)",
"show symbols s*/out=s.txt",
"show symbols /all /full",
};
for (auto const& str : input)
{
scl_ast::COMMAND cmd;
auto b = str.begin ();
auto e = str.end ();
bool ok = parse (b, e, scl::command, cmd);
std::cout << (ok ? "OK" : "FAIL") << '\t' << std::quoted (str) << std::endl;
if (ok)
{
std::cout << " -- Full AST: " << cmd << std::endl;
std::cout << " -- Verb + Qualifiers: " << scl::to_string (cmd.verb) << cmd.qualifiers << std::endl;
for (auto const& param : cmd.parameters)
{
std::cout << " -- Parameter + Qualifiers: " << param << std::endl;
}
if (b != e)
{
std::cout << "*** Remaining unparsed: " << std::quoted (std::string (b, e)) << std::endl;
}
}
std::cout << std::endl;
} // End for
return 0;
} // End main
So, I spent quite some time thinking about this.
I admit most of the thoughts didn't escape brainstorm. However, I made a proof-of-concept, from scratch, starting from /just/ the bare minimum:
/* Synopsis:
*
* LOAD DLL <file-spec>
* LOAD PDB <file-spec>
* SET DEBUG {/ON | /OFF}
* SET TRACE {/ON | /OFF}
*
* SHOW DEBUG
* SHOW MODULE <wildcard-expression> [/SYMBOLS]
* SHOW SYMBOLS { [/ALL] [/FULL] [/OUT=<file-spec>] [/TYPE=(+{all,exports,imports})] [wild-card-expression] }...
* SHOW TRACE
*/
Utilities
Since we have several domains that can have sets of options that are to be treated as (case-insensitive) keyword identifiers, I thought of creating a facility for those:
Note: for brevity this keeps all values as int for now. In that, it falls short of "Better Enum". But given a few macros you should be able to make Options::type (and Enum<TagType>) resolve to a proper enum type.
namespace util {
template <typename Tag> struct FlavouredString : std::string {
using std::string::string;
using std::string::operator=;
};
template <typename Tag> struct Options {
using type = int; // TODO typed enums? Requires macro tedium
std::vector<char const*> _options;
Options(std::initializer_list<char const*> options) : _options(options) {}
Options(std::vector<char const*> options) : _options(options) {}
std::string to_string(type id) const { return _options.at(id); }
type to_id(std::string const& name) const { return find(_options.begin(), _options.end(), name) - _options.begin(); }
};
template <typename Tag> using Enum = typename Options<Tag>::type;
template <typename Tag> struct IcOptions : Options<Tag> { using Options<Tag>::Options; };
}
To support our AST types, we will create instances of these utilities like:
IcOptions<struct DllPdb> static const dll_pdb { "DLL", "PDB" };
IcOptions<struct Setting> static const setting { "DEBUG", "TRACE" };
IcOptions<struct OnOff> static const on_off { "OFF", "ON" };
IcOptions<struct SymType> static const sym_type{ "all", "imports", "exports" };
using Wildcard = FlavouredString<struct _Wild>;
using Filespec = FlavouredString<struct _Filespec>;
AST Types
This goes a completely different route from before: instead of defining a general-purpose AST with arbitrary numbers of arbitrary-type arguments and values, I've opted to define the commands strongly-typed:
namespace ast {
struct LoadCommand {
Enum<DllPdb> kind = {};
Filespec filespec;
};
struct SetCommand {
Enum<Setting> setting = {};
Enum<OnOff> value = {};
};
struct ShowSettingCommand {
Enum<Setting> setting;
};
struct ShowModuleCommand {
Wildcard wildcard;
bool symbols = false;
};
using SymbolTypes = std::vector<Enum<SymType> >;
struct ShowSymbolsCommand {
bool all = false;
bool full = false;
Filespec out;
SymbolTypes types;
Wildcard wildcard;
};
using Command = boost::variant<
LoadCommand,
SetCommand,
ShowSettingCommand,
ShowModuleCommand,
ShowSymbolsCommand
>;
}
Adaptation is like before:
BOOST_FUSION_ADAPT_STRUCT(scl::ast::LoadCommand, kind, filespec)
BOOST_FUSION_ADAPT_STRUCT(scl::ast::SetCommand, setting, value)
BOOST_FUSION_ADAPT_STRUCT(scl::ast::ShowSettingCommand, setting)
BOOST_FUSION_ADAPT_STRUCT(scl::ast::ShowModuleCommand, wildcard, symbols)
Note that ShowSymbolsCommand is not adapted because the rule doesn't follow the struct layout
Parser Utilities
Let's support our core concepts with some composable parser factories:
// (case insensitive) keyword handling
static auto kw = [](auto p) { return x3::lexeme[p >> !(x3::graph - x3::char_("/=,()"))]; };
static auto ikw = [](auto p) { return x3::no_case [kw(p)]; };
static auto qualifier = [](auto p) { return x3::lexeme['/' >> ikw(p)]; };
I could explain these, but the usage below will be more clear. So, without further ado, presenting the trick that allows us to use any Options or CiOptions instance directly in a parser expression:
// Options and CiOptions
namespace util {
template <typename Tag>
auto as_spirit_parser(Options<Tag> const& o, bool to_lower = false) {
x3::symbols<typename Options<Tag>::type> p;
int n = 0;
for (std::string el : o._options) {
if (to_lower) boost::to_lower(el);
p.add(el, n++);
}
return kw(p);
}
template <typename Tag>
auto as_spirit_parser(IcOptions<Tag> const& o) {
return x3::no_case [ as_spirit_parser(o, true) ];
}
}
Nothing unexpected there, I suppose, but it does allow for elegant rule definitions:
The Rule Definitions
DEF_RULE(Filespec) = quoted_string | bare_string;
DEF_RULE(Wildcard) = lexeme[+char_("a-zA-Z0-9$_.\\*?+-")];
DEF_RULE(LoadCommand)
= ikw("load") >> ast::dll_pdb >> Filespec;
DEF_RULE(SetCommand)
= ikw("set") >> ast::setting >> qualifier(ast::on_off);
DEF_RULE(ShowSettingCommand)
= ikw("show") >> ast::setting;
DEF_RULE(ShowModuleCommand)
= ikw("show") >> ikw("module") >> Wildcard >> matches[qualifier("symbols")];
// ... ShowSymbolsQualifiers (see below) ...
DEF_RULE(ShowSymbolsCommand)
= ikw("show") >> ikw("symbols") >> *ShowSymbolsQualifiers;
DEF_RULE(Command)
= skip(blank)[ LoadCommand | SetCommand | ShowSettingCommand | ShowModuleCommand | ShowSymbolsCommand ];
The Heavier Lifting
You'll note I skipped ShowSymbolsQualifiers. That's because that's the only rule that cannot benefit from automatic attribute propagation, so I've resorted to using semantic actions:
Note the IIFE idiom allows for "very local" helper definitions
DEF_RULE(SymbolTypes) = [] {
auto type = as_parser(ast::sym_type);
return '(' >> (type % ',') >> ')' | repeat(1) [ type ];
}(); // IIFE pattern
RULE(ShowSymbolsQualifiers, ShowSymbolsCommand)
= [] {
auto set = [](auto member, auto p) {
auto propagate = [member](auto& ctx) {
traits::move_to(_attr(ctx), _val(ctx).*(member));
};
return as_parser(p)[propagate];
};
using T = ast::ShowSymbolsCommand;;
return qualifier("all") >> set(&T::all, attr(true))
| qualifier("full") >> set(&T::full, attr(true))
| qualifier("out") >> set(&T::out, '=' >> Filespec)
| qualifier("type") >> set(&T::types, '=' >> SymbolTypes)
| set(&T::wildcard, Wildcard);
}(); // IIFE pattern
FULL DEMO
Live On Coliru
//#define BOOST_SPIRIT_X3_DEBUG
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/case_conv.hpp> // to_lower
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/home/x3.hpp>
/* Synopsis:
*
* LOAD DLL <file-spec>
* LOAD PDB <file-spec>
* SET DEBUG {/ON | /OFF}
* SET TRACE {/ON | /OFF}
*
* SHOW DEBUG
* SHOW MODULE <wildcard-expression> [/SYMBOLS]
* SHOW SYMBOLS { [/ALL] [/FULL] [/OUT=<file-spec>] [/TYPE=(+{all,exports,imports})] [wild-card-expression] }...
* SHOW TRACE
*/
namespace scl {
namespace util {
template <typename Tag> struct FlavouredString : std::string {
using std::string::string;
using std::string::operator=;
};
template <typename Tag> struct Options {
using type = int; // TODO typed enums? Requires macro tedium
std::vector<char const*> _options;
Options(std::initializer_list<char const*> options) : _options(options) {}
Options(std::vector<char const*> options) : _options(options) {}
std::string to_string(type id) const { return _options.at(id); }
type to_id(std::string const& name) const { return find(_options.begin(), _options.end(), name) - _options.begin(); }
};
template <typename Tag> using Enum = typename Options<Tag>::type;
template <typename Tag> struct IcOptions : Options<Tag> { using Options<Tag>::Options; };
}
namespace ast {
using namespace util;
IcOptions<struct DllPdb> static const dll_pdb { "DLL", "PDB" };
IcOptions<struct Setting> static const setting { "DEBUG", "TRACE" };
IcOptions<struct OnOff> static const on_off { "OFF", "ON" };
IcOptions<struct SymType> static const sym_type{ "all", "imports", "exports" };
using Wildcard = FlavouredString<struct _Wild>;
using Filespec = FlavouredString<struct _Filespec>;
struct LoadCommand {
Enum<DllPdb> kind = {};
Filespec filespec;
};
struct SetCommand {
Enum<Setting> setting = {};
Enum<OnOff> value = {};
};
struct ShowSettingCommand {
Enum<Setting> setting;
};
struct ShowModuleCommand {
Wildcard wildcard;
bool symbols = false;
};
using SymbolTypes = std::vector<Enum<SymType> >;
struct ShowSymbolsCommand {
bool all = false;
bool full = false;
Filespec out;
SymbolTypes types;
Wildcard wildcard;
};
using Command = boost::variant<
LoadCommand,
SetCommand,
ShowSettingCommand,
ShowModuleCommand,
ShowSymbolsCommand
>;
}
}
#ifndef NDEBUG // for debug printing
namespace scl { namespace ast {
static inline std::ostream &operator<<(std::ostream &os, Wildcard const &w) { return os << std::quoted(w); }
static inline std::ostream &operator<<(std::ostream &os, Filespec const &s) { return os << std::quoted(s); }
static inline std::ostream &operator<<(std::ostream &os, LoadCommand const &cmd) {
return os << "LOAD " << dll_pdb.to_string(cmd.kind) << " " << cmd.filespec ;
}
static inline std::ostream &operator<<(std::ostream &os, SetCommand const &cmd) {
return os << "SET " << setting.to_string(cmd.setting) << " /" << on_off.to_string(cmd.value);
}
static inline std::ostream &operator<<(std::ostream &os, ShowSettingCommand const &cmd) {
return os << "SHOW " << setting.to_string(cmd.setting);
}
static inline std::ostream &operator<<(std::ostream &os, ShowModuleCommand const &cmd) {
return os << "SHOW MODULE " << cmd.wildcard << (cmd.symbols?" /SYMBOLS":"");
}
static inline std::ostream &operator<<(std::ostream &os, ShowSymbolsCommand const &cmd) {
os << "SHOW SYMBOLS";
if (cmd.all) os << " /ALL";
if (cmd.full) os << " /FULL";
if (cmd.out.size()) os << " /OUT=" << cmd.out;
if (cmd.types.size()) {
os << " /TYPE=(";
bool first = true;
for (auto type : cmd.types)
os << (std::exchange(first, false)?"":",") << sym_type.to_string(type);
os << ")";
}
return os << " " << cmd.wildcard;
}
} }
#endif
BOOST_FUSION_ADAPT_STRUCT(scl::ast::LoadCommand, kind, filespec)
BOOST_FUSION_ADAPT_STRUCT(scl::ast::SetCommand, setting, value)
BOOST_FUSION_ADAPT_STRUCT(scl::ast::ShowSettingCommand, setting)
BOOST_FUSION_ADAPT_STRUCT(scl::ast::ShowModuleCommand, wildcard, symbols)
// Grammar for simple command language
namespace scl {
namespace x3 = boost::spirit::x3;
// (case insensitive) keyword handling
static auto kw = [](auto p) { return x3::lexeme[p >> !(x3::graph - x3::char_("/=,()"))]; };
static auto ikw = [](auto p) { return x3::no_case [kw(p)]; };
static auto qualifier = [](auto p) { return x3::lexeme['/' >> ikw(p)]; };
// Options and CiOptions
namespace util {
template <typename Tag>
auto as_spirit_parser(Options<Tag> const& o, bool to_lower = false) {
x3::symbols<typename Options<Tag>::type> p;
int n = 0;
for (std::string el : o._options) {
if (to_lower) boost::to_lower(el);
p.add(el, n++);
}
return kw(p);
}
template <typename Tag>
auto as_spirit_parser(IcOptions<Tag> const& o) {
return x3::no_case [ as_spirit_parser(o, true) ];
}
}
// shorthand rule declarations
#define RULE(name, Attr) static auto const name = x3::rule<struct _##Attr, ast::Attr>{#Attr}
#define DEF_RULE(Attr) RULE(Attr, Attr)
using namespace x3;
auto const bare_string
= lexeme[+char_("a-zA-Z0-9$_.\\*?+-")]; // bare string taken from old "param" rule
auto const quoted_string
= lexeme['"' >> *(('\\' > char_) | ~char_('"')) >> '"'];
DEF_RULE(Filespec) = quoted_string | bare_string;
DEF_RULE(Wildcard) = lexeme[+char_("a-zA-Z0-9$_.\\*?+-")];
DEF_RULE(LoadCommand)
= ikw("load") >> ast::dll_pdb >> Filespec;
DEF_RULE(SetCommand)
= ikw("set") >> ast::setting >> qualifier(ast::on_off);
DEF_RULE(ShowSettingCommand)
= ikw("show") >> ast::setting;
DEF_RULE(ShowModuleCommand)
= ikw("show") >> ikw("module") >> Wildcard >> matches[qualifier("symbols")];
// Note the IIFE idiom allows for "very local" helper definitions
DEF_RULE(SymbolTypes) = [] {
auto type = as_parser(ast::sym_type);
return '(' >> (type % ',') >> ')' | repeat(1) [ type ];
}(); // IIFE idiom
RULE(ShowSymbolsQualifiers, ShowSymbolsCommand)
= [] {
auto set = [](auto member, auto p) {
auto propagate = [member](auto& ctx) {
traits::move_to(_attr(ctx), _val(ctx).*(member));
};
return as_parser(p)[propagate];
};
using T = ast::ShowSymbolsCommand;;
return qualifier("all") >> set(&T::all, attr(true))
| qualifier("full") >> set(&T::full, attr(true))
| qualifier("out") >> set(&T::out, '=' >> Filespec)
| qualifier("type") >> set(&T::types, '=' >> SymbolTypes)
| set(&T::wildcard, Wildcard);
}(); // IIFE idiom
DEF_RULE(ShowSymbolsCommand)
= ikw("show") >> ikw("symbols") >> *ShowSymbolsQualifiers;
DEF_RULE(Command)
= skip(blank)[ LoadCommand | SetCommand | ShowSettingCommand | ShowModuleCommand | ShowSymbolsCommand ];
#undef DEF_RULE
#undef RULE
} // End namespace scl
int main() {
for (std::string const str : {
"load dll test.dll",
"LOAD pdb \"test special.pdb\"",
"LOAD pDb test.pdb",
"set debug /on",
"show debug",
"SHOW module test.dll/symbols",
"SHOW MODULE TEST.DLL /SYMBOLS",
"SHOW module test.dll / symbols",
"SHOW module test.dll",
"show symbols/type=exports test*",
"show symbols/type=(exports,imports) test*",
"show symbols test.dll/type=(imports,exports)",
"show symbols test.dll/tyPE=(imports,exports)",
"show symbols s*/out=s.txt",
"show symbols /all /full",
}) {
std::cout << " ======== " << std::quoted(str) << std::endl;
auto b = str.begin(), e = str.end();
scl::ast::Command cmd;
if (parse(b, e, scl::Command, cmd))
std::cout << " - Parsed: " << cmd << std::endl;
if (b != e)
std::cout << " - Remaining unparsed: " << std::quoted(std::string(b, e)) << std::endl;
}
}
Prints
======== "load dll test.dll"
- Parsed: LOAD DLL "test.dll"
======== "LOAD pdb \"test special.pdb\""
- Parsed: LOAD PDB "test special.pdb"
======== "LOAD pDb test.pdb"
- Parsed: LOAD PDB "test.pdb"
======== "set debug /on"
- Parsed: SET DEBUG /ON
======== "show debug"
- Parsed: SHOW DEBUG
======== "SHOW module test.dll/symbols"
- Parsed: SHOW MODULE "test.dll" /SYMBOLS
======== "SHOW MODULE TEST.DLL /SYMBOLS"
- Parsed: SHOW MODULE "TEST.DLL" /SYMBOLS
======== "SHOW module test.dll / symbols"
- Parsed: SHOW MODULE "test.dll"
- Remaining unparsed: "/ symbols"
======== "SHOW module test.dll"
- Parsed: SHOW MODULE "test.dll"
======== "show symbols/type=exports test*"
- Parsed: SHOW SYMBOLS /TYPE=(exports) "test*"
======== "show symbols/type=(exports,imports) test*"
- Parsed: SHOW SYMBOLS /TYPE=(exports,imports) "test*"
======== "show symbols test.dll/type=(imports,exports)"
- Parsed: SHOW SYMBOLS /TYPE=(imports,exports) "test.dll"
======== "show symbols test.dll/tyPE=(imports,exports)"
- Parsed: SHOW SYMBOLS /TYPE=(imports,exports) "test.dll"
======== "show symbols s*/out=s.txt"
- Parsed: SHOW SYMBOLS /OUT="s.txt" "s*"
======== "show symbols /all /full"
- Parsed: SHOW SYMBOLS /ALL /FULL ""

Overload operator<<

I would like to overload operator<< like this:
ostringstream oss;
MyDate a(2000, 1, 2);
oss << dateFormat("%Y/%m/%d") << a;
assert(oss.str() == "2000-01-02");
so that the date at a will be formatted to specific format. How to achieve this?
In order to store custom state in a stream, you need to use the xalloc static function to get a unique index, and then either pword to get a pointer at that index (allocated specifically for each stream it is used on), or iword to get an integer at that index(allocated specifically for each stream it is used on). In your case, you will probably want pword. You can use the pointer returned by pword to point to a dynamically allocated object which stores the formatting information.
struct DateFormatter
{
// The implementation of this class (e.g. parsing the format string)
// is a seperate issue. If you need help with it, you can ask another
// question
static int xalloc_index;
};
int DateFormatter::xalloc_index = std::ios_base::xalloc();
void destroy_date_formatter(std::ios_base::event evt, std::ios_base& io, int idx)
{
if (evt == std::ios_base::erase_event) {
void*& vp = io.pword(DateFormatter::xalloc_index);
delete (DateFormatter*)(vp);
}
}
DateFormatter& get_date_formatter(std::ios_base& io) {
void*& vp = io.pword(DateFormatter::xalloc_index);
if (!vp) {
vp = new DateFormatter;
io.register_callback(destroy_date_formatter, 0);
}
return *static_cast<DateFormatter*>(vp);
}
std::ostream& operator<<(std::ostream& os, const DateFormatter& df) {
get_date_formatter(os) = df;
return os;
}
std::ostream& operator<<(std::ostream& os, const MyDate& date)
{
DateFormatter& df = get_date_formatter(os);
// format output according to df
return os;
}
int main() {
MyDate a ( 2000, 1, 2 );
std::cout << DateFormatter("%Y/%m/%d") << a;
}
This is the standard method. It is terrible, in my opinion. I much prefer an alternative approach, which is to pass the date object together with the formatting as a single object. For example:
class DateFormatter
{
const MyDate* date;
std::string format_string;
DateFormatter(const MyDate& _date, std::string _format_string)
:date(&_date)
,format_string(_format_string)
{}
friend std::ostream& operator<<(std::ostream& os, const DateFormatter& df) {
// handle formatting details here
return os;
}
};
int main() {
MyDate a ( 2000, 1, 2 );
std::cout << DateFormatter(a, "%Y/%m/%d");
}
Or you can do something like that (using static variable):
#include <iostream>
struct MyDate
{
MyDate(int y, int m, int d): year{y}, month{m}, day{d} {}
int year{};
int month{};
int day{};
};
class DateFormatter
{
public:
DateFormatter(const std::string & format)
{
format_ = format;
}
static const std::string & format()
{
return format_;
}
private:
static std::string format_;
};
std::string DateFormatter::format_ = {"Default Format"};
std::ostream & operator<< (std::ostream & stream, const DateFormatter &)
{
return stream;
}
std::ostream & operator<< (std::ostream & stream, const MyDate & date)
{
auto currentFormat = DateFormatter::format();
// some code using current format ...
return stream << currentFormat << " - " << date.year << "/" << date.month << "/" << date.day;
}
int main(void)
{
MyDate date{2016,4,18};
std::cout << date << std::endl;
std::cout << DateFormatter("New format") << date << std::endl;
return 0;
}

Is it possible to define a variable that can be set only once?

I know of const, that can't be changed after creation. But I was wondering if there is a way to declare a variable that you set only once and after that, can't overwrite.
In my code, I would like to avoid the bool variable by having an nFirst that, once set to nIdx, can't be set to the new value of nIdx.
My code:
int nFirst = 0;
int nIdx = 0;
bool bFound = false;
BOOST_FOREACH(Foo* pFoo, aArray)
{
if (pFoo!= NULL)
{
pFoo->DoSmth();
if (!bFound)
{
nFirst= nIdx;
bFound = true;
}
}
nIdx++;
}
Pretty easy to roll your own.
template<typename T>
class SetOnce
{
public:
SetOnce(T init) : m_Val(init)
{}
SetOnce<T>& operator=(const T& other)
{
std::call_once(m_OnceFlag, [&]()
{
m_Val = other;
});
return *this;
}
const T& get() { return m_Val; }
private:
T m_Val;
std::once_flag m_OnceFlag;
};
Then just use the wrapper class for your variable.
SetOnce<int> nFirst(0);
nFirst= 1;
nFirst= 2;
nFirst= 3;
std::cout << nFirst.get() << std::endl;
Outputs:
1
I would like to avoid the bool variable
You can check nFirst itself, based on the fact that it won't be set a negative number. Such as:
int nFirst = -1;
int nIdx = 0;
BOOST_FOREACH(Foo* pFoo, aArray)
{
if (pFoo != NULL)
{
pFoo->DoSmth();
if (nFirst == -1)
{
nFirst = nIdx;
}
}
nIdx++;
}
Similar to cocarin's, but throws exception instead of silently ignoring assignment:
template <typename T, typename Counter = unsigned char>
class SetOnce {
public:
SetOnce(const T& initval = T(), const Counter& initcount = 1):
val(initval), counter(initcount) {}
SetOnce(const SetOnce&) = default;
SetOnce<T, Counter>& operator=(const T& newval) {
if (counter) {
--counter;
val = newval;
return *this;
}
else throw "Some error";
}
operator const T&() const { return val; } // "getter"
protected:
T val;
Counter counter;
};
Usage:
SetOnce<int> x = 42;
std::cout << x << '\n'; // => 42
x = 4;
// x = 5; // fails
std::cout << x << '\n'; // => 4
Online demo
Your question is about avoiding the bool but also implies the need for const-ness.
To avoid the bool, I'd use a boost::optional like this:
boost::optional<int> nFirst;
// ..
if (!nFirst) nFirst = nIdx;
// and now you can use *nFirst to get its value
Then, you can enforce logical (rather than literal) const-ness like this:
const boost::optional<int> nFirst;
// ..
if (!nFirst) const_cast<boost::optional<int>&>(nFirst) = nIdx;
// you can use *nFirst to get the value, any attempt to change it would cause a compile-time error
Using const_cast is not the safest practice, but in your particular case and as long as you only do it once it'd be OK. It simplifies both your code and your intentions: you do want a const, it's just that you want to defer it's initialisation for a bit.
Now, as songyuanyao suggested, you could directly use an int instead of a boost::optional, but the latter makes your intention explicit so I think it's better this way. In the end of day this is C++ while songyuanyao's solution is really a C-style one.
This is set once template. You can use this class as assurance that the value will be set and saved only once. Every next try will be canceled.
#include <iostream>
using namespace std;
template <class T>
class SetOnce;
template<class T>
std::ostream& operator<<( ostream& os, const SetOnce<T>& Obj );
template <class T>
class SetOnce
{
public:
SetOnce() {set = false; }
~SetOnce() {}
void SetValue(T newValue) { value = !set ? newValue : value; set = true; }
private:
T value;
bool set;
friend std::ostream& operator<< <>( ostream& os, const SetOnce& Obj );
public:
SetOnce<T>& operator=( const T& newValue )
{
this->SetValue(newValue);
return *this;
}
};
template<class T>
std::ostream& operator<<( ostream& os, const SetOnce<T>& Obj )
{
os << Obj.value;
return os;
}
Use case:
int main()
{
SetOnce<bool> bvar;
SetOnce<int> ivar;
SetOnce<std::string> strvar;
std::cout<<"initial values: \n"<<bvar<<" "
<<ivar<<" "<<strvar<<" \n\n";
bvar = false; //bvar.SetValue(false);
ivar = 45; //ivar.SetValue(45);
strvar = "Darth Vader"; //strvar.SetValue("Darth Vader");
std::cout<<"set values: \n"<<bvar<<" "
<<ivar<<" "<<strvar<<" \n\n";
bvar = true; //bvar.SetValue(true);
ivar = 0; //ivar.SetValue(0);
strvar = "Anakin"; //strvar.SetValue("Anakin");
std::cout<<"set again values: \n"<<bvar<<" "
<<ivar<<" "<<strvar<<" \n\n";
return 0;
}