Clean up the DebugHint class

This commit is contained in:
Gunnar Beutner 2014-11-17 10:34:11 +01:00 committed by Gunnar Beutner
parent 77b746841a
commit ea729b2b6c
3 changed files with 60 additions and 46 deletions

View File

@ -188,11 +188,13 @@ void ObjectListCommand::PrintHints(std::ostream& fp, const Dictionary::Ptr& debu
Array::Ptr messages = debug_hints->Get("messages");
if (messages) {
ObjectLock olock(messages);
BOOST_FOREACH(const Value& msg, messages) {
PrintHint(fp, msg, indent);
}
}
}
void ObjectListCommand::PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent)

View File

@ -282,7 +282,8 @@ Value DictExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) c
Value SetExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
DebugHint *sdhint = dhint;
DebugHint *psdhint = dhint;
DebugHint sdhint;
Value parent, object;
String index;
@ -291,8 +292,10 @@ Value SetExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) co
Expression *indexExpr = m_Indexer[i];
String tempindex = indexExpr->Evaluate(context, dhint);
if (sdhint)
sdhint = sdhint->GetChild(tempindex);
if (psdhint) {
sdhint = psdhint->GetChild(tempindex);
psdhint = &sdhint;
}
if (i == 0)
parent = context;
@ -311,7 +314,7 @@ Value SetExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) co
LiteralExpression *eindex = MakeLiteral(tempindex);
IndexerExpression eip(eparent, eindex, m_DebugInfo);
object = eip.Evaluate(context, sdhint);
object = eip.Evaluate(context, psdhint);
if (i != m_Indexer.size() - 1 && object.IsEmpty()) {
object = new Dictionary();
@ -346,8 +349,8 @@ Value SetExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) co
SetField(parent, index, right);
if (sdhint)
sdhint->AddMessage("=", m_DebugInfo);
if (psdhint)
psdhint->AddMessage("=", m_DebugInfo);
return right;
}
@ -524,37 +527,6 @@ Value ForExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) co
return Empty;
}
Dictionary::Ptr DebugHint::ToDictionary(void) const
{
Dictionary::Ptr result = new Dictionary();
Array::Ptr messages = new Array();
typedef std::pair<String, DebugInfo> MessageType;
BOOST_FOREACH(const MessageType& message, Messages) {
Array::Ptr amsg = new Array();
amsg->Add(message.first);
amsg->Add(message.second.Path);
amsg->Add(message.second.FirstLine);
amsg->Add(message.second.FirstColumn);
amsg->Add(message.second.LastLine);
amsg->Add(message.second.LastColumn);
messages->Add(amsg);
}
result->Set("messages", messages);
Dictionary::Ptr properties = new Dictionary();
typedef std::map<String, DebugHint>::value_type ChildType;
BOOST_FOREACH(const ChildType& kv, Children) {
properties->Set(kv.first, kv.second.ToDictionary());
}
result->Set("properties", properties);
return result;
}
bool Expression::HasField(const Object::Ptr& context, const String& field)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);

View File

@ -31,20 +31,60 @@ namespace icinga
struct DebugHint
{
std::vector<std::pair<String, DebugInfo> > Messages;
std::map<String, DebugHint> Children;
public:
DebugHint(const Dictionary::Ptr& hints = Dictionary::Ptr())
: m_Hints(hints)
{ }
inline void AddMessage(const String& message, const DebugInfo& di)
{
Messages.push_back(std::make_pair(message, di));
if (!m_Hints)
m_Hints = new Dictionary();
if (!m_Messages) {
m_Messages = new Array();
m_Hints->Set("messages", m_Messages);
}
inline DebugHint *GetChild(const String& name)
Array::Ptr amsg = new Array();
amsg->Add(message);
amsg->Add(di.Path);
amsg->Add(di.FirstLine);
amsg->Add(di.FirstColumn);
amsg->Add(di.LastLine);
amsg->Add(di.LastColumn);
m_Messages->Add(amsg);
}
inline DebugHint GetChild(const String& name)
{
return &Children[name];
if (!m_Hints)
m_Hints = new Dictionary();
if (!m_Children) {
m_Children = new Dictionary;
m_Hints->Set("properties", m_Children);
}
Dictionary::Ptr ToDictionary(void) const;
Dictionary::Ptr child = m_Children->Get(name);
if (!child) {
child = new Dictionary();
m_Children->Set(name, child);
}
return DebugHint(child);
}
Dictionary::Ptr ToDictionary(void) const
{
return m_Hints;
}
private:
Dictionary::Ptr m_Hints;
Array::Ptr m_Messages;
Dictionary::Ptr m_Children;
};
enum CombinedSetOp