-#include "RecordValue.h"\r
-\r
-#include "RecordType.h"\r
-#include "OptionalType.h"\r
-\r
-#include "Component.h"\r
-\r
-#include "Constants.h"\r
-\r
-#include <sstream>\r
-\r
-namespace Databoard {\r
- namespace Value {\r
- RecordValue::RecordValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard), fieldCount(0)\r
- {\r
- Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);\r
-\r
- if(recordType != NULL)\r
- {\r
- fieldCount = recordType->count();\r
-\r
- if(fieldCount > 0)\r
- {\r
- fields = new Value*[fieldCount];\r
-\r
- for(int i = 0; i < fieldCount; ++i)\r
- {\r
- fields[i] = NULL;\r
- }\r
- }\r
- }\r
- }\r
-\r
- RecordValue::~RecordValue()\r
- {\r
- for(int i = 0; i < fieldCount; ++i)\r
- {\r
- delete fields[i];\r
- }\r
-\r
- delete[] fields;\r
- }\r
-\r
- void RecordValue::setField(int fieldIndex, Value* value)\r
- {\r
- if(fieldIndex >= 0 && fieldIndex < fieldCount)\r
- {\r
- fields[fieldIndex] = value;\r
- }\r
- }\r
-\r
- int RecordValue::count()\r
- {\r
- return fieldCount;\r
- }\r
-\r
- Value* RecordValue::getField(int fieldIndex)\r
- {\r
- if(fieldIndex >= 0 && fieldIndex < fieldCount)\r
- {\r
- return fields[fieldIndex];\r
- }\r
- else\r
- {\r
- return NULL;\r
- }\r
- }\r
-\r
- std::string RecordValue::writeOut(int indent)\r
- {\r
- Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);\r
-\r
- bool isTuple = true;\r
-\r
- for(int i = 0; i < (int)recordType->count(); ++i)\r
- {\r
- std::istringstream oss(recordType->getComponent(i)->getName());\r
-\r
- int name;\r
-\r
- oss >> name;\r
-\r
- if(name != i)\r
- {\r
- isTuple = false;\r
-\r
- break;\r
- }\r
- }\r
-\r
- std::string s;\r
-\r
- if(isTuple == false)\r
- {\r
- s += "{\n";\r
- }\r
- else\r
- {\r
- s += "( ";\r
- }\r
-\r
- indent += 1;\r
-\r
- for(int i = 0; i < fieldCount; ++i)\r
- {\r
- if(isTuple == false)\r
- {\r
- s.append(indent * 2, ' ');\r
-\r
- s += recordType->getComponent(i)->getName() + " = ";\r
- }\r
-\r
- if(fields[i] != NULL)\r
- {\r
- s += fields[i]->writeOut(indent);\r
-\r
- if(i != (fieldCount-1))\r
- {\r
- s += ",";\r
- }\r
- }\r
-\r
- if(isTuple == false)\r
- {\r
- s += "\n";\r
- }\r
- else\r
- {\r
- s += " ";\r
- }\r
- }\r
-\r
- indent -= 1;\r
-\r
- if(isTuple == false)\r
- {\r
- s.append(indent * 2, ' ');\r
-\r
- s += "}";\r
- }\r
- else\r
- {\r
- s += ")";\r
- }\r
-\r
- return s;\r
- }\r
-\r
- bool RecordValue::equals(const Value* other)\r
- {\r
- RecordValue* o = (RecordValue*)other;\r
-\r
- if(this->count() < o->count())\r
- {\r
- return true;\r
- }\r
- else if(this->count() > o->count())\r
- {\r
- return false;\r
- }\r
-\r
- for(int i = 0; i <(int)this->count(); ++i)\r
- {\r
- if(this->getField(i) < o->getField(i))\r
- {\r
- return true;\r
- }\r
- }\r
-\r
- return true;\r
-\r
- }\r
-\r
- std::string RecordValue::isValid()\r
- {\r
- Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);\r
-\r
- if(recordType == NULL)\r
- {\r
- return "RecordValue: Type is not record.";\r
- }\r
-\r
- if(fieldCount != recordType->count())\r
- {\r
- return "RecordValue: field count mismatch.";\r
- }\r
-\r
- for(int i = 0; i < fieldCount; ++i)\r
- {\r
- if(fields[i] == NULL)\r
- {\r
- Databoard::Type::OptionalType* optionalType = dynamic_cast<Databoard::Type::OptionalType*>(recordType->getComponent(i)->getDataBoard());\r
-\r
- if(optionalType == NULL)\r
- {\r
- return "RecordValue: Required field is missing.";\r
- }\r
- }\r
- else\r
- {\r
- std::string fieldValidity = fields[i]->isValid();\r
-\r
- if(fieldValidity.length() > 0)\r
- {\r
- return fieldValidity;\r
- }\r
- }\r
- }\r
-\r
- return STR_EMPTY;\r
- }\r
- }\r
-}\r
+#include "RecordValue.h"
+
+#include "RecordType.h"
+#include "OptionalType.h"
+
+#include "Component.h"
+
+#include "Constants.h"
+
+#include <sstream>
+
+namespace Databoard {
+ namespace Value {
+ RecordValue::RecordValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard), fieldCount(0)
+ {
+ Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);
+
+ if(recordType != NULL)
+ {
+ fieldCount = recordType->count();
+
+ if(fieldCount > 0)
+ {
+ fields = new Value*[fieldCount];
+
+ for(int i = 0; i < fieldCount; ++i)
+ {
+ fields[i] = NULL;
+ }
+ }
+ }
+ }
+
+ RecordValue::~RecordValue()
+ {
+ for(int i = 0; i < fieldCount; ++i)
+ {
+ delete fields[i];
+ }
+
+ delete[] fields;
+ }
+
+ void RecordValue::setField(int fieldIndex, Value* value)
+ {
+ if(fieldIndex >= 0 && fieldIndex < fieldCount)
+ {
+ fields[fieldIndex] = value;
+ }
+ }
+
+ int RecordValue::count()
+ {
+ return fieldCount;
+ }
+
+ Value* RecordValue::getField(int fieldIndex)
+ {
+ if(fieldIndex >= 0 && fieldIndex < fieldCount)
+ {
+ return fields[fieldIndex];
+ }
+ else
+ {
+ return NULL;
+ }
+ }
+
+ std::string RecordValue::writeOut(int indent)
+ {
+ Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);
+
+ bool isTuple = true;
+
+ for(int i = 0; i < (int)recordType->count(); ++i)
+ {
+ std::istringstream oss(recordType->getComponent(i)->getName());
+
+ int name;
+
+ oss >> name;
+
+ if(name != i)
+ {
+ isTuple = false;
+
+ break;
+ }
+ }
+
+ std::string s;
+
+ if(isTuple == false)
+ {
+ s += "{\n";
+ }
+ else
+ {
+ s += "( ";
+ }
+
+ indent += 1;
+
+ for(int i = 0; i < fieldCount; ++i)
+ {
+ if(isTuple == false)
+ {
+ s.append(indent * 2, ' ');
+
+ s += recordType->getComponent(i)->getName() + " = ";
+ }
+
+ if(fields[i] != NULL)
+ {
+ s += fields[i]->writeOut(indent);
+
+ if(i != (fieldCount-1))
+ {
+ s += ",";
+ }
+ }
+
+ if(isTuple == false)
+ {
+ s += "\n";
+ }
+ else
+ {
+ s += " ";
+ }
+ }
+
+ indent -= 1;
+
+ if(isTuple == false)
+ {
+ s.append(indent * 2, ' ');
+
+ s += "}";
+ }
+ else
+ {
+ s += ")";
+ }
+
+ return s;
+ }
+
+ bool RecordValue::equals(const Value* other)
+ {
+ RecordValue* o = (RecordValue*)other;
+
+ if(this->count() < o->count())
+ {
+ return true;
+ }
+ else if(this->count() > o->count())
+ {
+ return false;
+ }
+
+ for(int i = 0; i <(int)this->count(); ++i)
+ {
+ if(this->getField(i) < o->getField(i))
+ {
+ return true;
+ }
+ }
+
+ return true;
+
+ }
+
+ std::string RecordValue::isValid()
+ {
+ Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);
+
+ if(recordType == NULL)
+ {
+ return "RecordValue: Type is not record.";
+ }
+
+ if(fieldCount != recordType->count())
+ {
+ return "RecordValue: field count mismatch.";
+ }
+
+ for(int i = 0; i < fieldCount; ++i)
+ {
+ if(fields[i] == NULL)
+ {
+ Databoard::Type::OptionalType* optionalType = dynamic_cast<Databoard::Type::OptionalType*>(recordType->getComponent(i)->getDataBoard());
+
+ if(optionalType == NULL)
+ {
+ return "RecordValue: Required field is missing.";
+ }
+ }
+ else
+ {
+ std::string fieldValidity = fields[i]->isValid();
+
+ if(fieldValidity.length() > 0)
+ {
+ return fieldValidity;
+ }
+ }
+ }
+
+ return STR_EMPTY;
+ }
+ }
+}