NuTo
Numerics Tool
DofContainer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <vector>
6 
7 namespace NuTo
8 {
9 template <typename T>
11 {
12 public:
13  virtual ~DofContainer() = default;
14 
18  // an existing value
19  // an newly default constructed value
21  T& operator[](DofType dofType)
22  {
23  return mData[dofType];
24  }
25 
30  // require T to be default constructable.
31  T& At(DofType dofType)
32  {
33  return mData.at(dofType);
34  }
35 
39  const T& operator[](DofType dofType) const
40  {
41  return mData.at(dofType);
42  }
43 
47  void Insert(DofType dofType, T t)
48  {
49  auto it = mData.emplace(dofType, t); // it = pair<iterator, bool>
50  if (not it.second)
51  throw Exception(__PRETTY_FUNCTION__,
52  "Insert failed. Container already contains an entry for " + dofType.GetName() + ".");
53  }
54 
55  bool Has(DofType dofType) const
56  {
57  return mData.find(dofType) != mData.end();
58  }
59 
60  auto begin() const
61  {
62  return mData.begin();
63  }
64 
65  auto end() const
66  {
67  return mData.end();
68  }
69 
70 protected:
71  std::map<DofType, T, CompareDofType> mData;
72 };
73 } /* NuTo */
void Insert(DofType dofType, T t)
copies a t into the container, throws, if there already is an entry at dofType
Definition: DofContainer.h:47
Base class for all exceptions thrown in NuTo.
Definition: Exception.h:9
auto begin() const
Definition: DofContainer.h:60
T & operator[](DofType dofType)
nonconst access, similar to map::operator[]()
Definition: DofContainer.h:21
virtual ~DofContainer()=default
T & At(DofType dofType)
nonconst access, similar to map::at()
Definition: DofContainer.h:31
bool Has(DofType dofType) const
Definition: DofContainer.h:55
Definition: DofType.h:8
auto end() const
Definition: DofContainer.h:65
const std::string & GetName() const
Definition: DofType.h:21
std::map< DofType, T, CompareDofType > mData
Definition: DofContainer.h:71
Definition: Exception.h:6
const T & operator[](DofType dofType) const
const access
Definition: DofContainer.h:39
Definition: DofContainer.h:10