Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2020-01-06 18:43:33 +0300
committerDavid Crocker <dcrocker@eschertech.com>2020-01-06 18:43:33 +0300
commitc8ef10d29d38856353689f3b5934c8fb226787d9 (patch)
tree444a61581b4c49b2de46141112051891ec3f0201 /src/ObjectModel/ObjectModel.h
parent1d61e272e01727afa810f8aa5acedc051206772d (diff)
Major refactoring of object model framework
Refactored object model framework to simplify the code Started bringing the object model into line with DSF Added echo command Added line number constant
Diffstat (limited to 'src/ObjectModel/ObjectModel.h')
-rw-r--r--src/ObjectModel/ObjectModel.h105
1 files changed, 52 insertions, 53 deletions
diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h
index 78cb55d9..44635f2c 100644
--- a/src/ObjectModel/ObjectModel.h
+++ b/src/ObjectModel/ObjectModel.h
@@ -17,15 +17,13 @@
typedef uint32_t ObjectModelFilterFlags;
typedef uint8_t TypeCode;
-constexpr TypeCode IsArray = 128; // this is or'ed in to a type code to indicate an array
constexpr TypeCode NoType = 0; // code for an invalid or unknown type
// Dummy types, used to define type codes
class Bitmap32;
class Enum32;
-class Float2; // float printed to 2 decimal places instead of 1
-class Float3; // float printed to 3 decimal places instead of 1
-class ObjectModel; // forward declaration
+class ObjectModel; // forward declaration
+class ObjectModelArrayDescriptor; // forward declaration
// Function template used to get constexpr type IDs
// Each type must return a unique type code in the range 1 to 127
@@ -35,13 +33,12 @@ template<> constexpr TypeCode TypeOf<bool>() noexcept { return 1; }
template<> constexpr TypeCode TypeOf<uint32_t>() noexcept { return 2; }
template<> constexpr TypeCode TypeOf<int32_t>() noexcept { return 3; }
template<> constexpr TypeCode TypeOf<float>() noexcept { return 4; }
-template<> constexpr TypeCode TypeOf<Float2>() noexcept { return 5; }
-template<> constexpr TypeCode TypeOf<Float3>() noexcept { return 6; }
-template<> constexpr TypeCode TypeOf<Bitmap32>() noexcept { return 7; }
-template<> constexpr TypeCode TypeOf<Enum32>() noexcept { return 8; }
-template<> constexpr TypeCode TypeOf<ObjectModel>() noexcept { return 9; }
-template<> constexpr TypeCode TypeOf<const char *>() noexcept { return 10; }
-template<> constexpr TypeCode TypeOf<IPAddress>() noexcept { return 11; }
+template<> constexpr TypeCode TypeOf<Bitmap32>() noexcept { return 5; }
+template<> constexpr TypeCode TypeOf<Enum32>() noexcept { return 6; }
+template<> constexpr TypeCode TypeOf<const ObjectModel*>() noexcept { return 7; }
+template<> constexpr TypeCode TypeOf<const char*>() noexcept { return 8; }
+template<> constexpr TypeCode TypeOf<IPAddress>() noexcept { return 9; }
+template<> constexpr TypeCode TypeOf<const ObjectModelArrayDescriptor*>() noexcept { return 10; }
#define TYPE_OF(_t) (TypeOf<_t>())
@@ -59,6 +56,7 @@ class StringParser;
struct ExpressionValue
{
TypeCode type;
+ uint8_t param;
union
{
bool bVal;
@@ -67,16 +65,33 @@ struct ExpressionValue
uint32_t uVal; // used for enumerations, bitmaps and IP addresses (not for integers, we always use int32_t for those)
const char *sVal;
const ObjectModel *omVal;
+ const ObjectModelArrayDescriptor *omadVal;
};
- constexpr ExpressionValue(bool b) noexcept : type(TYPE_OF(bool)), bVal(b) { }
- constexpr ExpressionValue(float f) noexcept : type(TYPE_OF(float)), fVal(f) { }
- constexpr ExpressionValue(int32_t i) noexcept : type(TYPE_OF(int32_t)), iVal(i) { }
ExpressionValue() noexcept : type(NoType) { }
+ explicit constexpr ExpressionValue(bool b) noexcept : type(TYPE_OF(bool)), param(0), bVal(b) { }
+ explicit constexpr ExpressionValue(float f) noexcept : type(TYPE_OF(float)), param(1), fVal(f) { }
+ constexpr ExpressionValue(float f, uint8_t numDecimalPlaces) noexcept : type(TYPE_OF(float)), param(numDecimalPlaces), fVal(f) { }
+ explicit constexpr ExpressionValue(int32_t i) noexcept : type(TYPE_OF(int32_t)), param(0), iVal(i) { }
+ explicit constexpr ExpressionValue(const ObjectModel *om) noexcept : type(TYPE_OF(const ObjectModel*)), param(0), omVal(om) { }
+ constexpr ExpressionValue(const ObjectModel *om, uint8_t tableNumber) noexcept : type(TYPE_OF(const ObjectModel*)), param(tableNumber), omVal(om) { }
+ explicit constexpr ExpressionValue(const char *s) noexcept : type(TYPE_OF(const char*)), param(0), sVal(s) { }
+ explicit constexpr ExpressionValue(const ObjectModelArrayDescriptor *omad) noexcept : type(TYPE_OF(const ObjectModelArrayDescriptor*)), param(0), omadVal(omad) { }
+ explicit constexpr ExpressionValue(IPAddress ip) noexcept : type(TYPE_OF(IPAddress)), param(0), uVal(ip.GetV4LittleEndian()) { }
+ explicit constexpr ExpressionValue(nullptr_t dummy) noexcept : type(NoType), param(0), uVal(0) { }
+
+ void Set(bool b) noexcept { type = TYPE_OF(bool); bVal = b; }
+ void Set(int32_t i) noexcept { type = TYPE_OF(int32_t); iVal = i; }
+ void Set(int i) noexcept { type = TYPE_OF(int32_t); iVal = i; }
+ void Set(float f) noexcept { type = TYPE_OF(float); fVal = f; param = 1; }
+};
- void SetBool(bool b) noexcept { type = TYPE_OF(bool); bVal = b; }
- void SetInt(int32_t i) noexcept { type = TYPE_OF(int32_t); iVal = i; }
- void SetFloat(float f) noexcept { type = TYPE_OF(float); fVal = f; }
+// Entry to describe an array. These should be brace-initializable in flash memory.
+class ObjectModelArrayDescriptor
+{
+public:
+ size_t (*GetNumElements)(const ObjectModel*) noexcept;
+ ExpressionValue (*GetElement)(const ObjectModel*, size_t) noexcept;
};
class ObjectModel
@@ -91,22 +106,19 @@ public:
ObjectModel() noexcept;
// Construct a JSON representation of those parts of the object model requested by the user
- bool ReportAsJson(OutputBuffer *buf, uint8_t tableNumber, const char *filter, ReportFlags rflags) noexcept;
+ bool ReportAsJson(OutputBuffer *buf, uint8_t tableNumber, const char *filter, ReportFlags rflags) const noexcept;
- // Return the type of an object
- TypeCode GetObjectType(uint8_t tableNumber, const char *idString) noexcept;
+ // Report an entire array as JSON
+ bool ReportArrayAsJson(OutputBuffer *buf, const ObjectModelArrayDescriptor *omad, const char *filter, ReportFlags rflags) const noexcept;
- // Get the value of an object when we don't know what its type is
- ExpressionValue GetObjectValue(uint8_t tableNumber, const StringParser& sp, const char *idString) THROWS_PARSE_ERROR;
-
- // Specialisation of above for float, allowing conversion from integer to float
- bool GetObjectValue(uint8_t tableNumber, float& val, const char *idString) THROWS_PARSE_ERROR;
+ // Function to report a value or object as JSON
+ bool ReportItemAsJson(ExpressionValue val, OutputBuffer *buf, const char *filter, ObjectModel::ReportFlags flags) const noexcept;
- // Specialisation of above for int, allowing conversion from unsigned to signed
- bool GetObjectValue(uint8_t tableNumber, int32_t& val, const char *idString) THROWS_PARSE_ERROR;
+ // Get the value of an object when we don't know what its type is
+ ExpressionValue GetObjectValue(const StringParser& sp, uint8_t tableNumber, const char *idString) const THROWS_PARSE_ERROR;
// Get the object model table entry for the current level object in the query
- const ObjectModelTableEntry *FindObjectModelTableEntry(uint8_t tableNumber, const char *idString) noexcept;
+ const ObjectModelTableEntry *FindObjectModelTableEntry(uint8_t tableNumber, const char *idString) const noexcept;
// Skip the current element in the ID or filter string
static const char* GetNextElement(const char *id) noexcept;
@@ -116,23 +128,15 @@ protected:
private:
// Get pointers to various types from the object model, returning null if failed
- template<class T> T* GetObjectPointer(const char* idString) noexcept;
+// template<class T> T* GetObjectPointer(const char* idString) noexcept;
- const char **GetStringObjectPointer(const char *idString) noexcept;
- uint32_t *GetShortEnumObjectPointer(const char *idString) noexcept;
- uint32_t *GetBitmapObjectPointer(const char *idString) noexcept;
-};
-
-// Entry to describe an array
-class ObjectModelArrayDescriptor
-{
-public:
- size_t (*GetNumElements)(ObjectModel*) noexcept;
- void * (*GetElement)(ObjectModel*, size_t) noexcept;
+// const char **GetStringObjectPointer(const char *idString) noexcept;
+// uint32_t *GetShortEnumObjectPointer(const char *idString) noexcept;
+// uint32_t *GetBitmapObjectPointer(const char *idString) noexcept;
};
// Flags field of a table entry
-enum class ObjectModelEntryFlags : uint16_t
+enum class ObjectModelEntryFlags : uint8_t
{
none = 0, // nothing special
live = 1, // fast changing data, included in common status response
@@ -141,19 +145,17 @@ enum class ObjectModelEntryFlags : uint16_t
// Object model table entry
// It must be possible to construct these in the form of initialised data in flash memory, to avoid using large amounts of RAM.
-// Therefore we can't use a class hierarchy to represent different types of entry. Instead we use a type code and a void* parameter.
+// Therefore we can't use a class hierarchy to represent different types of entry.
class ObjectModelTableEntry
{
public:
// Type declarations
- // Type of the function pointer in the table entry, that returns a pointer to the data
- typedef void *(*ParamFuncPtr_t)(ObjectModel*) noexcept;
+ // Type of the function pointer in the table entry, that returns the data
+ typedef ExpressionValue(*DataFetchPtr_t)(const ObjectModel*) noexcept;
// Member data. This must be public so that we can brace-initialise table entries.
const char * name; // name of this field
- ParamFuncPtr_t param; // function that yields a pointer to this value
- TypeCode type; // code for the type of this value
- uint8_t tableNumber; // which object model table we want, for objects that have more than one table
+ DataFetchPtr_t func; // function that yields this value
ObjectModelEntryFlags flags; // information about this value
// Member functions. These must all be 'const'.
@@ -162,16 +164,13 @@ public:
bool Matches(const char *filter, ObjectModelFilterFlags flags) const noexcept;
// See whether we should add the value of this element to the buffer, returning true if it matched the filter and we did add it
- bool ReportAsJson(OutputBuffer* buf, ObjectModel *self, const char* filter, ObjectModel::ReportFlags flags) const noexcept;
+ bool ReportAsJson(OutputBuffer* buf, const ObjectModel *self, const char* filter, ObjectModel::ReportFlags flags) const noexcept;
// Return the name of this field
const char* GetName() const noexcept { return name; }
// Compare the name of this field with the filter string that we are trying to match
int IdCompare(const char *id) const noexcept;
-
- // Private function to report a value of primitive type
- static void ReportItemAsJson(OutputBuffer *buf, const char *filter, ObjectModel::ReportFlags flags, void *nParam, uint8_t tableNumber, TypeCode type) noexcept;
};
// Use this macro to inherit form ObjectModel
@@ -193,8 +192,8 @@ public:
return objectModelTable; \
}
-#define OBJECT_MODEL_FUNC_BODY(_class,_ret) [] (ObjectModel* arg) noexcept { _class * const self = static_cast<_class*>(arg); return (void *)(_ret); }
-#define OBJECT_MODEL_FUNC_NOSELF(_ret) [] (ObjectModel* arg) noexcept { return (void *)(_ret); }
+#define OBJECT_MODEL_FUNC_BODY(_class,...) [] (const ObjectModel* arg) noexcept { const _class * const self = static_cast<const _class*>(arg); return ExpressionValue(__VA_ARGS__); }
+#define OBJECT_MODEL_FUNC_NOSELF(...) [] (const ObjectModel* arg) noexcept { return ExpressionValue(__VA_ARGS__); }
#else