Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed May 24, 2018
1 parent 0566e25 commit 38baead
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 107 deletions.
37 changes: 19 additions & 18 deletions asdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,26 @@ asdf::asdf(const copy_state &cs, const asdf &project) {
grp = make_shared<group>(cs, *project.grp);
}

writer &asdf::to_yaml(writer &ws) const {
ws << YAML::VerbatimTag("tag:stsci.edu:asdf/core/asdf-1.1.0");
ws << YAML::BeginMap;
ws << YAML::Key << "asdf_library" << YAML::Value
<< software("asdf-cxx", "Erik Schnetter",
"https://github.com/eschnett/asdf-cxx", ASDF_VERSION);
writer &asdf::to_yaml(writer &w) const {
w << YAML::VerbatimTag("tag:stsci.edu:asdf/core/asdf-1.1.0");
w << YAML::BeginMap;
w << YAML::Key << "asdf_library" << YAML::Value
<< software("asdf-cxx", "Erik Schnetter",
"https://github.com/eschnett/asdf-cxx", ASDF_VERSION);
for (const auto &kv : data)
ws << YAML::Key << kv.first << YAML::Value << *kv.second;
w << YAML::Key << kv.first << YAML::Value << *kv.second;
// if (tab)
// node["table"] = tab->to_yaml(ws);
// node["table"] = tab->to_yaml(w);
if (grp)
ws << YAML::Key << "group" << YAML::Value << *grp;
w << YAML::Key << "group" << YAML::Value << *grp;
for (const auto &kv : nodes)
ws << YAML::Key << kv.first << YAML::Value << kv.second;
// if (writer)
// for (const auto &kv : writer(ws))
// node[kv.first] = kv.second;
ws << YAML::EndMap;
return ws;
w << YAML::Key << kv.first << YAML::Value << kv.second;
for (const auto &kv : writers) {
w << YAML::Key << kv.first << YAML::Value;
kv.second(w);
}
w << YAML::EndMap;
return w;
}

asdf::asdf(istream &is) {
Expand All @@ -92,9 +93,9 @@ asdf::asdf(istream &is) {
asdf asdf::copy(const copy_state &cs) const { return asdf(cs, *this); }

void asdf::write(ostream &os) const {
writer ws(os);
ws << *this;
ws.flush();
writer w(os);
w << *this;
w.flush();
}

} // namespace ASDF
10 changes: 0 additions & 10 deletions asdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
#include "asdf_stl.hpp"
#include "asdf_table.hpp"

// namespace ASDF {

// template <typename T>
// writer &operator<<(writer &ws, const T &value) {
// ws.emitter << value;
// return ws;
// }

// } // namespace ASDF

#define ASDF_HPP_DONE
#endif // #ifndef ASDF_HPP
#ifndef ASDF_HPP_DONE
Expand Down
12 changes: 6 additions & 6 deletions asdf_asdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class asdf {
// shared_ptr<table> tab;
shared_ptr<group> grp;
map<string, YAML::Node> nodes;
// function<map<string, YAML::Node>(writer &ws)> writer;
map<string, function<void(writer &w)>> writers;

public:
asdf() = delete;
Expand All @@ -35,14 +35,14 @@ class asdf {
// asdf(const shared_ptr<table> &tab) : tab(tab) { assert(tab); }
asdf(const shared_ptr<group> &grp) : grp(grp) { assert(grp); }
asdf(const map<string, YAML::Node> &nodes) : nodes(nodes) {}
// asdf(const function<map<string, YAML::Node>(writer &ws)> &writer)
// : writer(writer) {}
asdf(const map<string, function<void(writer &w)>> &writers)
: writers(writers) {}

asdf(const reader_state &rs, const YAML::Node &node);
asdf(const copy_state &cs, const asdf &project);
writer &to_yaml(writer &ws) const;
friend writer &operator<<(writer &ws, const asdf &proj) {
return proj.to_yaml(ws);
writer &to_yaml(writer &w) const;
friend writer &operator<<(writer &w, const asdf &proj) {
return proj.to_yaml(w);
}

asdf(istream &is);
Expand Down
4 changes: 2 additions & 2 deletions asdf_datatype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class field_t {

field_t(const reader_state &rs, const YAML::Node &node);
field_t(const copy_state &cs, const field_t &field);
YAML::Node to_yaml(writer &ws) const;
YAML::Node to_yaml(writer &w) const;
};

class datatype_t {
Expand All @@ -207,7 +207,7 @@ class datatype_t {

datatype_t(const reader_state &rs, const YAML::Node &node);
datatype_t(const copy_state &cs, const datatype_t &datatype);
YAML::Node to_yaml(writer &ws) const;
YAML::Node to_yaml(writer &w) const;

size_t type_size() const;
};
Expand Down
12 changes: 6 additions & 6 deletions asdf_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class entry {

entry(const reader_state &rs, const YAML::Node &node);
entry(const copy_state &cs, const entry &ent);
writer &to_yaml(writer &ws) const;
friend writer &operator<<(writer &ws, const entry &ent) {
return ent.to_yaml(ws);
writer &to_yaml(writer &w) const;
friend writer &operator<<(writer &w, const entry &ent) {
return ent.to_yaml(w);
}
};

Expand All @@ -70,9 +70,9 @@ class group {

group(const reader_state &rs, const YAML::Node &node);
group(const copy_state &cs, const group &grp);
writer &to_yaml(writer &ws) const;
friend writer &operator<<(writer &ws, const group &grp) {
return grp.to_yaml(ws);
writer &to_yaml(writer &w) const;
friend writer &operator<<(writer &w, const group &grp) {
return grp.to_yaml(w);
}
};

Expand Down
10 changes: 3 additions & 7 deletions asdf_ndarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,12 @@ class ndarray {

ndarray(const reader_state &rs, const YAML::Node &node);
ndarray(const copy_state &cs, const ndarray &arr);
writer &to_yaml(writer &ws) const;
friend writer &operator<<(writer &ws, const ndarray &arr) {
return arr.to_yaml(ws);
writer &to_yaml(writer &w) const;
friend writer &operator<<(writer &w, const ndarray &arr) {
return arr.to_yaml(w);
}
};

// inline writer &make_yaml(writer &ws, const ndarray &arr) {
// return arr.to_yaml(ws);
// }

} // namespace ASDF

#define ASDF_NDARRAY_HPP_DONE
Expand Down
12 changes: 6 additions & 6 deletions asdf_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class column {

column(const reader_state &rs, const YAML::Node &node);
column(const copy_state &cs, const column &col);
writer &to_yaml(writer &ws) const;
friend writer &operator<<(writer &ws, const column &col) {
return col.to_yaml(ws);
writer &to_yaml(writer &w) const;
friend writer &operator<<(writer &w, const column &col) {
return col.to_yaml(w);
}
};

Expand All @@ -54,9 +54,9 @@ class table {

table(const reader_state &rs, const YAML::Node &node);
table(const copy_state &cs, const table &tab);
writer &to_yaml(writer &ws) const;
friend writer &operator<<(writer &ws, const table &tab) {
return tab.to_yaml(ws);
writer &to_yaml(writer &w) const;
friend writer &operator<<(writer &w, const table &tab) {
return tab.to_yaml(w);
}
};

Expand Down
8 changes: 4 additions & 4 deletions datatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ field_t::field_t(const reader_state &rs, const YAML::Node &node) { assert(0); }

field_t::field_t(const copy_state &cs, const field_t &field) { assert(0); }

YAML::Node field_t::to_yaml(writer &ws) const {
YAML::Node field_t::to_yaml(writer &w) const {
YAML::Node node;
if (!name.empty())
node["name"] = name;
node["datatype"] = datatype->to_yaml(ws);
node["datatype"] = datatype->to_yaml(w);
if (have_byteorder)
node["byteorder"] = yaml_encode(byteorder);
if (!shape.empty())
Expand Down Expand Up @@ -478,12 +478,12 @@ datatype_t::datatype_t(const copy_state &cs, const datatype_t &datatype) {
assert(0);
}

YAML::Node datatype_t::to_yaml(writer &ws) const {
YAML::Node datatype_t::to_yaml(writer &w) const {
if (is_scalar)
return yaml_encode(scalar_type_id);
YAML::Node node;
for (const auto &field : fields)
node.push_back(field->to_yaml(ws));
node.push_back(field->to_yaml(w));
return node;
}

Expand Down
30 changes: 15 additions & 15 deletions group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ entry::entry(const copy_state &cs, const entry &ent)
arr = make_shared<ndarray>(cs, *ent.arr);
}

writer &entry::to_yaml(writer &ws) const {
ws << YAML::VerbatimTag("tag:github.com/eschnett/asdf-cxx/core/entry-1.0.0");
ws << YAML::BeginMap;
ws << YAML::Key << "name" << YAML::Value << name;
writer &entry::to_yaml(writer &w) const {
w << YAML::VerbatimTag("tag:github.com/eschnett/asdf-cxx/core/entry-1.0.0");
w << YAML::BeginMap;
w << YAML::Key << "name" << YAML::Value << name;
if (grp)
ws << YAML::Key << "group" << YAML::Value << *grp;
w << YAML::Key << "group" << YAML::Value << *grp;
if (arr)
ws << YAML::Key << "data" << YAML::Value << *arr;
w << YAML::Key << "data" << YAML::Value << *arr;
if (!description.empty())
ws << YAML::Key << "description" << YAML::Value << description;
ws << YAML::EndMap;
return ws;
w << YAML::Key << "description" << YAML::Value << description;
w << YAML::EndMap;
return w;
}

group::group(const reader_state &rs, const YAML::Node &node) {
Expand All @@ -49,13 +49,13 @@ group::group(const copy_state &cs, const group &grp) {
entries[kv.first] = make_shared<entry>(cs, *kv.second);
}

writer &group::to_yaml(writer &ws) const {
ws << YAML::VerbatimTag("tag:github.com/eschnett/asdf-cxx/core/group-1.0.0");
ws << YAML::BeginMap;
writer &group::to_yaml(writer &w) const {
w << YAML::VerbatimTag("tag:github.com/eschnett/asdf-cxx/core/group-1.0.0");
w << YAML::BeginMap;
for (const auto &kv : entries)
ws << YAML::Key << kv.first << YAML::Value << *kv.second;
ws << YAML::EndMap;
return ws;
w << YAML::Key << kv.first << YAML::Value << *kv.second;
w << YAML::EndMap;
return w;
}

} // namespace ASDF
32 changes: 16 additions & 16 deletions ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,39 +520,39 @@ ndarray::ndarray(const copy_state &cs, const ndarray &arr) : ndarray(arr) {
compression = cs.compression;
}

writer &ndarray::to_yaml(writer &ws) const {
ws << YAML::VerbatimTag("tag:stsci.edu:asdf/core/ndarray-1.0.0");
ws << YAML::BeginMap;
writer &ndarray::to_yaml(writer &w) const {
w << YAML::VerbatimTag("tag:stsci.edu:asdf/core/ndarray-1.0.0");
w << YAML::BeginMap;
if (block_format == block_format_t::block) {
// source
const auto &self = *this;
uint64_t idx = ws.add_task([=](ostream &os) { self.write_block(os); });
ws << YAML::Key << "source" << YAML::Value << idx;
uint64_t idx = w.add_task([=](ostream &os) { self.write_block(os); });
w << YAML::Key << "source" << YAML::Value << idx;
} else {
// data
ws << YAML::Key << "data" << YAML::Value
<< emit_inline_array(static_cast<const unsigned char *>(data->ptr()) +
offset,
datatype, byteorder, shape, strides);
w << YAML::Key << "data" << YAML::Value
<< emit_inline_array(static_cast<const unsigned char *>(data->ptr()) +
offset,
datatype, byteorder, shape, strides);
}
// mask
assert(mask.empty());
// datatype
ws << YAML::Key << "datatype" << YAML::Value << datatype->to_yaml(ws);
w << YAML::Key << "datatype" << YAML::Value << datatype->to_yaml(w);
if (block_format == block_format_t::block) {
// byteorder
ws << YAML::Key << "byteorder" << YAML::Value << yaml_encode(byteorder);
w << YAML::Key << "byteorder" << YAML::Value << yaml_encode(byteorder);
}
// shape
ws << YAML::Key << "shape" << YAML::Value << YAML::Flow << shape;
w << YAML::Key << "shape" << YAML::Value << YAML::Flow << shape;
if (block_format == block_format_t::block) {
// offset
ws << YAML::Key << "offset" << YAML::Value << offset;
w << YAML::Key << "offset" << YAML::Value << offset;
// strides
ws << YAML::Key << "strides" << YAML::Value << YAML::Flow << strides;
w << YAML::Key << "strides" << YAML::Value << YAML::Flow << strides;
}
ws << YAML::EndMap;
return ws;
w << YAML::EndMap;
return w;
}

} // namespace ASDF
34 changes: 17 additions & 17 deletions table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ column::column(const reader_state &rs, const YAML::Node &node) {

column::column(const copy_state &cs, const column &col) : column(col) {}

writer &column::to_yaml(writer &ws) const {
ws << YAML::VerbatimTag("tag:stsci.edu:asdf/core/column-1.0.0");
ws << YAML::BeginMap;
ws << YAML::Key << "name" << YAML::Value << name;
ws << YAML::Key << "data" << YAML::Value << *data;
writer &column::to_yaml(writer &w) const {
w << YAML::VerbatimTag("tag:stsci.edu:asdf/core/column-1.0.0");
w << YAML::BeginMap;
w << YAML::Key << "name" << YAML::Value << name;
w << YAML::Key << "data" << YAML::Value << *data;
if (!description.empty())
ws << YAML::Key << "description" << YAML::Value << description;
ws << YAML::EndMap;
return ws;
w << YAML::Key << "description" << YAML::Value << description;
w << YAML::EndMap;
return w;
}

table::table(const reader_state &rs, const YAML::Node &node) {
Expand All @@ -36,16 +36,16 @@ table::table(const copy_state &cs, const table &tab) {
columns.push_back(make_shared<column>(cs, *col));
}

writer &table::to_yaml(writer &ws) const {
ws << YAML::VerbatimTag("tag:stsci.edu:asdf/core/table-1.0.0");
ws << YAML::BeginMap;
ws << YAML::Key << "columns" << YAML::Value;
ws << YAML::BeginSeq;
writer &table::to_yaml(writer &w) const {
w << YAML::VerbatimTag("tag:stsci.edu:asdf/core/table-1.0.0");
w << YAML::BeginMap;
w << YAML::Key << "columns" << YAML::Value;
w << YAML::BeginSeq;
for (size_t i = 0; i < columns.size(); ++i)
ws << *columns[i];
ws << YAML::EndSeq;
ws << YAML::EndMap;
return ws;
w << *columns[i];
w << YAML::EndSeq;
w << YAML::EndMap;
return w;
}

} // namespace ASDF

0 comments on commit 38baead

Please sign in to comment.