solver  1.0
Utils.h
Go to the documentation of this file.
1 /*
2  This file is part of the EnergyOptimizatorOfRoboticCells program.
3 
4  EnergyOptimizatorOfRoboticCells is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  EnergyOptimizatorOfRoboticCells is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with EnergyOptimizatorOfRoboticCells. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef HLIDAC_PES_UTILS_H
19 #define HLIDAC_PES_UTILS_H
20 
28 #include <map>
29 #include <string>
30 #include <typeinfo>
31 #include <type_traits>
32 #include <utility>
33 #include <vector>
34 #include <stdint.h>
35 #include "SolverConfig.h"
36 #include "Shared/Exceptions.h"
37 
61 template <class C>
62 typename C::mapped_type getValue(const C& m, const typename C::key_type& key, std::string calledFrom) {
63  typename C::const_iterator it = m.find(key);
64  if (it != m.cend()) {
65  return it->second;
66  } else {
67  std::string msg = "Invalid key!\n\n";
68  msg += "Called from:\n"+calledFrom;
69  throw SolverException(caller(), msg);
70  }
71 }
72 
73 template <class C>
74 void setValueHelper(C& m, const typename C::key_type& key, const typename C::mapped_type& value, std::string calledFrom) {
75  auto p = m.insert({key, value});
76  if (p.second == false) {
77  std::string msg = "Value with the same key already exists!\n\n";
78  msg += "Called from:\n"+calledFrom;
79  throw SolverException(caller(), msg);
80  }
81 }
82 
83 template <class C, class T = typename C::mapped_type>
84 void setValue(C& m, const typename C::key_type& key, const typename std::enable_if<std::is_pointer<T>::value, T>::type& value, std::string calledFrom) {
85  if (value == nullptr) {
86  std::string msg = "Attempt to store nullptr as a value!\n\n";
87  msg += "Called from:\n"+calledFrom;
88  throw SolverException(caller(), msg);
89  }
90 
91  setValueHelper(m, key, value, calledFrom);
92 }
93 
94 template <class C, class T = typename C::mapped_type>
95 void setValue(C& m, const typename C::key_type& key, const typename std::enable_if<!std::is_pointer<T>::value, T>::type& value, std::string calledFrom) {
96  setValueHelper(m, key, value, calledFrom);
97 }
98 // @}
99 
105 uint64_t pack(const uint32_t& v1, const uint32_t& v2);
111 std::pair<uint32_t, uint32_t> unpack(const uint64_t& v);
112 
125 template <class C>
126 extern uint64_t hashOW(const C& v);
127 
135 template <class C>
136 extern uintptr_t hashEW(const C& v);
137 
139 
145 bool fileExists(const std::string& pathToFile);
146 
147 #endif
A general exception of the program.
Definition: Exceptions.h:58
bool fileExists(const std::string &pathToFile)
It checks the existence of the file.
Definition: Utils.cpp:85
uint64_t hashOW(const C &v)
It calculates a hash of the container, the order of elements influences (Order Wise) the hash value...
uintptr_t hashEW(const C &v)
It calculates a hash of the container, the order of elements does not influence (Element Wise) the ha...
The file defines extended exceptions for the better error handling in the program.
std::pair< uint32_t, uint32_t > unpack(const uint64_t &v)
It unpacks two uint32_t numbers from uint64_t data type.
Definition: Utils.cpp:32
uint64_t pack(const uint32_t &v1, const uint32_t &v2)
It packs two uint32_t numbers to uint64_t data type.
Definition: Utils.cpp:26