Skip to content

Commit

Permalink
Added setString() which can change the value of the field assuming th…
Browse files Browse the repository at this point in the history
…e new string will fit
  • Loading branch information
craigminihan committed Jul 22, 2015
1 parent 8e27c9a commit 6f98352
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/libscriptobject/script_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,40 @@ const char* rs::scriptobject::ScriptObject::getString(const char* name) const {
return str;
}

bool rs::scriptobject::ScriptObject::setString(int index, const char* value) {
ScriptObjectKey key;
if (!keys->getKey(index, key)) {
throw UnknownScriptObjectFieldException();
}

return setString(key, value);
}

bool rs::scriptobject::ScriptObject::setString(const char* name, const char* value) {
ScriptObjectKey key;
if (!keys->getKey(name, key)) {
throw UnknownScriptObjectFieldException();
}

return setString(key, value);
}

bool rs::scriptobject::ScriptObject::setString(const ScriptObjectKey& key, const char* value) {
if (key.type != (unsigned)ScriptObjectType::String) {
throw TypeCastException();
}

auto fieldLength = getStringFieldLength(key);
if (std::strlen(value) + 1 > fieldLength) {
return false;
} else {
auto str = const_cast<char*>(reinterpret_cast<const char*>(getValueStart()));
str += valueOffsets[key.index];
std::strcpy(str, value);
return true;
}
}

double rs::scriptobject::ScriptObject::getDouble(int index) const {
ScriptObjectKey key;
if (!keys->getKey(index, key)) {
Expand Down
4 changes: 4 additions & 0 deletions src/libscriptobject/script_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct ScriptObject {

const char* getString(int index) const;
const char* getString(const char* name) const;
bool setString(int index, const char* value);
bool setString(const char* name, const char* value);

std::int32_t getInt32(int index) const;
std::int32_t getInt32(const char* name) const;
Expand Down Expand Up @@ -86,6 +88,8 @@ struct ScriptObject {

unsigned getStringFieldLength(const ScriptObjectKey&) const;

bool setString(const ScriptObjectKey&, const char*);

static void ScriptObjectDeleter(ScriptObject* ptr);

friend class ScriptObjectFactory;
Expand Down
30 changes: 30 additions & 0 deletions src/libscriptobject/tests/simple_object_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,4 +1218,34 @@ TEST_F(SimpleObjectTests, test34) {
ASSERT_THROW({
object->getStringFieldLength("xyz");
}, rs::scriptobject::UnknownScriptObjectFieldException);
}

TEST_F(SimpleObjectTests, test35) {
rs::scriptobject::utils::ScriptObjectVectorSource defn({
std::make_pair("pi", rs::scriptobject::utils::VectorValue(3.14159)),
std::make_pair("text", rs::scriptobject::utils::VectorValue("lorem ipsum")),
std::make_pair("the_answer", rs::scriptobject::utils::VectorValue(42))
});

auto object = rs::scriptobject::ScriptObjectFactory::CreateObject(defn);

ASSERT_TRUE(object->setString(1, "hello world"));
ASSERT_STREQ("hello world", object->getString(1));

ASSERT_FALSE(object->setString(1, "hello world from mars"));
ASSERT_STREQ("hello world", object->getString(1));

ASSERT_TRUE(object->setString("text", "welcome"));
ASSERT_STREQ("welcome", object->getString("text"));

ASSERT_FALSE(object->setString("text", "hello world from mars"));
ASSERT_STREQ("welcome", object->getString("text"));

ASSERT_THROW({
object->setString("the_answer", "once more into the ....");
}, rs::scriptobject::TypeCastException);

ASSERT_THROW({
object->setString("xyz", "the bomb");
}, rs::scriptobject::UnknownScriptObjectFieldException);
}

0 comments on commit 6f98352

Please sign in to comment.