18 #ifndef FOEDUS_EXTERNALIZE_EXTERNALIZABLE_HPP_
19 #define FOEDUS_EXTERNALIZE_EXTERNALIZABLE_HPP_
46 namespace externalize {
124 tinyxml2::XMLElement** out);
129 template <
typename T>
131 const std::string& comment, T value);
133 template <u
int MAXLEN,
typename CHAR>
135 tinyxml2::XMLElement* parent,
136 const std::string& tag,
137 const std::string& comment,
139 return add_element< std::string >(parent, tag, comment, value.
str());
143 template <
typename T>
145 const std::string& comment,
const std::vector< T >& value) {
146 if (comment.size() > 0) {
151 for (std::size_t i = 0; i < value.size(); ++i) {
158 template <
typename ENUM>
160 const std::string& comment, ENUM value) {
161 return add_element(parent, tag, comment, static_cast<int64_t>(value));
171 template <
typename T>
173 T* out,
bool optional =
false, T value = 0);
176 std::string* out,
bool optional =
false,
const char* value =
"");
177 template <u
int MAXLEN,
typename CHAR>
179 tinyxml2::XMLElement* parent,
180 const std::string& tag,
182 bool optional =
false,
186 out->
assign(tmp_out.data(), tmp_out.size());
191 template <
typename ENUM>
193 ENUM* out,
bool optional =
false, ENUM default_value = static_cast<ENUM>(0)) {
197 CHECK_ERROR(get_element<int64_t>(parent, tag, &tmp, optional, default_value));
198 if (static_cast<int64_t>(static_cast<ENUM>(tmp)) != tmp) {
201 *out =
static_cast<ENUM
>(tmp);
209 template <
typename T>
211 std::vector< T >* out,
bool optional =
false);
222 #define EX_QUOTE(str) #str
223 #define EX_EXPAND(str) EX_QUOTE(str)
234 #define EXTERNALIZE_SAVE_ELEMENT(element, attribute, comment) \
235 CHECK_ERROR(add_element(element, EX_EXPAND(attribute), comment, attribute))
243 #define EXTERNALIZE_SAVE_ENUM_ELEMENT(element, attribute, comment) \
244 CHECK_ERROR(add_enum_element(element, EX_EXPAND(attribute), comment, attribute))
254 #define EXTERNALIZE_LOAD_ELEMENT(element, attribute) \
255 CHECK_ERROR(get_element(element, EX_EXPAND(attribute), & attribute))
264 #define EXTERNALIZE_LOAD_ELEMENT_OPTIONAL(element, attribute, default_value) \
265 CHECK_ERROR(get_element(element, EX_EXPAND(attribute), & attribute, true, default_value))
274 #define EXTERNALIZE_LOAD_ENUM_ELEMENT(element, attribute) \
275 CHECK_ERROR(get_enum_element(element, EX_EXPAND(attribute), & attribute))
283 #define EXTERNALIZE_LOAD_ENUM_ELEMENT_OPTIONAL(element, attribute, default_value) \
284 CHECK_ERROR(get_enum_element(element, EX_EXPAND(attribute), & attribute, true, default_value))
294 #define EXTERNALIZABLE(clazz) \
295 ErrorStack load(tinyxml2::XMLElement* element) CXX11_OVERRIDE;\
296 ErrorStack save(tinyxml2::XMLElement* element) const CXX11_OVERRIDE;\
297 const char* get_tag_name() const CXX11_OVERRIDE { return EX_EXPAND(clazz); }\
298 void assign(const foedus::externalize::Externalizable *other) CXX11_OVERRIDE {\
299 *this = *dynamic_cast< const clazz * >(other);\
301 friend std::ostream& operator<<(std::ostream& o, const clazz & v) {\
302 v.save_to_stream(&o);\
306 #endif // FOEDUS_EXTERNALIZE_EXTERNALIZABLE_HPP_
ErrorStack load_from_string(const std::string &xml)
Load the content of this object from the given XML string.
void save_to_stream(std::ostream *ptr) const
Invokes save() and directs the resulting XML text to the given stream.
static ErrorStack get_child_element(tinyxml2::XMLElement *parent, const std::string &tag, Externalizable *child, bool optional=false)
child Externalizable version
Forward declarations of classes in filesystem package.
Represents an object that can be written to and read from files/bytes in XML format.
static ErrorStack add_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, const std::vector< T > &value)
vector version
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
static ErrorStack add_child_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, const Externalizable &child)
child Externalizable version
static ErrorStack append_comment(tinyxml2::XMLElement *parent, const std::string &comment)
Brings error stacktrace information as return value of functions.
ErrorStack save_to_file(const fs::Path &path) const
Atomically and durably writes out this object to the specified XML file.
static ErrorStack insert_comment(tinyxml2::XMLElement *element, const std::string &comment)
static ErrorStack add_enum_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, ENUM value)
enum version
static ErrorStack add_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, const assorted::FixedString< MAXLEN, CHAR > &value)
Analogue of boost::filesystem::path.
static ErrorStack create_element(tinyxml2::XMLElement *parent, const std::string &name, tinyxml2::XMLElement **out)
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
std::basic_string< CHAR > str() const
Convert to a std::string object.
virtual const char * get_tag_name() const =0
Returns an XML tag name for this object as a root element.
virtual void assign(const foedus::externalize::Externalizable *other)=0
Polymorphic assign operator.
virtual ErrorStack save(tinyxml2::XMLElement *element) const =0
Writes the content of this object to the given XML element.
#define CHECK_ERROR(x)
This macro calls x and checks its returned value.
ErrorStack load_from_file(const fs::Path &path)
Load the content of this object from the specified XML file.
const ErrorStack kRetOk
Normal return value for no-error case.
static ErrorStack get_element(tinyxml2::XMLElement *parent, const std::string &tag, T *out, bool optional=false, T value=0)
Only declaration in header.
0x0402 : "CONFIG : Configuration value out of range." .
#define ERROR_STACK_MSG(e, m)
Overload of ERROR_STACK(e) to receive a custom error message.
An embedded string object of fixed max-length, which uses no external memory.
std::string get_pretty_type_name()
Returns the name of the C++ type as readable as possible.
static ErrorStack get_enum_element(tinyxml2::XMLElement *parent, const std::string &tag, ENUM *out, bool optional=false, ENUM default_value=static_cast< ENUM >(0))
enum version
virtual ErrorStack load(tinyxml2::XMLElement *element)=0
Reads the content of this object from the given XML element.
static ErrorStack add_element(tinyxml2::XMLElement *parent, const std::string &tag, const std::string &comment, T value)
Only declaration in header.
static ErrorStack get_element(tinyxml2::XMLElement *parent, const std::string &tag, assorted::FixedString< MAXLEN, CHAR > *out, bool optional=false, const assorted::FixedString< MAXLEN, CHAR > &value=assorted::FixedString< MAXLEN, CHAR >())