WaveBlocksND
complex.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <complex>
4 
5 #include <yaml-cpp/yaml.h>
6 
7 #include "../utilities/complexnumber_parser.hpp"
8 
9 
10 namespace YAML {
11  template<>
12  struct convert<std::complex<double> > {
13  static Node encode(const std::complex<double> & rhs) {
14  if (rhs.imag() == 0.0) {
15  return Node(std::to_string(rhs.real()));
16  }
17  else if (rhs.real() == 0.0) {
18  return Node(""+std::to_string(rhs.imag())+"j");
19  }
20  else if (rhs.imag() < 0.0) {
21  return Node(""+std::to_string(rhs.real())+std::to_string(rhs.imag())+"j");
22  }
23  else {
24  return Node(""+std::to_string(rhs.real())+"+"+std::to_string(rhs.imag())+"j");
25  }
26  }
27 
28  static bool decode(const Node& node, std::complex<double> & rhs) {
29  std::string str = node.as<std::string>();
30 
31  // deal with python's annoying habbit to add parentheses to complex numbers
32  if (str.size() >= 2 && str.front() == '(' && str.back() == ')')
33  str = str.substr(1, str.size()-2);
34 
35  return waveblocks::utilities::parse_complex(str.c_str(), rhs);
36  }
37  };
38 }
std::string to_string(T in)
Definition: to_string.hpp:10
Definition: stdarray2stream.hpp:7
static bool decode(const Node &node, std::complex< double > &rhs)
Definition: complex.hpp:28
bool parse_complex(char const *text, std::complex< double > &result)
Definition: complexnumber_parser.cpp:9
Definition: complex.hpp:10
static Node encode(const std::complex< double > &rhs)
Definition: complex.hpp:13