libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNSavable.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 INSA Lyon, CoReNum, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNSavable.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNSAVABLE_HEADER
23 #define CRNSAVABLE_HEADER
24 
25 #include <CRNObject.h>
26 #include <map>
27 #include <CRNString.h>
28 #include <CRNIO/CRNPath.h>
29 #include <CRNData/CRNMapPtr.h>
30 #include <mutex>
31 
32 namespace crn
33 {
34  class Savable;
35  CRN_ALIAS_SMART_PTR(Savable)
36 }
37 namespace crn
38 {
39  /****************************************************************************/
58  class Savable : public Object
59  {
60  /**************************************************************************/
61  /* Complex object interface */
62  /**************************************************************************/
63  public:
65  Savable(const String &s = U"");
67  virtual ~Savable();
68 
70  Savable(const Savable&) = delete;
72  Savable& operator=(const Savable&) = delete;
73 
74  Savable(Savable&&) noexcept;
76 
78  const String& GetName() const { return name; }
80  void SetName(const String &s) { name = s; }
82  void SetName(String &&s) noexcept { name = std::move(s); }
83 
85  void SetUserData(const String &key, SObject value);
87  bool IsUserData(const String &key) const;
89  SObject GetUserData(const String &key);
91  String GetUserDataKey(const SObject &value) const;
93  const SObject GetUserData(const String &key) const;
95  void DeleteUserData(const String &key);
97  void ClearUserData();
98 
99  private:
100  String name;
101  std::unique_ptr<Map> user_data;
103  /**************************************************************************/
104  /* Savable protocol */
105  /**************************************************************************/
106  public:
108  Savable(const String &s, const Path &fname);
110  void Load(const Path &fname);
112  void Save(const Path &fname);
114  void Save();
116  const Path& GetFilename() const noexcept { return filename; }
117  protected:
119  void setFilename(const Path &fname) { filename = fname; }
121  void setFilename(Path &&fname) noexcept { filename = std::move(fname); }
122  std::unique_ptr<std::mutex> filelock;
123  private:
125  virtual Path completeFilename(const Path &fn) const;
127  virtual void load(const Path &fname);
129  virtual void save(const Path &fname);
130  Path filename;
132  public:
136  void serialize_internal_data(xml::Element &el) const;
137  };
138 }
139 
140 #endif
Savable & operator=(const Savable &)=delete
No assignment allowed.
XML element.
Definition: CRNXml.h:135
complex base abstract class
Definition: CRNSavable.h:58
bool IsUserData(const String &key) const
Tests if a user data key exists.
Definition: CRNSavable.cpp:61
std::unique_ptr< std::mutex > filelock
Definition: CRNSavable.h:122
const String & GetName() const
Returns the name of the object.
Definition: CRNSavable.h:78
void SetName(String &&s) noexcept
Sets the name of the object.
Definition: CRNSavable.h:82
void serialize_internal_data(xml::Element &el) const
Dumps some internal data to an XML element.
Definition: CRNSavable.cpp:316
void Load(const Path &fname)
Loads the object from an XML file (Safe)
Definition: CRNSavable.cpp:188
virtual ~Savable()
Destructor.
Definition: CRNSavable.cpp:47
A UTF32 character string class.
Definition: CRNString.h:61
void SetUserData(const String &key, SObject value)
Adds or replaces a user data.
Definition: CRNSavable.cpp:145
A convenience class for file paths.
Definition: CRNPath.h:39
String GetUserDataKey(const SObject &value) const
Gets a user data key by value.
Definition: CRNSavable.cpp:95
void ClearUserData()
Deletes all user data entries.
Definition: CRNSavable.cpp:155
void setFilename(Path &&fname) noexcept
Overwrites the filename.
Definition: CRNSavable.h:121
void Save()
Saves the object to an already set XML file.
Definition: CRNSavable.cpp:227
SObject GetUserData(const String &key)
Gets a user data by key.
Definition: CRNSavable.cpp:78
const Path & GetFilename() const noexcept
Returns the file name of the object.
Definition: CRNSavable.h:116
void DeleteUserData(const String &key)
Deletes a user data entry and frees the value.
Definition: CRNSavable.cpp:132
void setFilename(const Path &fname)
Overwrites the filename.
Definition: CRNSavable.h:119
void SetName(const String &s)
Sets the name of the object.
Definition: CRNSavable.h:80
CRN_ALIAS_SMART_PTR(ImageBW)
void deserialize_internal_data(xml::Element &el)
Initializes some internal data from an XML element.
Definition: CRNSavable.cpp:282
Savable(const String &s=U"")
Default constructor.
Definition: CRNSavable.cpp:36