Commit 611c60c7 authored by gbalduzz's avatar gbalduzz
Browse files

cleanup

parent e088cfb1
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -27,26 +27,37 @@ public:
  JSONReader(bool verbose = true);
  ~JSONReader() = default;

  // Load a file content in memory.
  // Throws std::logic_error if the file is not correctly formatted, with a description of the issue.
  void open_file(const std::string& filename);

  // Clears the internal memory. The reader will be able to read another file.
  void close_file() noexcept;

  // Opens a new group from the currently topmost open group. Returns false if the group does not
  // exists, but still pushes a null group to the stack of open groups.
  bool open_group(const std::string& name) noexcept;

  // Closes the topmost open griup, irreardless of its validity. Returns false if trying to close
  // the root group.
  bool close_group() noexcept;

  constexpr static bool is_reader = true;
  constexpr static bool is_writer = false;

  // Reads the variable with given name from the current group into the object "obj".
  // If the variable or the currently open group does not exist, or the internal string
  // representation is not compatible with the desired type "T" returns false and leaves "obj"
  // unmodified.
  template <class T>
  bool execute(const std::string& name, T& obj) noexcept;

  template <class Scalar, class Domain>
  bool execute(const std::string& name, func::function<Scalar, Domain>& f) noexcept;

  template <class Scalar>
  bool execute(const std::string& name, linalg::Matrix<Scalar, dca::linalg::CPU>& m);
  bool execute(const std::string& name, linalg::Matrix<Scalar, dca::linalg::CPU>& m) noexcept;

  template <class T>
  bool execute(T& f) {
  bool execute(T& f) noexcept {
    return execute(f.get_name(), f);
  }

@@ -99,7 +110,8 @@ bool JSONReader::execute(const std::string& name, func::function<Scalar, Domain>
}

template <class Scalar>
bool JSONReader::execute(const std::string& name, linalg::Matrix<Scalar, dca::linalg::CPU>& m) {
bool JSONReader::execute(const std::string& name,
                         linalg::Matrix<Scalar, dca::linalg::CPU>& m) noexcept {
  open_group(name);

  bool result = true;
+22 −15
Original line number Diff line number Diff line
@@ -27,45 +27,51 @@ public:
  JSONWriter(bool verbose = true);
  ~JSONWriter();

  // Acquires lock on file. If "overwrite" is true and the file exists, the content is read into
  // memory and serves as the initial state of the writer.
  void open_file(const std::string& filename, bool overwrite = true);

  // Flushes and closes the file.
  void close_file();

  // Writes the current state to disk into the opened file.
  void flush();

  // Creates or opens a pre-existing group.
  // Throws if there is a data entry with the same name in the current group.
  void open_group(const std::string& name);
  // Closes the topmost open group.
  // Precondition: the current group is not the root.
  void close_group();

  constexpr static bool is_reader = false;
  constexpr static bool is_writer = true;

  void set_verbose(bool verbose) {
  void set_verbose(bool verbose) noexcept {
    verbose_ = verbose;
  }

  // Returns true if a file is open.
  operator bool() const noexcept;

  // Commits to the internal data representation the value of "obj".
  template <class T>
  void execute(const std::string& name, const T& obj);

  void execute(const std::string& name, const T& obj) noexcept;
  template <class Scalar, class Domain>
  void execute(const std::string& name, const func::function<Scalar, Domain>& f);

  void execute(const std::string& name, const func::function<Scalar, Domain>& f) noexcept;
  template <class Scalar>
  void execute(const std::string& name, const linalg::Matrix<Scalar, dca::linalg::CPU>& m);

  void execute(const std::string& name, const linalg::Matrix<Scalar, dca::linalg::CPU>& m) noexcept;
  template <class T>
  void execute(const std::string& name, const std::unique_ptr<T>& ptr) {
  void execute(const std::string& name, const std::unique_ptr<T>& ptr) noexcept {
    if (ptr)
      execute(name, *ptr);
  }

  template <class T>
  void execute(const T& f) {
  void execute(const T& f) noexcept {
    execute(f.get_name(), f);
  }

  template <class T>
  void execute(const std::unique_ptr<T>& f) {
  void execute(const std::unique_ptr<T>& f) noexcept {
    if (f)
      execute(f->get_name(), *f);
  }
@@ -79,12 +85,12 @@ private:
};

template <class T>
void JSONWriter::execute(const std::string& name, const T& obj) {
void JSONWriter::execute(const std::string& name, const T& obj) noexcept {
  open_groups_.top()->addEntry(name, obj);
}

template <class Scalar, class Domain>
void JSONWriter::execute(const std::string& name, const func::function<Scalar, Domain>& f) {
void JSONWriter::execute(const std::string& name, const func::function<Scalar, Domain>& f) noexcept {
  if (verbose_)
    std::cout << "\t starts writing function : " << f.get_name() << "\n";

@@ -98,7 +104,8 @@ void JSONWriter::execute(const std::string& name, const func::function<Scalar, D
}

template <class Scalar>
void JSONWriter::execute(const std::string& name, const linalg::Matrix<Scalar, dca::linalg::CPU>& m) {
void JSONWriter::execute(const std::string& name,
                         const linalg::Matrix<Scalar, dca::linalg::CPU>& m) noexcept {
  open_group(name);

  std::vector<std::vector<Scalar>> data(m.nrRows());