Skip to content

Commit bf4f59e

Browse files
committed
fixed INI serialization
1 parent d11744e commit bf4f59e

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

kit/meta/meta.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ enum class MetaFormat : unsigned {
247247
UNKNOWN=0,
248248
JSON,
249249
INI,
250-
HUMAN,
250+
YAML,
251251
//BSON
252252
};
253253

@@ -271,7 +271,8 @@ enum class MetaSerialize : unsigned {
271271
template<
272272
class Mutex=kit::dummy_mutex,
273273
template <typename> class Storage=kit::local_shared_ptr,
274-
template <typename> class This=kit::enable_shared_from_this
274+
template <typename> class This=kit::enable_shared_from_this//,
275+
//template <typename> class Make=kit::make_local_shared
275276
>
276277
class MetaBase:
277278
public This<MetaBase<Mutex,Storage,This>>,
@@ -286,6 +287,15 @@ class MetaBase:
286287
F_CREATE = kit::bit(0),
287288
F_MERGE = kit::bit(1)
288289
};
290+
291+
//template<class... Args>
292+
//static Storage<Mutex,Storage,This,Make> make(Args&&... args) {
293+
// return Make<MeteBase<Mutex,Storage,This,Make>>((std::forward<Args>(args)...));
294+
//}
295+
//static Storage<Mutex,Storage,This,Make> json(const std::string& s) {
296+
// return Make<MeteBase<Mutex,Storage,This,Make>>(MetaFormat::JSON,s);
297+
//}
298+
289299
//typedef Mutex mutex_type;
290300

291301
//struct Iterator

kit/meta/meta.inl

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <boost/filesystem.hpp>
22
#include <boost/lexical_cast.hpp>
33
#include <sstream>
4+
#include <string>
45
#include "meta.h"
56
#include "../log/log.h"
67
#include "../kit.h"
@@ -344,7 +345,7 @@ MetaLoop MetaBase<Mutex,Storage,This>::each(
344345

345346
// use type checks with typeid() before attempting conversion
346347
// or boost::any cast?
347-
// POD types should also work if behind smart ptrs
348+
// POD types should also work if behind smart ptrs -- not yet impl
348349
// only Storage's to other trees should serialize (not weak)
349350

350351
/*
@@ -532,17 +533,42 @@ std::string MetaBase<Mutex,Storage,This> :: serialize(MetaFormat fmt, unsigned f
532533
if(m)
533534
{
534535
data += "["+e.key+"]\n";
535-
for(auto&& j: *m)
536-
data += j.key + "=" + kit::any_to_string(j.value) + "\n";
536+
for(auto&& j: *m){
537+
// TODO: write sub category [foo.bar]
538+
//if(e.type.id==MetaType::ID::META)
539+
// data += j.key + "=" + boost::any_cast<Storage<MetaBase<Mutex,Storage,This>>>(j.value) + "\n";
540+
if(j.type.id==MetaType::ID::INT)
541+
data += j.key + "=" + std::to_string(boost::any_cast<int>(j.value)) + "\n";
542+
else if(j.type.id==MetaType::ID::REAL)
543+
data += j.key + "=" + std::to_string(boost::any_cast<double>(j.value)) + "\n";
544+
else if(j.type.id==MetaType::ID::BOOL)
545+
data += j.key + "=" + (boost::any_cast<bool>(j.value)?"true":"false") + "\n";
546+
else if(j.type.id==MetaType::ID::STRING)
547+
data += j.key + "=" + boost::any_cast<std::string>(j.value) + "\n";
548+
else{
549+
//LOG(std::to_string(int(j.type.id)));
550+
WARNING("warning: cannot serialize value");
551+
}
552+
}
537553
data += "\n";
538554
}
539555
else
540556
{
541-
data += e.key + "=" + kit::any_to_string(e.value) + "\n";
557+
//data += e.key + "=" + kit::any_to_string(e.value) + "\n";
558+
if(e.type.id==MetaType::ID::INT)
559+
data += e.key + "=" + std::to_string(boost::any_cast<int>(e.value)) + "\n";
560+
else if(e.type.id==MetaType::ID::REAL)
561+
data += e.key + "=" + std::to_string(boost::any_cast<double>(e.value)) + "\n";
562+
else if(e.type.id==MetaType::ID::BOOL)
563+
data += e.key + "=" + (boost::any_cast<bool>(e.value)?"true":"false") + "\n";
564+
else if(e.type.id==MetaType::ID::STRING)
565+
data += e.key + "=" + boost::any_cast<std::string>(e.value) + "\n";
566+
else
567+
WARNING("warning: cannot serialize value");
542568
}
543569
}
544570
}
545-
//else if (fmt == MetaFormat::HUMAN)
571+
//else if (fmt == MetaFormat::YAML)
546572
// assert(false);
547573
else
548574
assert(false);
@@ -653,9 +679,8 @@ void MetaBase<Mutex,Storage,This> :: deserialize(MetaFormat fmt, std::istream& d
653679
//deserialize_json(root, pth_vec);
654680
}
655681
}
656-
else if (fmt == MetaFormat::HUMAN)
682+
else if (fmt == MetaFormat::YAML)
657683
{
658-
// human deserialization unsupported
659684
assert(false);
660685
}
661686
else if (fmt == MetaFormat::INI)

tests/meta.test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,18 @@ TEST_CASE("Meta","[meta]") {
219219

220220
SECTION("objects") {
221221
auto m = make_local_shared<Meta>(MetaFormat::JSON,"{\"one\":1}");
222+
REQUIRE(!m->empty());
222223
REQUIRE(m->at<int>("one") == 1);
224+
REQUIRE(!m->empty());
223225
m->clear();
226+
REQUIRE(m->empty());
224227

225228
m = make_local_shared<Meta>();
226229
m->deserialize(MetaFormat::JSON,"{\"one\":1}");
227230
REQUIRE(m->at<int>("one") == 1);
228231

229232
string data = m->serialize(MetaFormat::JSON);
233+
REQUIRE(!m->empty());
230234
m->clear();
231235
REQUIRE(m->empty());
232236
m->deserialize(MetaFormat::JSON,data);

0 commit comments

Comments
 (0)