// ObjectFormat.h #ifndef ObjectFormat_H #define ObjectFormat_H // A format is inserted in a stram and dictates the behavior when the // following objects are are inserted or extracted from that stream: // begin_object - signals the start of a new object // end_object - signals the end of the current object // new_line - starts a new line of output with appropriate prefix // (including indentation) // // For example // // cout << Format( "{ ", " }", " ", ">>> " ); // cout << begin_object << "A = " << a << new_line << "B = " << b; // cout << end_object; // // would produce: // // >>> { A = 1.234 // >>> B = 6 } //********************************************************************** #include #include using std::ostream; using std::string; class ObjectFormat { public: // Print the format for a stream. static void print_format(ostream& stream); private: string _begin; string _end; string _indent; string _prefix; int _depth; public: // constructor // Constructor. // begin = string to appear at the start of an object // '\n' is replaced with new_line. // end = string to appear at the end of an object // new_line = string to appear between "lines" // indent = string used to indent // prefix = string to appear in front of each line ObjectFormat(string begin="", string end ="", string indent ="", string prefix =""); public: // non-const functions // Return the start of object string and increment object depth. string begin_object(); // Return the end of object string and decrement object depth. string end_object(); // Return the prefix for a new line. string new_line(); // Copy the state (depth) from another format. void copy_state(const ObjectFormat& fmt); public: // non-const functions // Return the start of object string. string get_begin_object_string() const { return _begin; } // Return the start of object string. string get_end_object_string() const { return _end; } // Return the indent string. string get_indent_string() const { return _indent; } // Return the prefix string. string get_prefix_string() const { return _prefix; } }; //********************************************************************** // Types for operator<<. class BeginObject { }; class EndObject { }; class NewLine { }; //********************************************************************** // Output stream operators. // Associate a format with a stream. ostream& operator<<(ostream& stream, const ObjectFormat& rhs); // Indicate the start of an object. // The start of object string is written to the stream. ostream& operator<<(ostream& stream, const BeginObject& rhs); // Indicate the end of an object. // The end of object string is written to the stream. ostream& operator<<(ostream& stream, const EndObject& rhs); // Start a new line and indent for the current object. // Do NOT call just before end_object; ostream& operator<<(ostream& stream, const NewLine& rhs); //********************************************************************** // Objects intended for stream insertion. // Assign the default format. extern ObjectFormat null_format; // Assign format which indents objects. extern ObjectFormat indented_format; // Assign format which indents and surrounds objects with brackets: []. extern ObjectFormat bracketed_format; // Assign format which indents and surrounds objects with braces: []. extern ObjectFormat braced_format; // Indicate the start of an objects. extern BeginObject begin_object; // Indicate the end of an object. extern EndObject end_object; // Indicate a new line. extern NewLine new_line; //********************************************************************** #endif