Skip to content

Commit

Permalink
Let Group be copyable.
Browse files Browse the repository at this point in the history
Storage is still uncopyable. But I give Storage the ability to
create a default JSON object. Here is an example:

Storage has_JObj; ///< this will create a default JSON object
EXPECT_TRUE(has_JObj.isValid());

Storage has_no_JObj(Storage::Empty); ///< it's waiting for read/open
EXPECT_FALSE(has_JObj.isValid());
has_no_JObj.read("{ }");
  • Loading branch information
lubatang committed Jan 3, 2019
1 parent 83c6422 commit 4ec493f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
7 changes: 7 additions & 0 deletions include/onnc/JSON/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ class Group
typedef GroupIterator iterator;

public:
/// default constructor is a Null group.
Group();

Group(Storage& pParent, StringRef pName);

Group(const Group& pOther);

~Group() { }

Group& operator=(const Group& pOther);

/// group - Return the sub-group @ref pName.
/// This function is unsafe. If there is no sub-group named @ref pKey, then
/// the function behavior is undefined.
Expand Down
19 changes: 18 additions & 1 deletion include/onnc/JSON/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ namespace json {
* a parent group. @ref Storage::group and @ref Group::group returns child
* group.
*
* In case that you uses storage as a member variable, you might want the
* storage object is invalid and can postpone open/read calls. You can add
* a dummy integer in the constructor parameter. Here is the example:
* \code
* json::Storage storage(0);
* storage.read("{ }");
* \endcode
*
* An entry is a key-value pair.
* A key must be a string, used to identify an entry. A value can be multiple
* types:
Expand All @@ -66,10 +74,19 @@ class Storage : private Uncopyable
kReadWrite ///< Opened, and will write back to JSON file in destructor.
};

enum {
Empty
};

public:
/// Default constructor. A invalid storage, because we didn't read any thing.
/// Default constructor. It is equivalent to Storage("{ }")
Storage();

/// Constructor with dummy integer. This is a invalid storage (not read
/// anything).
/// This constructor is used to postpone open/read calls.
explicit Storage(int);

/// Read the JSON object from string @ref pContent
///
/// @param[in] pContent The content of a configuration file
Expand Down
3 changes: 1 addition & 2 deletions lib/Analysis/Statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ using namespace onnc;
// It is reduced to an adapter of StatisticsPrivate.
//===----------------------------------------------------------------------===//
Statistics::Statistics()
: json::Storage() {
// read nothing
: json::Storage(json::Storage::Empty) {
}

Statistics::Statistics(StringRef pContent)
Expand Down
14 changes: 14 additions & 0 deletions lib/JSON/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ Group Group::ValueIterator::group()
//===----------------------------------------------------------------------===//
// Group
//===----------------------------------------------------------------------===//
Group::Group()
: m_pObject(&*g_NullObject) {
}

Group::Group(Storage& pParent, StringRef pName)
: m_pObject(pParent.group(pName).m_pObject) {
}
Expand All @@ -117,6 +121,16 @@ Group::Group(json::Object& pObject)
: m_pObject(&pObject) {
}

Group::Group(const Group& pOther)
: m_pObject(pOther.m_pObject) {
}

Group& Group::operator=(const Group& pOther)
{
m_pObject = pOther.m_pObject;
return *this;
}

Group Group::group(StringRef pName)
{
if (hasGroup(pName))
Expand Down
6 changes: 5 additions & 1 deletion lib/JSON/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ namespace json {
//===----------------------------------------------------------------------===//
Storage::Storage()
: m_pGroup(NULL), m_AccessMode(kReadOnly), m_FilePath(), m_Value() {
// read nothing
read("{ }");
}

Storage::Storage(int)
: m_pGroup(NULL), m_AccessMode(kReadOnly), m_FilePath(), m_Value() {
}

Storage::Storage(StringRef pContent)
Expand Down
2 changes: 1 addition & 1 deletion tools/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ add_onnc_test(Any AnyTest.cpp)
add_onnc_test(BinaryTree BinaryTreeTest.cpp)
add_onnc_test(StringSwitch StringSwitchTest.cpp)
add_onnc_test(StringMap StringMapTest.cpp)
add_onnc_test(Json JsonValueTest.cpp JsonObjectTest.cpp)
add_onnc_test(Json JsonValueTest.cpp JsonObjectTest.cpp StorageTest.cpp)
add_onnc_test(ComputeIR ComputeIRTest.cpp)
add_onnc_test(TensorSel TensorSelTest.cpp)
add_onnc_test(MemAllocTest MemAllocTest.cpp)
1 change: 1 addition & 0 deletions tools/unittests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ TEST_SOURCES = DigraphTest.cpp \
BinaryTreeTest.cpp \
StringSwitchTest.cpp \
StringMapTest.cpp \
StorageTest.cpp \
JsonValueTest.cpp \
JsonObjectTest.cpp \
ComputeIRTest.cpp \
Expand Down
29 changes: 29 additions & 0 deletions tools/unittests/StorageTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===- StorageTest.cpp ----------------------------------------------------===//
//
// The ONNC Project
//
// See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <skypat/skypat.h>
#include <onnc/JSON/Storage.h>
#include <onnc/JSON/Group.h>
#include <onnc/Support/IOStream.h>

using namespace onnc;

//===----------------------------------------------------------------------===//
// Storage Test
//===----------------------------------------------------------------------===//
SKYPAT_F(GroupTest, constructor)
{
json::Group group;
EXPECT_TRUE(group.isNull());

json::Storage storage;
json::Group g1 = storage.addGroup("group 1");
outs() << storage << std::endl;
// group = g1;
// EXPECT_FALSE(group.isNull());
// EXPECT_TRUE(group.isEmpty());
}

0 comments on commit 4ec493f

Please sign in to comment.