Game Development Reference
In-Depth Information
YAML's natural flow is visually striking: unlike most markup languages, its
syntax is not cluttered by cryptic tags, quotation marks, and redundancy. It can
also be noted that YAML is a superset of the JavaScript Object Notation (JSON)
[Crockford 06] since version 1.2, which means that a YAML parser can understand
any JSON file. This language is widely used as a configuration and data interchange
format on the Internet for its compactness and readability.
Two of our requirements for the adoption of DDD principles are achieved by
YAML's simple but powerful syntax:
Human legibility of the data,
Support for structured data containers.
We can now focus on the last and most important requirement for developers: ease
of integration into C++ code.
20.3 Seamless C++ Integration
Our goal is the seamless integration of YAML-defined data into C++ code with
minimal wheel reinventing. We provide a thin wrapper for the yaml-cpp library
[YCL 10], intended to exploit most of the features of this open-source project while
having a single entry point for our game engine code.
#include <yaml.h>
class Config
{
public:
typedef YAML::Node Node;
Config(std::istream &input) throw();
const Node &operator[](const std::string &key) const throw;
protected:
Node _doc;
};
Listing 20.3. Our minimal YAML configuration wrapper interface for C++.
The Config wrapper interface (Listing 20.3) is minimalist and achieves only
two tasks: reading a single YAML document through its constructor, and accessing
YAML elements through their YAML key path. The yaml-cpp library exploits
modern C++ features like lexical casting and templated cast operators [Meyers 96],
automagically translating YAML scalars, mappings, and sequences into C++ base
types, STL maps, and vectors, whenever possible.
Search WWH ::




Custom Search