Essentia  2.1-beta6-dev
yamlast.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef YAML_AST_H
21 #define YAML_AST_H
22 
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include <exception>
27 #include <sstream>
28 #include <yaml.h>
29 
30 namespace essentia {
31 
32 class YamlException : public std::exception {
33 
34  public:
35  YamlException(const char* msg) : exception(), _msg(msg) {}
36  YamlException(const std::string& msg) : exception(), _msg(msg) {}
37  YamlException(const std::ostringstream& msg) : exception(), _msg(msg.str()) {}
38 
39  template <typename T, typename U>
40  YamlException(const T& a, const U& b) : exception() {
41  std::ostringstream oss; oss << a << b; _msg = oss.str();
42  }
43 
44  template <typename T, typename U, typename V>
45  YamlException(const T& a, const U& b, const V& c) : exception() {
46  std::ostringstream oss; oss << a << b << c; _msg = oss.str();
47  }
48 
49  virtual ~YamlException() throw() {}
50  virtual const char* what() const throw() { return _msg.c_str(); }
51 
52  protected:
53  std::string _msg;
54 };
55 
56 
57 
58 class YamlNode {
59  public:
60  virtual ~YamlNode() = 0;
61 };
62 
63 
64 class YamlScalarNode : public YamlNode {
65  public:
68  FLOAT
69  };
70 
71  private:
73  std::string _strDS;
74  float _floatDS;
75 
76  public:
77  YamlScalarNode(const float& f) : _tp(FLOAT), _floatDS(f) {}
78  YamlScalarNode(const std::string& s) : _tp(STRING), _strDS(s) {}
79  const YamlScalarType& getType() const { return _tp; }
80  virtual ~YamlScalarNode() {}
81  const std::string& toString() const {
82  if (_tp != STRING) throw YamlException("YamlScalarNode is not a string");
83  return _strDS;
84  }
85  const float& toFloat() const {
86  if (_tp != FLOAT) throw YamlException("YamlScalarNode is not a float");
87  return _floatDS;
88  }
89 };
90 
91 
92 class YamlSequenceNode : public YamlNode {
93  public:
94  const std::vector<YamlNode*>& getData() const { return _data; }
95  void add(YamlNode* n) { _data.push_back(n); }
96  virtual ~YamlSequenceNode();
97  const int size() const { return int(_data.size()); }
98  const bool empty() const { return _data.empty(); }
99 
100  private:
101  std::vector<YamlNode*> _data;
102 };
103 
104 
105 class YamlMappingNode : public YamlNode {
106  public:
107  const std::map<std::string, YamlNode*>& getData() const { return _data; }
108  void add(const std::string& key, YamlNode* value) { _data[key] = value; }
109  virtual ~YamlMappingNode();
110  const int size() const { return int(_data.size()); }
111 
112  private:
113  std::map<std::string, YamlNode*> _data;
114 };
115 
116 YamlNode* parseYaml(FILE*, const std::string& fileString = std::string());
117 
118 } // namespace essentia
119 
120 #endif // YAML_AST_H
Definition: yamlast.h:32
std::string _msg
Definition: yamlast.h:53
YamlException(const std::string &msg)
Definition: yamlast.h:36
YamlException(const char *msg)
Definition: yamlast.h:35
YamlException(const std::ostringstream &msg)
Definition: yamlast.h:37
YamlException(const T &a, const U &b)
Definition: yamlast.h:40
YamlException(const T &a, const U &b, const V &c)
Definition: yamlast.h:45
virtual const char * what() const
Definition: yamlast.h:50
virtual ~YamlException()
Definition: yamlast.h:49
Definition: yamlast.h:105
void add(const std::string &key, YamlNode *value)
Definition: yamlast.h:108
const std::map< std::string, YamlNode * > & getData() const
Definition: yamlast.h:107
std::map< std::string, YamlNode * > _data
Definition: yamlast.h:113
const int size() const
Definition: yamlast.h:110
Definition: yamlast.h:58
virtual ~YamlNode()=0
Definition: yamlast.h:64
const YamlScalarType & getType() const
Definition: yamlast.h:79
float _floatDS
Definition: yamlast.h:74
const float & toFloat() const
Definition: yamlast.h:85
YamlScalarNode(const float &f)
Definition: yamlast.h:77
virtual ~YamlScalarNode()
Definition: yamlast.h:80
YamlScalarType
Definition: yamlast.h:66
@ FLOAT
Definition: yamlast.h:68
@ STRING
Definition: yamlast.h:67
YamlScalarNode(const std::string &s)
Definition: yamlast.h:78
YamlScalarType _tp
Definition: yamlast.h:72
std::string _strDS
Definition: yamlast.h:73
const std::string & toString() const
Definition: yamlast.h:81
Definition: yamlast.h:92
std::vector< YamlNode * > _data
Definition: yamlast.h:101
const bool empty() const
Definition: yamlast.h:98
const std::vector< YamlNode * > & getData() const
Definition: yamlast.h:94
const int size() const
Definition: yamlast.h:97
void add(YamlNode *n)
Definition: yamlast.h:95
Definition: algorithm.h:28
YamlNode * parseYaml(FILE *, const std::string &fileString=std::string())