Sleeping Wombat Common Library  0.50.0
swCommonLibrary
Serializer.h
1 #pragma once
2 
10 
11 #include <string>
12 #include <assert.h>
13 #include <memory>
14 
15 
16 
17 
18 struct SerializerImpl;
19 
22 enum class WritingMode : uint8
23 {
24  Sparing,
25  Readable
26 };
27 
32 {
33 private:
34  SerializerImpl* impl;
35  ISerializationContextPtr context;
36 protected:
37 public:
38  explicit ISerializer ( ISerializationContextPtr serContext );
39  virtual ~ISerializer ();
40 
41  void EnterObject ( const std::string& name );
42  void EnterArray ( const std::string& name );
43 
44  void Exit ();
45 
46  void SetAttribute ( const std::string& name, const std::string& value );
47  void SetAttribute ( const std::string& name, const char* value );
48  void SetAttribute ( const std::string& name, uint32 value );
49  void SetAttribute ( const std::string& name, uint64 value );
50  void SetAttribute ( const std::string& name, int32 value );
51  void SetAttribute ( const std::string& name, int64 value );
52  void SetAttribute ( const std::string& name, bool value );
53  void SetAttribute ( const std::string& name, double value );
54 
55 
56  bool SaveFile ( const std::string& fileName, WritingMode mode = WritingMode::Sparing );
57  std::string SaveString ( WritingMode mode = WritingMode::Sparing );
58 
59 
60 public:
61 
67  template< typename ContextType >
68  inline ContextType* GetContext ()
69  {
70  assert( context != nullptr );
71 
72  // Sprawdzanie dynamicznego typu tylko, jeżeli włączone jest RTTI.
73 #ifdef _CPPRTTI
74  assert( typeid( *context ) == typeid( ContextType ) );
75 #endif
76 
77  return static_cast< ContextType* >( context.get() );
78  }
79 };
80 
81 
WritingMode
Writing to file mode.
Definition: Serializer.h:22
void EnterArray(const std::string &name)
Tworzy tablicę o podanej nazwie.
Definition: Serializer.cpp:168
virtual ~ISerializer()
Destruktor.
Definition: Serializer.cpp:74
Serializers interface.
Definition: Serializer.h:31
bool SaveFile(const std::string &fileName, WritingMode mode=WritingMode::Sparing)
Zapisuje zserializowane dane do pliku.
Definition: Serializer.cpp:107
Plik zawiera definicje podstawowych typów zmiennych.
ContextType * GetContext()
Returns serialization context.
Definition: Serializer.h:68
std::string SaveString(WritingMode mode=WritingMode::Sparing)
Zwraca stringa zawierającego zserializowanego Jsona.
Definition: Serializer.cpp:83
void SetAttribute(const std::string &name, const std::string &value)
Ustawia parę ( nazwa, wartość ) w aktualnym obiekcie.
Definition: Serializer.cpp:187
void Exit()
Koniec tworzenia obiektu. EnterObject i ExitObject powinny być stosowane parami.
Definition: Serializer.cpp:151
void EnterObject(const std::string &name)
Tworzy obiekt o podanej nazwie.
Definition: Serializer.cpp:134
Definition: Serializer.cpp:25
Mode without spaces and tabulations.
Mode for reading by people.
ISerializer(ISerializationContextPtr serContext)
Konstruktor.
Definition: Serializer.cpp:65