Debugger: Refactor + resolve TODO.

- Introduce interface class ValueFormatter. This one takes on the
responsibilities of formatting a value into a string, which were
previously embedded within the various TableCellValueRenderer
subclasses.
- Add implementing subclasses for the various value types.
- Introduce TableCellValueRenderer subclass TableCellFormattedValueRenderer.
  This is a simple TableCellValueRenderer implementation for the simple case
  of a renderer that does nothing more than use a formatter to present a
  string version of its corresponding Value. Since this describes all existing
  renderers, this renders them obsolete.
- Refactor the respective ValueHandler subclasses to make use of the formatters
  and new rendererer subclass.
This commit is contained in:
Rene Gollent
2015-06-13 16:57:09 -04:00
parent 7589a9affa
commit 5f5499b40b
32 changed files with 990 additions and 527 deletions
+10 -6
View File
@@ -47,6 +47,7 @@ SEARCH_SOURCE += [ FDirName $(SUBDIR) user_interface util ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) util ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) value ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) value type_handlers ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) value value_formatters ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) value value_handlers ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) value value_nodes ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) value values ] ;
@@ -299,12 +300,7 @@ Application Debugger :
WatchPromptWindow.cpp
# user_interface/gui/value
TableCellBoolRenderer.cpp
TableCellEnumerationRenderer.cpp
TableCellFloatRenderer.cpp
TableCellIntegerRenderer.cpp
TableCellStringRenderer.cpp
TableCellValueEditor.cpp
TableCellFormattedValueRenderer.cpp
TableCellValueRenderer.cpp
TableCellValueRendererUtils.cpp
@@ -323,6 +319,7 @@ Application Debugger :
TypeHandler.cpp
TypeHandlerRoster.cpp
Value.cpp
ValueFormatter.cpp
ValueHandler.cpp
ValueHandlerRoster.cpp
ValueLoader.cpp
@@ -335,6 +332,13 @@ Application Debugger :
BMessageTypeHandler.cpp
CStringTypeHandler.cpp
# value/value_formatters
BoolValueFormatter.cpp
EnumerationValueFormatter.cpp
IntegerValueFormatter.cpp
FloatValueFormatter.cpp
StringValueFormatter.cpp
# value/value_handlers
AddressValueHandler.cpp
BoolValueHandler.cpp
@@ -1,43 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "TableCellBoolRenderer.h"
#include "BoolValue.h"
#include "TableCellValueRendererUtils.h"
static inline const char*
bool_value_string(BoolValue* value)
{
return value->GetValue() ? "true" : "false";
}
void
TableCellBoolRenderer::RenderValue(Value* _value, bool valueChanged,
BRect rect, BView* targetView)
{
BoolValue* value = dynamic_cast<BoolValue*>(_value);
if (value == NULL)
return;
TableCellValueRendererUtils::DrawString(targetView, rect,
bool_value_string(value), valueChanged, B_ALIGN_RIGHT, true);
}
float
TableCellBoolRenderer::PreferredValueWidth(Value* _value, BView* targetView)
{
BoolValue* value = dynamic_cast<BoolValue*>(_value);
if (value == NULL)
return 0;
return TableCellValueRendererUtils::PreferredStringWidth(targetView,
bool_value_string(value));
}
@@ -1,24 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_BOOL_RENDERER_H
#define TABLE_CELL_BOOL_RENDERER_H
#include <Referenceable.h>
#include "TableCellValueRenderer.h"
class TableCellBoolRenderer : public TableCellValueRenderer {
public:
virtual void RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView);
virtual float PreferredValueWidth(Value* value,
BView* targetView);
};
#endif // TABLE_CELL_BOOL_RENDERER_H
@@ -1,63 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "TableCellEnumerationRenderer.h"
#include "EnumerationValue.h"
#include "TableCellValueRendererUtils.h"
#include "Type.h"
TableCellEnumerationRenderer::TableCellEnumerationRenderer(Config* config)
:
TableCellIntegerRenderer(config)
{
}
void
TableCellEnumerationRenderer::RenderValue(Value* _value, bool valueChanged,
BRect rect, BView* targetView)
{
Config* config = GetConfig();
if (config != NULL && config->IntegerFormat() == INTEGER_FORMAT_DEFAULT) {
EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
if (value == NULL)
return;
if (EnumeratorValue* enumValue
= value->GetType()->ValueFor(value->GetValue())) {
TableCellValueRendererUtils::DrawString(targetView, rect,
enumValue->Name(), valueChanged, B_ALIGN_RIGHT, true);
return;
}
}
TableCellIntegerRenderer::RenderValue(_value, valueChanged, rect,
targetView);
}
float
TableCellEnumerationRenderer::PreferredValueWidth(Value* _value,
BView* targetView)
{
Config* config = GetConfig();
if (config != NULL && config->IntegerFormat() == INTEGER_FORMAT_DEFAULT) {
EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
if (value == NULL)
return 0;
if (EnumeratorValue* enumValue
= value->GetType()->ValueFor(value->GetValue())) {
return TableCellValueRendererUtils::PreferredStringWidth(targetView,
enumValue->Name());
}
}
return TableCellIntegerRenderer::PreferredValueWidth(_value, targetView);
}
@@ -1,26 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_ENUMERATION_RENDERER_H
#define TABLE_CELL_ENUMERATION_RENDERER_H
#include <Referenceable.h>
#include "TableCellIntegerRenderer.h"
class TableCellEnumerationRenderer : public TableCellIntegerRenderer {
public:
TableCellEnumerationRenderer(Config* config);
virtual void RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView);
virtual float PreferredValueWidth(Value* value,
BView* targetView);
};
#endif // TABLE_CELL_ENUMERATION_RENDERER_H
@@ -1,44 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "TableCellFloatRenderer.h"
#include <stdio.h>
#include "FloatValue.h"
#include "TableCellValueRendererUtils.h"
void
TableCellFloatRenderer::RenderValue(Value* _value, bool valueChanged,
BRect rect, BView* targetView)
{
FloatValue* value = dynamic_cast<FloatValue*>(_value);
if (value == NULL)
return;
char buffer[64];
snprintf(buffer, sizeof(buffer), "%g", value->GetValue());
TableCellValueRendererUtils::DrawString(targetView, rect, buffer,
valueChanged, B_ALIGN_RIGHT, true);
}
float
TableCellFloatRenderer::PreferredValueWidth(Value* _value, BView* targetView)
{
FloatValue* value = dynamic_cast<FloatValue*>(_value);
if (value == NULL)
return 0;
char buffer[64];
snprintf(buffer, sizeof(buffer), "%g", value->GetValue());
return TableCellValueRendererUtils::PreferredStringWidth(targetView,
buffer);
}
@@ -1,24 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_FLOAT_RENDERER_H
#define TABLE_CELL_FLOAT_RENDERER_H
#include <Referenceable.h>
#include "TableCellValueRenderer.h"
class TableCellFloatRenderer : public TableCellValueRenderer {
public:
virtual void RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView);
virtual float PreferredValueWidth(Value* value,
BView* targetView);
};
#endif // TABLE_CELL_FLOAT_RENDERER_H
@@ -0,0 +1,64 @@
/*
* Copyright 2014-2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "TableCellFormattedValueRenderer.h"
#include <String.h>
#include "TableCellValueRendererUtils.h"
#include "ValueFormatter.h"
TableCellFormattedValueRenderer::TableCellFormattedValueRenderer(
ValueFormatter* formatter)
:
fValueFormatter(formatter)
{
fValueFormatter->AcquireReference();
}
TableCellFormattedValueRenderer::~TableCellFormattedValueRenderer()
{
fValueFormatter->ReleaseReference();
}
Settings*
TableCellFormattedValueRenderer::GetSettings() const
{
return fValueFormatter->GetSettings();
}
void
TableCellFormattedValueRenderer::RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView)
{
BString output;
if (fValueFormatter->FormatValue(value, output) != B_OK)
return;
// render
TableCellValueRendererUtils::DrawString(targetView, rect, output,
valueChanged, B_ALIGN_RIGHT, true);
}
float
TableCellFormattedValueRenderer::PreferredValueWidth(Value* value, BView* targetView)
{
BString output;
if (fValueFormatter->FormatValue(value, output) != B_OK)
return 0;
// render
return TableCellValueRendererUtils::PreferredStringWidth(targetView,
output);
}
@@ -0,0 +1,38 @@
/*
* Copyright 2014-2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_FORMATTED_VALUE_RENDERER_H
#define TABLE_CELL_FORMATTED_VALUE_RENDERER_H
#include "TableCellValueRenderer.h"
class ValueFormatter;
class TableCellFormattedValueRenderer : public TableCellValueRenderer {
public:
TableCellFormattedValueRenderer(
ValueFormatter* formatter);
virtual ~TableCellFormattedValueRenderer();
virtual Settings* GetSettings() const;
ValueFormatter* GetValueFormatter() const
{ return fValueFormatter; }
virtual void RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView);
virtual float PreferredValueWidth(Value* value,
BView* targetView);
private:
ValueFormatter* fValueFormatter;
};
#endif // TABLE_CELL_FORMATTED_VALUE_RENDERER_H
@@ -1,94 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "TableCellIntegerRenderer.h"
#include <stdio.h>
#include <TypeConstants.h>
#include "TableCellValueRendererUtils.h"
#include "IntegerValue.h"
// #pragma mark - TableCellIntegerRenderer
TableCellIntegerRenderer::TableCellIntegerRenderer(Config* config)
:
fConfig(config)
{
if (fConfig != NULL)
fConfig->AcquireReference();
}
TableCellIntegerRenderer::~TableCellIntegerRenderer()
{
if (fConfig != NULL)
fConfig->ReleaseReference();
}
Settings*
TableCellIntegerRenderer::GetSettings() const
{
return fConfig != NULL ? fConfig->GetSettings() : NULL;
}
void
TableCellIntegerRenderer::RenderValue(Value* _value, bool valueChanged,
BRect rect, BView* targetView)
{
IntegerValue* value = dynamic_cast<IntegerValue*>(_value);
if (value == NULL)
return;
// format the value
integer_format format = fConfig != NULL
? fConfig->IntegerFormat() : INTEGER_FORMAT_DEFAULT;
char buffer[32];
if (!IntegerFormatter::FormatValue(value->GetValue(), format, buffer,
sizeof(buffer))) {
return;
}
// render
TableCellValueRendererUtils::DrawString(targetView, rect, buffer,
valueChanged, B_ALIGN_RIGHT, true);
}
float
TableCellIntegerRenderer::PreferredValueWidth(Value* _value, BView* targetView)
{
IntegerValue* value = dynamic_cast<IntegerValue*>(_value);
if (value == NULL)
return 0;
// format the value
integer_format format = fConfig != NULL
? fConfig->IntegerFormat() : INTEGER_FORMAT_DEFAULT;
char buffer[32];
if (!IntegerFormatter::FormatValue(value->GetValue(), format, buffer,
sizeof(buffer))) {
return 0;
}
// render
return TableCellValueRendererUtils::PreferredStringWidth(targetView,
buffer);
}
// #pragma mark - Config
TableCellIntegerRenderer::Config::~Config()
{
}
@@ -1,48 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_INTEGER_RENDERER_H
#define TABLE_CELL_INTEGER_RENDERER_H
#include <Referenceable.h>
#include "IntegerFormatter.h"
#include "TableCellValueRenderer.h"
class TableCellIntegerRenderer : public TableCellValueRenderer {
public:
class Config;
public:
TableCellIntegerRenderer(Config* config);
virtual ~TableCellIntegerRenderer();
Config* GetConfig() const
{ return fConfig; }
virtual Settings* GetSettings() const;
virtual void RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView);
virtual float PreferredValueWidth(Value* value,
BView* targetView);
private:
Config* fConfig;
};
class TableCellIntegerRenderer::Config : public BReferenceable {
public:
virtual ~Config();
virtual Settings* GetSettings() const = 0;
virtual integer_format IntegerFormat() const = 0;
};
#endif // TABLE_CELL_INTEGER_RENDERER_H
@@ -1,81 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "TableCellStringRenderer.h"
#include <stdio.h>
#include <String.h>
#include "TableCellValueRendererUtils.h"
#include "Value.h"
void
TableCellStringRenderer::RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView)
{
BString string = "\"";
BString tempString;
if (!value->ToString(tempString))
return;
for (int32 i = 0; i < tempString.Length(); i++) {
if (tempString[i] < 31) {
switch (tempString[i]) {
case '\0':
string << "\\0";
break;
case '\a':
string << "\\a";
break;
case '\b':
string << "\\b";
break;
case '\t':
string << "\\t";
break;
case '\r':
string << "\\r";
break;
case '\n':
string << "\\n";
break;
case '\f':
string << "\\f";
break;
default:
{
char buffer[5];
snprintf(buffer, sizeof(buffer), "\\x%x",
tempString.String()[i]);
string << buffer;
break;
}
}
} else if (tempString[i] == '\"')
string << "\\\"";
else
string << tempString[i];
}
string += "\"";
TableCellValueRendererUtils::DrawString(targetView, rect, string,
valueChanged, B_ALIGN_RIGHT, true);
}
float
TableCellStringRenderer::PreferredValueWidth(Value* value, BView* targetView)
{
BString string;
if (!value->ToString(string))
return 0;
return TableCellValueRendererUtils::PreferredStringWidth(targetView,
string);
}
@@ -1,24 +0,0 @@
/*
* Copyright 2014, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_STRING_RENDERER_H
#define TABLE_CELL_STRING_RENDERER_H
#include "TableCellValueRenderer.h"
#include <Referenceable.h>
class TableCellStringRenderer : public TableCellValueRenderer {
public:
virtual void RenderValue(Value* value, bool valueChanged,
BRect rect, BView* targetView);
virtual float PreferredValueWidth(Value* value,
BView* targetView);
};
#endif // TABLE_CELL_STRING_RENDERER_H
@@ -0,0 +1,35 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "ValueFormatter.h"
ValueFormatter::~ValueFormatter()
{
}
bool
ValueFormatter::SupportsValidation() const
{
return false;
}
bool
ValueFormatter::ValidateFormattedValue(const BString& input, type_code type)
const
{
return false;
}
status_t
ValueFormatter::GetValueFromFormattedInput(const BString& input,
type_code type, Value*& _output) const
{
return B_NOT_SUPPORTED;
}
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef VALUE_FORMATTER_H
#define VALUE_FORMATTER_H
#include <Referenceable.h>
class BString;
class Settings;
class Value;
class ValueFormatter : public BReferenceable {
public:
virtual ~ValueFormatter();
virtual Settings* GetSettings() const = 0;
// returns NULL, if no settings
virtual status_t FormatValue(Value* value, BString& _output)
= 0;
virtual bool SupportsValidation() const;
virtual bool ValidateFormattedValue(
const BString& input,
type_code type) const;
// checks if the passed in string
// would be considered a valid value
// according to the current format
// configuration and the size constraints
// imposed by the passed in type.
virtual status_t GetValueFromFormattedInput(
const BString& input, type_code type,
Value*& _output) const;
// returns reference
};
#endif // VALUE_FORMATTER_H
@@ -0,0 +1,33 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "BoolValueFormatter.h"
#include "BoolValue.h"
BoolValueFormatter::BoolValueFormatter()
:
ValueFormatter()
{
}
BoolValueFormatter::~BoolValueFormatter()
{
}
status_t
BoolValueFormatter::FormatValue(Value* _value, BString& _output)
{
BoolValue* value = dynamic_cast<BoolValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
_output.SetTo(value->GetValue() ? "true" : "false");
return B_OK;
}
@@ -0,0 +1,28 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef BOOL_VALUE_FORMATTER_H
#define BOOL_VALUE_FORMATTER_H
#include "ValueFormatter.h"
class Settings;
class Value;
class BoolValueFormatter : public ValueFormatter {
public:
BoolValueFormatter();
virtual ~BoolValueFormatter();
virtual Settings* GetSettings() const
{ return NULL; }
virtual status_t FormatValue(Value* value, BString& _output);
};
#endif // BOOL_VALUE_FORMATTER_H
@@ -0,0 +1,41 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "EnumerationValueFormatter.h"
#include "EnumerationValue.h"
#include "Type.h"
EnumerationValueFormatter::EnumerationValueFormatter(Config* config)
:
IntegerValueFormatter(config)
{
}
EnumerationValueFormatter::~EnumerationValueFormatter()
{
}
status_t
EnumerationValueFormatter::FormatValue(Value* _value, BString& _output)
{
Config* config = GetConfig();
if (config != NULL && config->IntegerFormat() == INTEGER_FORMAT_DEFAULT) {
EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
if (EnumeratorValue* enumValue
= value->GetType()->ValueFor(value->GetValue())) {
_output.SetTo(enumValue->Name());
return B_OK;
}
}
return IntegerValueFormatter::FormatValue(_value, _output);
}
@@ -0,0 +1,21 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef ENUMERATION_VALUE_FORMATTER_H
#define ENUMERATION_VALUE_FORMATTER_H
#include "IntegerValueFormatter.h"
class EnumerationValueFormatter : public IntegerValueFormatter {
public:
EnumerationValueFormatter(Config* config);
virtual ~EnumerationValueFormatter();
virtual status_t FormatValue(Value* value, BString& _output);
};
#endif // ENUMERATION_VALUE_FORMATTER_H
@@ -0,0 +1,38 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "FloatValueFormatter.h"
#include <stdio.h>
#include "FloatValue.h"
FloatValueFormatter::FloatValueFormatter()
:
ValueFormatter()
{
}
FloatValueFormatter::~FloatValueFormatter()
{
}
status_t
FloatValueFormatter::FormatValue(Value* _value, BString& _output)
{
FloatValue* value = dynamic_cast<FloatValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
char buffer[64];
snprintf(buffer, sizeof(buffer), "%g", value->GetValue());
_output.SetTo(buffer);
return B_OK;
}
@@ -0,0 +1,28 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef FLOAT_VALUE_FORMATTER_H
#define FLOAT_VALUE_FORMATTER_H
#include "ValueFormatter.h"
class Settings;
class Value;
class FloatValueFormatter : public ValueFormatter {
public:
FloatValueFormatter();
virtual ~FloatValueFormatter();
virtual Settings* GetSettings() const
{ return NULL; }
virtual status_t FormatValue(Value* value, BString& _output);
};
#endif // FLOAT_VALUE_FORMATTER_H
@@ -0,0 +1,234 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "IntegerValueFormatter.h"
#include <new>
#include <ctype.h>
#include "IntegerFormatter.h"
#include "IntegerValue.h"
// #pragma mark - IntegerValueFormatter
IntegerValueFormatter::IntegerValueFormatter(Config* config)
:
ValueFormatter(),
fConfig(config)
{
if (fConfig != NULL)
fConfig->AcquireReference();
}
IntegerValueFormatter::~IntegerValueFormatter()
{
if (fConfig != NULL)
fConfig->ReleaseReference();
}
Settings*
IntegerValueFormatter::GetSettings() const
{
return fConfig != NULL ? fConfig->GetSettings() : NULL;
}
status_t
IntegerValueFormatter::FormatValue(Value* _value, BString& _output)
{
IntegerValue* value = dynamic_cast<IntegerValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
// format the value
integer_format format = fConfig != NULL
? fConfig->IntegerFormat() : INTEGER_FORMAT_DEFAULT;
char buffer[32];
if (!IntegerFormatter::FormatValue(value->GetValue(), format, buffer,
sizeof(buffer))) {
return B_BAD_VALUE;
}
_output.SetTo(buffer);
return B_OK;
}
bool
IntegerValueFormatter::SupportsValidation() const
{
return true;
}
bool
IntegerValueFormatter::ValidateFormattedValue(const BString& input,
type_code type) const
{
::Value* value = NULL;
return _PerformValidation(input, type, value, false) == B_OK;
}
status_t
IntegerValueFormatter::GetValueFromFormattedInput(const BString& input,
type_code type, Value*& _output) const
{
return _PerformValidation(input, type, _output, true);
}
status_t
IntegerValueFormatter::_PerformValidation(const BString& input, type_code type,
::Value*& _output, bool wantsValue) const
{
integer_format format;
if (fConfig != NULL)
format = fConfig->IntegerFormat();
else {
bool isSigned;
if (BVariant::TypeIsInteger(type, &isSigned)) {
format = isSigned ? INTEGER_FORMAT_SIGNED
: INTEGER_FORMAT_UNSIGNED;
} else
return B_BAD_VALUE;
}
status_t error = B_OK;
if (format == INTEGER_FORMAT_UNSIGNED
|| format >= INTEGER_FORMAT_HEX_DEFAULT) {
error = _ValidateUnsigned(input, type, _output, format, wantsValue);
} else
error = _ValidateSigned(input, type, _output, wantsValue);
return error;
}
status_t
IntegerValueFormatter::_ValidateSigned(const BString& input, type_code type,
::Value*& _output, bool wantsValue) const
{
const char* text = input.String();
char *parseEnd = NULL;
intmax_t parsedValue = strtoimax(text, &parseEnd, 10);
if (parseEnd - text < input.Length() && !isspace(*parseEnd))
return B_NO_MEMORY;
BVariant newValue;
switch (type) {
case B_INT8_TYPE:
{
if (parsedValue < INT8_MIN || parsedValue > INT8_MAX)
return B_BAD_VALUE;
newValue.SetTo((int8)parsedValue);
break;
}
case B_INT16_TYPE:
{
if (parsedValue < INT16_MIN || parsedValue > INT16_MAX)
return B_BAD_VALUE;
newValue.SetTo((int16)parsedValue);
break;
}
case B_INT32_TYPE:
{
if (parsedValue < INT32_MAX || parsedValue > INT32_MAX)
return B_BAD_VALUE;
newValue.SetTo((int32)parsedValue);
break;
}
case B_INT64_TYPE:
{
newValue.SetTo((int64)parsedValue);
break;
}
default:
return B_BAD_VALUE;
}
if (wantsValue) {
_output = new(std::nothrow) IntegerValue(newValue);
if (_output == NULL)
return B_NO_MEMORY;
}
return B_OK;
}
status_t
IntegerValueFormatter::_ValidateUnsigned(const BString& input, type_code type,
::Value*& _output, integer_format format, bool wantsValue) const
{
const char* text = input.String();
int32 base = format == INTEGER_FORMAT_UNSIGNED ? 10 : 16;
char *parseEnd = NULL;
uintmax_t parsedValue = strtoumax(text, &parseEnd, base);
if (parseEnd - text < input.Length() && !isspace(*parseEnd))
return B_BAD_VALUE;
BVariant newValue;
switch (type) {
case B_UINT8_TYPE:
{
if (parsedValue > UINT8_MAX)
return B_BAD_VALUE;
newValue.SetTo((uint8)parsedValue);
break;
}
case B_UINT16_TYPE:
{
if (parsedValue > UINT16_MAX)
return B_BAD_VALUE;
newValue.SetTo((uint16)parsedValue);
break;
}
case B_UINT32_TYPE:
{
if (parsedValue > UINT32_MAX)
return B_BAD_VALUE;
newValue.SetTo((uint32)parsedValue);
break;
}
case B_UINT64_TYPE:
{
newValue.SetTo((uint64)parsedValue);
break;
}
default:
return B_BAD_VALUE;
}
if (wantsValue) {
_output = new(std::nothrow) IntegerValue(newValue);
if (_output == NULL)
return B_NO_MEMORY;
}
return B_OK;
}
// #pragma mark - Config
IntegerValueFormatter::Config::~Config()
{
}
@@ -0,0 +1,70 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef INTEGER_VALUE_FORMATTER_H
#define INTEGER_VALUE_FORMATTER_H
#include "util/IntegerFormatter.h"
#include "ValueFormatter.h"
class Settings;
class Value;
class IntegerValueFormatter : public ValueFormatter {
public:
class Config;
public:
IntegerValueFormatter(Config* config);
virtual ~IntegerValueFormatter();
Config* GetConfig() const
{ return fConfig; }
virtual Settings* GetSettings() const;
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;
status_t _ValidateSigned(const BString& input,
type_code type,
::Value*& _output,
bool wantsValue = false) const;
status_t _ValidateUnsigned(const BString& input,
type_code type,
::Value*& _output,
integer_format format,
bool wantsValue = false) const;
Config* fConfig;
};
class IntegerValueFormatter::Config : public BReferenceable {
public:
virtual ~Config();
virtual Settings* GetSettings() const = 0;
virtual integer_format IntegerFormat() const = 0;
};
#endif // INTEGER_VALUE_FORMATTER_H
@@ -0,0 +1,77 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "StringValueFormatter.h"
#include <stdio.h>
#include <String.h>
#include "Value.h"
StringValueFormatter::StringValueFormatter()
:
ValueFormatter()
{
}
StringValueFormatter::~StringValueFormatter()
{
}
status_t
StringValueFormatter::FormatValue(Value* value, BString& _output)
{
_output = "\"";
BString tempString;
if (!value->ToString(tempString))
return B_BAD_VALUE;
for (int32 i = 0; i < tempString.Length(); i++) {
if (tempString[i] < 31) {
switch (tempString[i]) {
case '\0':
_output << "\\0";
break;
case '\a':
_output << "\\a";
break;
case '\b':
_output << "\\b";
break;
case '\t':
_output << "\\t";
break;
case '\r':
_output << "\\r";
break;
case '\n':
_output << "\\n";
break;
case '\f':
_output << "\\f";
break;
default:
{
char buffer[5];
snprintf(buffer, sizeof(buffer), "\\x%x",
tempString.String()[i]);
_output << buffer;
break;
}
}
} else if (tempString[i] == '\"')
_output << "\\\"";
else
_output << tempString[i];
}
_output += "\"";
return B_OK;
}
@@ -0,0 +1,28 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef STRING_VALUE_FORMATTER_H
#define STRING_VALUE_FORMATTER_H
#include "ValueFormatter.h"
class Settings;
class Value;
class StringValueFormatter : public ValueFormatter {
public:
StringValueFormatter();
virtual ~StringValueFormatter();
virtual Settings* GetSettings() const
{ return NULL; }
virtual status_t FormatValue(Value* value, BString& _output);
};
#endif // STRING_VALUE_FORMATTER_H
@@ -1,4 +1,5 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
@@ -9,7 +10,8 @@
#include <new>
#include "BoolValue.h"
#include "TableCellBoolRenderer.h"
#include "BoolValueFormatter.h"
#include "TableCellFormattedValueRenderer.h"
BoolValueHandler::BoolValueHandler()
@@ -40,8 +42,15 @@ status_t
BoolValueHandler::GetValueFormatter(Value* value,
ValueFormatter*& _formatter)
{
// TODO:...
return B_UNSUPPORTED;
if (dynamic_cast<BoolValue*>(value) == NULL)
return B_BAD_VALUE;
BoolValueFormatter* formatter = new(std::nothrow) BoolValueFormatter;
if (formatter == NULL)
return B_NO_MEMORY;
_formatter = formatter;
return B_OK;
}
@@ -52,8 +61,14 @@ BoolValueHandler::GetTableCellValueRenderer(Value* value,
if (dynamic_cast<BoolValue*>(value) == NULL)
return B_BAD_VALUE;
ValueFormatter* formatter = NULL;
if (GetValueFormatter(value, formatter))
return B_NO_MEMORY;
BReference<ValueFormatter> formatterReference(formatter, true);
// create the renderer
TableCellValueRenderer* renderer = new(std::nothrow) TableCellBoolRenderer;
TableCellValueRenderer* renderer
= new(std::nothrow) TableCellFormattedValueRenderer(formatter);
if (renderer == NULL)
return B_NO_MEMORY;
@@ -1,4 +1,5 @@
/*
* Copyright 2015, Rene Gollent, [email protected].
* Copyright 2009, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
@@ -9,7 +10,8 @@
#include <new>
#include "EnumerationValue.h"
#include "TableCellEnumerationRenderer.h"
#include "EnumerationValueFormatter.h"
#include "TableCellFormattedValueRenderer.h"
#include "Type.h"
@@ -37,6 +39,30 @@ EnumerationValueHandler::SupportsValue(Value* value)
}
status_t
EnumerationValueHandler::GetValueFormatter(Value* _value,
ValueFormatter*& _formatter)
{
EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
IntegerValueFormatter::Config* config = NULL;
status_t error = CreateIntegerFormatterConfig(value, config);
if (error != B_OK)
return error;
BReference<IntegerValueFormatter::Config> configReference(config, true);
ValueFormatter* formatter = NULL;
error = CreateValueFormatter(config, formatter);
if (error != B_OK)
return error;
_formatter = formatter;
return B_OK;
}
integer_format
EnumerationValueHandler::DefaultIntegerFormat(IntegerValue* _value)
@@ -49,6 +75,20 @@ EnumerationValueHandler::DefaultIntegerFormat(IntegerValue* _value)
}
status_t
EnumerationValueHandler::CreateValueFormatter(
IntegerValueFormatter::Config* config, ValueFormatter*& _formatter)
{
ValueFormatter* formatter = new(std::nothrow) EnumerationValueFormatter(
config);
if (formatter == NULL)
return B_NO_MEMORY;
_formatter = formatter;
return B_OK;
}
status_t
EnumerationValueHandler::AddIntegerFormatSettingOptions(IntegerValue* _value,
OptionsSettingImpl* setting)
@@ -68,14 +108,21 @@ EnumerationValueHandler::AddIntegerFormatSettingOptions(IntegerValue* _value,
status_t
EnumerationValueHandler::CreateTableCellValueRenderer(IntegerValue* _value,
TableCellIntegerRenderer::Config* config,
IntegerValueFormatter::Config* config,
TableCellValueRenderer*& _renderer)
{
EnumerationValue* value = dynamic_cast<EnumerationValue*>(_value);
if (value != NULL
&& value->GetType()->ValueFor(value->GetValue()) != NULL) {
ValueFormatter* formatter = NULL;
status_t error = GetValueFormatter(value, formatter);
if (error != B_OK)
return error;
BReference<ValueFormatter> formatterReference(formatter,
true);
TableCellValueRenderer* renderer
= new(std::nothrow) TableCellEnumerationRenderer(config);
= new(std::nothrow) TableCellFormattedValueRenderer(formatter);
if (renderer == NULL)
return B_NO_MEMORY;
@@ -17,15 +17,20 @@ public:
status_t Init();
virtual float SupportsValue(Value* value);
virtual status_t GetValueFormatter(Value* value,
ValueFormatter*& _formatter);
protected:
virtual integer_format DefaultIntegerFormat(IntegerValue* value);
virtual status_t CreateValueFormatter(
IntegerValueFormatter::Config* config,
ValueFormatter*& _formatter);
virtual status_t AddIntegerFormatSettingOptions(
IntegerValue* value,
OptionsSettingImpl* setting);
virtual status_t CreateTableCellValueRenderer(
IntegerValue* value,
TableCellIntegerRenderer::Config* config,
IntegerValueFormatter::Config* config,
TableCellValueRenderer*& _renderer);
};
@@ -9,7 +9,8 @@
#include <new>
#include "FloatValue.h"
#include "TableCellFloatRenderer.h"
#include "FloatValueFormatter.h"
#include "TableCellFormattedValueRenderer.h"
FloatValueHandler::FloatValueHandler()
@@ -40,8 +41,15 @@ status_t
FloatValueHandler::GetValueFormatter(Value* value,
ValueFormatter*& _formatter)
{
// TODO:...
return B_UNSUPPORTED;
if (dynamic_cast<FloatValue*>(value) == NULL)
return B_BAD_VALUE;
FloatValueFormatter* formatter = new(std::nothrow) FloatValueFormatter;
if (formatter == NULL)
return B_NO_MEMORY;
_formatter = formatter;
return B_OK;
}
@@ -52,8 +60,15 @@ FloatValueHandler::GetTableCellValueRenderer(Value* value,
if (dynamic_cast<FloatValue*>(value) == NULL)
return B_BAD_VALUE;
ValueFormatter* formatter = NULL;
status_t error = GetValueFormatter(value, formatter);
if (error != B_OK)
return error;
BReference<ValueFormatter> formatterReference(formatter, true);
// create the renderer
TableCellValueRenderer* renderer = new(std::nothrow) TableCellFloatRenderer;
TableCellValueRenderer* renderer
= new(std::nothrow) TableCellFormattedValueRenderer(formatter);
if (renderer == NULL)
return B_NO_MEMORY;
@@ -11,11 +11,12 @@
#include <AutoDeleter.h>
#include "IntegerValue.h"
#include "IntegerValueFormatter.h"
#include "Setting.h"
#include "Settings.h"
#include "SettingsDescription.h"
#include "SettingsMenu.h"
#include "TableCellIntegerRenderer.h"
#include "TableCellFormattedValueRenderer.h"
static const char* const kFormatSettingID = "format";
@@ -56,20 +57,20 @@ private:
};
// #pragma mark - TableCellRendererConfig
// #pragma mark - IntegerFormatterConfig
class IntegerValueHandler::TableCellRendererConfig
: public TableCellIntegerRenderer::Config {
class IntegerValueHandler::IntegerFormatterConfig
: public IntegerValueFormatter::Config {
public:
TableCellRendererConfig()
IntegerFormatterConfig()
:
fSettings(NULL),
fFormatSetting(NULL)
{
}
~TableCellRendererConfig()
~IntegerFormatterConfig()
{
if (fSettings != NULL)
fSettings->ReleaseReference();
@@ -139,11 +140,25 @@ IntegerValueHandler::SupportsValue(Value* value)
status_t
IntegerValueHandler::GetValueFormatter(Value* value,
IntegerValueHandler::GetValueFormatter(Value* _value,
ValueFormatter*& _formatter)
{
// TODO:...
return B_UNSUPPORTED;
IntegerValue* value = dynamic_cast<IntegerValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
IntegerValueFormatter::Config* config = NULL;
status_t error = CreateIntegerFormatterConfig(value, config);
if (error != B_OK)
return error;
BReference<IntegerValueFormatter::Config> configReference(config, true);
ValueFormatter* formatter = new(std::nothrow) IntegerValueFormatter(config);
if (formatter == NULL)
return B_NO_MEMORY;
_formatter = formatter;
return B_OK;
}
@@ -155,23 +170,11 @@ IntegerValueHandler::GetTableCellValueRenderer(Value* _value,
if (value == NULL)
return B_BAD_VALUE;
// create a settings description
SettingsDescription* settingsDescription
= _CreateTableCellSettingsDescription(value);
if (settingsDescription == NULL)
return B_NO_MEMORY;
BReference<SettingsDescription> settingsDescriptionReference(
settingsDescription, true);
// create config
TableCellRendererConfig* config = new(std::nothrow) TableCellRendererConfig;
if (config == NULL)
return B_NO_MEMORY;
BReference<TableCellRendererConfig> configReference(config, true);
status_t error = config->Init(settingsDescription);
IntegerValueFormatter::Config* config = NULL;
status_t error = CreateIntegerFormatterConfig(value, config);
if (error != B_OK)
return error;
BReference<IntegerValueFormatter::Config> configReference(config, true);
// create the renderer
return CreateTableCellValueRenderer(value, config, _renderer);
@@ -233,13 +236,34 @@ IntegerValueHandler::AddIntegerFormatSettingOptions(IntegerValue* value,
}
status_t
IntegerValueHandler::CreateValueFormatter(
IntegerValueFormatter::Config* config,
ValueFormatter*& _formatter)
{
ValueFormatter* formatter = new(std::nothrow) IntegerValueFormatter(
config);
if (formatter == NULL)
return B_NO_MEMORY;
_formatter = formatter;
return B_OK;
}
status_t
IntegerValueHandler::CreateTableCellValueRenderer(IntegerValue* value,
TableCellIntegerRenderer::Config* config,
IntegerValueFormatter::Config* config,
TableCellValueRenderer*& _renderer)
{
ValueFormatter* formatter;
status_t error = CreateValueFormatter(config, formatter);
if (error != B_OK)
return error;
BReference<ValueFormatter> formatterReference(formatter, true);
TableCellValueRenderer* renderer
= new(std::nothrow) TableCellIntegerRenderer(config);
= new(std::nothrow) TableCellFormattedValueRenderer(formatter);
if (renderer == NULL)
return B_NO_MEMORY;
@@ -248,6 +272,35 @@ IntegerValueHandler::CreateTableCellValueRenderer(IntegerValue* value,
}
status_t
IntegerValueHandler::CreateIntegerFormatterConfig(IntegerValue* value,
IntegerValueFormatter::Config*& _config)
{
// create a settings description
SettingsDescription* settingsDescription
= _CreateTableCellSettingsDescription(value);
if (settingsDescription == NULL)
return B_NO_MEMORY;
BReference<SettingsDescription> settingsDescriptionReference(
settingsDescription, true);
// create config
IntegerFormatterConfig* config = new(std::nothrow) IntegerFormatterConfig;
if (config == NULL)
return B_NO_MEMORY;
BReference<IntegerFormatterConfig> configReference(config, true);
status_t error = config->Init(settingsDescription);
if (error != B_OK)
return error;
_config = config;
configReference.Detach();
return B_OK;
}
status_t
IntegerValueHandler::AddIntegerFormatOption(OptionsSettingImpl* setting,
const char* id, const char* name, integer_format format)
@@ -6,12 +6,12 @@
#define INTEGER_VALUE_HANDLER_H
#include "IntegerFormatter.h"
#include "TableCellIntegerRenderer.h"
#include "IntegerValueFormatter.h"
#include "ValueHandler.h"
class IntegerValue;
class IntegerValueFormatter;
class OptionsSettingImpl;
class SettingsDescription;
@@ -36,10 +36,16 @@ protected:
virtual status_t AddIntegerFormatSettingOptions(
IntegerValue* value,
OptionsSettingImpl* setting);
virtual status_t CreateValueFormatter(
IntegerValueFormatter::Config* config,
ValueFormatter*& _formatter);
virtual status_t CreateTableCellValueRenderer(
IntegerValue* value,
TableCellIntegerRenderer::Config* config,
IntegerValueFormatter::Config* config,
TableCellValueRenderer*& _renderer);
virtual status_t CreateIntegerFormatterConfig(
IntegerValue* value,
IntegerValueFormatter::Config*& _config);
status_t AddIntegerFormatOption(
OptionsSettingImpl* setting, const char* id,
@@ -47,7 +53,7 @@ protected:
private:
class FormatOption;
class TableCellRendererConfig;
class IntegerFormatterConfig;
private:
SettingsDescription* _CreateTableCellSettingsDescription(
@@ -1,5 +1,5 @@
/*
* Copyright 2010, Rene Gollent, [email protected]
* Copyright 2010-2015, Rene Gollent, [email protected]
* Distributed under the terms of the MIT License.
*/
@@ -8,10 +8,9 @@
#include <new>
#include <stdio.h>
#include "StringValue.h"
#include "TableCellStringRenderer.h"
#include "StringValueFormatter.h"
#include "TableCellFormattedValueRenderer.h"
StringValueHandler::StringValueHandler()
@@ -42,8 +41,15 @@ status_t
StringValueHandler::GetValueFormatter(Value* value,
ValueFormatter*& _formatter)
{
// TODO:...
return B_UNSUPPORTED;
if (dynamic_cast<StringValue*>(value) == NULL)
return B_BAD_VALUE;
ValueFormatter* formatter = new(std::nothrow) StringValueFormatter;
if (formatter == NULL)
return B_NO_MEMORY;
_formatter = formatter;
return B_OK;
}
@@ -54,9 +60,15 @@ StringValueHandler::GetTableCellValueRenderer(Value* value,
if (dynamic_cast<StringValue*>(value) == NULL)
return B_BAD_VALUE;
ValueFormatter* formatter = NULL;
status_t error = GetValueFormatter(value, formatter);
if (error != B_OK)
return error;
BReference<ValueFormatter> formatterReference(formatter, true);
// create the renderer
TableCellValueRenderer* renderer = new(std::nothrow)
TableCellStringRenderer;
TableCellValueRenderer* renderer
= new(std::nothrow) TableCellFormattedValueRenderer(formatter);
if (renderer == NULL)
return B_NO_MEMORY;