Newer
Older
WickedDocs / Serializing / Visitor.h
#ifndef VISITOR_H
#define VISITOR_H


// With this defined, the virtual interfaces are
// declared and declared pure virtual so must be
// implemented. However the base class is here
// purely for checking the implementation of
// sub-classes meets the desired interface, the
// actual usage of sub-classes is using the CRTP
// and so we don't actually need virtual dispatch
// and can optionally not declare any virtual
// functions by not defining the below define and
// we won't pay any code to have a vtable and be
// doing virtual calls when we use this interface
#define VISITOR_INHERITED_INTERFACE_CHECK


class VisitorBase
{
public:
#ifdef VISITOR_INHERITED_INTERFACE_CHECK
  virtual ~VisitorBase() {}
  virtual void Enter(const char* name) = 0;
  virtual void Exit(const char* name) = 0;
#endif
  std::string quoted(const char* name)
  {
    return std::string("\"" + std::string(name) + "\"");
  }

  std::string indent(int spaces)
  {
    return std::string(spaces*2, ' ');
  }
};


class Serializer : public VisitorBase
{
public:
#ifdef VISITOR_INHERITED_INTERFACE_CHECK
  virtual const std::string& Output() = 0;
  virtual void Reset() = 0;
#endif
};


class Deserializer : public VisitorBase
{
public:
#ifdef VISITOR_INHERITED_INTERFACE_CHECK
  virtual void Reset(const std::string& a_input) = 0;
#endif
};


#endif // VISITOR_H