bpp-seq3  3.0.0
MappedNamedContainer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_CONTAINER_MAPPEDNAMEDCONTAINER_H
6 #define BPP_SEQ_CONTAINER_MAPPEDNAMEDCONTAINER_H
7 
8 #include "NamedContainer.h"
9 
10 // From the STL:
11 
12 #include <map>
13 #include <memory>
14 #include <string>
15 
16 namespace bpp
17 {
23 template<class T>
25  public virtual NamedContainerInterface<T>
26 {
27 private:
28  std::map<std::string, std::shared_ptr<T>> mObjects_;
29 
30 public:
32  mObjects_()
33  {}
34 
35  MappedNamedContainer(const std::map<std::string, std::shared_ptr<T>>& ms) :
36  mObjects_(ms)
37  {}
38 
40  mObjects_(msc.mObjects_)
41  {}
42 
43  virtual ~MappedNamedContainer() {}
44 
50  MappedNamedContainer* clone() const override { return new MappedNamedContainer(*this); }
55  {
56  mObjects_ = msc.mObjects_;
57  return *this;
58  }
59 
60 public:
61  const std::shared_ptr<T> getObject(const std::string& name) const override
62  {
63  const auto it = mObjects_.find(name);
64  if (it == mObjects_.end())
65  throw Exception("MappedNamedContainer::getObject : unknown name " + name);
66 
67  return it->second;
68  }
69 
70  std::shared_ptr<T> getObject(const std::string& name) override
71  {
72  auto it = mObjects_.find(name);
73  if (it == mObjects_.end())
74  throw Exception("MappedNamedContainer::getObject : unknown name " + name);
75 
76  return it->second;
77  }
78 
79  const T& object(const std::string& name) const override
80  {
81  const auto it = mObjects_.find(name);
82  if (it == mObjects_.end())
83  throw Exception("MappedNamedContainer::object : unknown name " + name);
84 
85  return *(it->second);
86  }
87 
88  T& object(const std::string& name) override
89  {
90  auto it = mObjects_.find(name);
91  if (it == mObjects_.end())
92  throw Exception("MappedNamedContainer::object : unknown name " + name);
93 
94  return *(it->second);
95  }
96 
97  bool hasObject(const std::string& name) const override
98  {
99  return mObjects_.find(name) != mObjects_.end();
100  }
101 
102 
110  void addObject(std::shared_ptr<T> newObject, const std::string& name, bool checkName = false)
111  {
112  if (checkName && hasObject(name))
113  throw Exception("MappedNamedContainer::addObject : Object's name already exists in container : " + name);
114  std::string nn = name;
115  mObjects_[nn] = newObject;
116  }
117 
123  void deleteObject(const std::string& name) override
124  {
125  if (!hasObject(name))
126  throw Exception("MappedNamedContainer::deleteObject : Object's name does not exist in container : " + name);
127 
128  mObjects_.erase(name);
129  }
130 
137  std::shared_ptr<T> removeObject(const std::string& name) override
138  {
139  if (!hasObject(name))
140  throw Exception("MappedNamedContainer::removeObject : Object's name does not exist in container : " + name);
141 
142  std::shared_ptr<T> obj = mObjects_[name];
143  mObjects_.erase(name);
144  return obj;
145  }
146 
150  virtual std::vector<std::string> getObjectNames() const override
151  {
152  std::vector<std::string> vNames;
153 
154  for (auto it : mObjects_)
155  {
156  vNames.push_back(it.first);
157  }
158 
159  return vNames;
160  }
161 
168  void changeName(const std::string& okey, const std::string& nkey)
169  {
170  if (okey == nkey)
171  return;
172 
173  if (!hasObject(okey))
174  throw Exception("MappedNamedContainer::changeName : Object's name does not exist in container : " + okey);
175 
176  if (hasObject(nkey))
177  throw Exception("MappedNamedContainer::changeName : Object's new name already exists in container : " + nkey);
178 
179  std::shared_ptr<T> obj = mObjects_[okey];
180  mObjects_.erase(okey);
181  mObjects_[nkey] = obj;
182  }
183 
184  size_t getSize() const override { return mObjects_.size(); }
185 
186  void clear() override
187  {
188  mObjects_.clear();
189  }
190 
195  bool isAvailableName(std::string objectName) const
196  {
197  return hasObject(objectName) && (getObject(objectName) == nullptr || getObject(objectName)->size() == 0);
198  }
199 
200  void addObject_(std::shared_ptr<T> newObject, const std::string& name, bool checkName = false) const
201  {
202  if (checkName && hasObject(name))
203  throw Exception("MappedNamedContainer::addObject : Object's name already exists in container : " + name);
204 
205  std::string nn = name;
206  const_cast<std::map<std::string, std::shared_ptr<T>>&>(mObjects_)[nn] = newObject;
207  }
208 
212  virtual void nullify()
213  {
214  for (auto& it : mObjects_)
215  {
216  it.second = nullptr;
217  }
218  }
219 };
220 } // end of namespace bpp.
221 #endif // BPP_SEQ_CONTAINER_MAPPEDNAMEDCONTAINER_H
MappedNamedContainer class.
void addObject(std::shared_ptr< T > newObject, const std::string &name, bool checkName=false)
Set an object.
void changeName(const std::string &okey, const std::string &nkey)
change the key of an object.
virtual std::vector< std::string > getObjectNames() const override
std::shared_ptr< T > removeObject(const std::string &name) override
Remove and returns an object.
MappedNamedContainer(const MappedNamedContainer &msc)
T & object(const std::string &name) override
void clear() override
Delete all objects in the container.
const T & object(const std::string &name) const override
virtual void nullify()
Nullify all elements.
void deleteObject(const std::string &name) override
Remove an object.
std::shared_ptr< T > getObject(const std::string &name) override
bool hasObject(const std::string &name) const override
Check if a object with a given name is present in the container.
std::map< std::string, std::shared_ptr< T > > mObjects_
MappedNamedContainer * clone() const override
bool isAvailableName(std::string objectName) const
const std::shared_ptr< T > getObject(const std::string &name) const override
Retrieve an object from the container.
size_t getSize() const override
Get the number of objects in the container.
MappedNamedContainer & operator=(const MappedNamedContainer &msc)
void addObject_(std::shared_ptr< T > newObject, const std::string &name, bool checkName=false) const
MappedNamedContainer(const std::map< std::string, std::shared_ptr< T >> &ms)
A Container template for objects that are accessible through names.
This alphabet is used to deal NumericAlphabet.