Skip to content

Commit

Permalink
Debugger: Implement float value editor.
Browse files Browse the repository at this point in the history
FloatValueFormatter:
- Implement parsing/validation hooks.

FloatValueHandler:
- Implement GetTableCellValueEditor() to accordingly return a
  (newly implemented) TableCellFloatEditor. This allows variable
  value editing for float/double variables.
  • Loading branch information
anevilyak committed Jul 24, 2015
1 parent 5c5c251 commit f5d564a
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/apps/debugger/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ local sources =
# user_interface/gui/value
TableCellBoolEditor.cpp
TableCellEnumerationEditor.cpp
TableCellFloatEditor.cpp
TableCellFormattedValueEditor.cpp
TableCellFormattedValueRenderer.cpp
TableCellIntegerEditor.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/

#include "TableCellFloatEditor.h"

#include <ctype.h>

#include <Variant.h>

#include "IntegerValue.h"
#include "IntegerValueFormatter.h"


TableCellFloatEditor::TableCellFloatEditor(::Value* initialValue,
ValueFormatter* formatter)
:
TableCellTextControlEditor(initialValue, formatter)
{
}


TableCellFloatEditor::~TableCellFloatEditor()
{
}


bool
TableCellFloatEditor::ValidateInput() const
{
BVariant variantValue;
if (!InitialValue()->ToVariant(variantValue))
return false;

return GetValueFormatter()->ValidateFormattedValue(Text(),
variantValue.Type());
}


status_t
TableCellFloatEditor::GetValueForInput(::Value*& _output) const
{
BVariant variantValue;
if (!InitialValue()->ToVariant(variantValue))
return B_NO_MEMORY;

return GetValueFormatter()->GetValueFromFormattedInput(Text(),
variantValue.Type(), _output);
}


25 changes: 25 additions & 0 deletions src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2015, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_FLOAT_EDITOR_H
#define TABLE_CELL_FLOAT_EDITOR_H

#include <TextControl.h>

#include "TableCellTextControlEditor.h"


class TableCellFloatEditor : public TableCellTextControlEditor {
public:
TableCellFloatEditor(::Value* initialValue,
ValueFormatter* formatter);
virtual ~TableCellFloatEditor();

virtual bool ValidateInput() const;

virtual status_t GetValueForInput(::Value*& _output) const;
// returns reference
};

#endif // TABLE_CELL_INTEGER_EDITOR_H
77 changes: 76 additions & 1 deletion src/apps/debugger/value/value_formatters/FloatValueFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/
#include "FloatValueFormatter.h"

#include <new>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "FloatValue.h"

Expand All @@ -30,9 +34,80 @@ FloatValueFormatter::FormatValue(Value* _value, BString& _output)
return B_BAD_VALUE;

char buffer[64];
snprintf(buffer, sizeof(buffer), "%g", value->GetValue());
BVariant variantValue = value->GetValue();
switch (variantValue.Type()) {
case B_FLOAT_TYPE:
{
snprintf(buffer, sizeof(buffer), "%f", variantValue.ToFloat());
break;
}
case B_DOUBLE_TYPE:
{
snprintf(buffer, sizeof(buffer), "%g", variantValue.ToDouble());
break;
}
}

_output.SetTo(buffer);

return B_OK;
}


bool
FloatValueFormatter::SupportsValidation() const
{
return true;
}


bool
FloatValueFormatter::ValidateFormattedValue(const BString& input,
type_code type) const
{
::Value* value = NULL;
return _PerformValidation(input, type, value, false) == B_OK;
}


status_t
FloatValueFormatter::GetValueFromFormattedInput(const BString& input,
type_code type, Value*& _output) const
{
return _PerformValidation(input, type, _output, true);
}


status_t
FloatValueFormatter::_PerformValidation(const BString& input, type_code type,
::Value*& _output, bool wantsValue) const
{
const char* text = input.String();
char *parseEnd = NULL;
double parsedValue = strtod(text, &parseEnd);
if (parseEnd - text < input.Length() && !isspace(*parseEnd))
return B_NO_MEMORY;

BVariant newValue;
switch (type) {
case B_FLOAT_TYPE:
{
newValue.SetTo((float)parsedValue);
break;
}
case B_DOUBLE_TYPE:
{
newValue.SetTo(parsedValue);
break;
}
default:
return B_BAD_VALUE;
}
if (wantsValue) {
_output = new(std::nothrow) FloatValue(newValue);
if (_output == NULL)
return B_NO_MEMORY;
}

return B_OK;
}
16 changes: 16 additions & 0 deletions src/apps/debugger/value/value_formatters/FloatValueFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ class FloatValueFormatter : public ValueFormatter {
{ return NULL; }

virtual status_t FormatValue(Value* value, BString& _output);

virtual bool SupportsValidation() const;

virtual bool ValidateFormattedValue(
const BString& input,
type_code type) const;

virtual status_t GetValueFromFormattedInput(
const BString& input, type_code type,
Value*& _output) const;
private:

status_t _PerformValidation(const BString& input,
type_code type,
::Value*& _output,
bool wantsValue) const;
};


Expand Down
31 changes: 31 additions & 0 deletions src/apps/debugger/value/value_handlers/FloatValueHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "FloatValue.h"
#include "FloatValueFormatter.h"
#include "TableCellFloatEditor.h"
#include "TableCellFormattedValueRenderer.h"


Expand Down Expand Up @@ -75,3 +76,33 @@ FloatValueHandler::GetTableCellValueRenderer(Value* value,
_renderer = renderer;
return B_OK;
}


status_t
FloatValueHandler::GetTableCellValueEditor(Value* _value, Settings* settings,
TableCellValueEditor*& _editor)
{
FloatValue* value = dynamic_cast<FloatValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;

ValueFormatter* formatter;
status_t error = GetValueFormatter(value, formatter);
if (error != B_OK)
return error;
BReference<ValueFormatter> formatterReference(formatter, true);

TableCellFloatEditor* editor = new(std::nothrow)
TableCellFloatEditor(value, formatter);
if (editor == NULL)
return B_NO_MEMORY;

BReference<TableCellFloatEditor> editorReference(editor, true);
error = editor->Init();
if (error != B_OK)
return error;

editorReference.Detach();
_editor = editor;
return B_OK;
}
3 changes: 3 additions & 0 deletions src/apps/debugger/value/value_handlers/FloatValueHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class FloatValueHandler : public ValueHandler {
ValueFormatter*& _formatter);
virtual status_t GetTableCellValueRenderer(Value* value,
TableCellValueRenderer*& _renderer);
virtual status_t GetTableCellValueEditor(Value* value,
Settings* settings,
TableCellValueEditor*& _editor);
};


Expand Down

0 comments on commit f5d564a

Please sign in to comment.