mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-03-14 06:32:47 +01:00
* basic ctb config api for register and bit names * tests for define and definelist pass. yet to implement using them for reg, setbit, clearbit and getbit * improved autocomplete for getbit,setbit, clearbit * validate autocomplete * definelist has no put * updating help * converting char array+int in runtimeerror compiles but throws at runtime.Fixed.Tested for it. Also check if string or int before using getregisterdefinitonbyvalue to see if it threw to call the other function. because both of it can throw and we should differentiate the issues for both * removed std::vector<std::pair<string,int> to std::map<string, int> for defiitions list * Dev/define cmd tie bit to reg (#1328) * strong type * moved everythign to bit_utils class * pybindings * added tests for python * removed duplicates * removed bit names in reg * changed BitPosition to BitAddress * Using define reg/bit from python (#1344) * define_bit, define_addr in python. * setBit/clearBit takes int or addr * added example using bits * split define into 2 commands define_reg and define_bit, definelist into 2: definelist_reg and definelist_bit * allow string for register and bit names in c++ api * refactor from github comments * naming refactoring (getRegisterDefnition to retunr name and address specifically * added marker for 8 cmd tests connected to define, changed macro to static constexpr * changed bitPosition from int to uint32_t * got rid of setbitposition and setaddress, instead overloaded constructor to take in strings so that the conversion from string to bit address members, takes place within the class for easy maintainance in case type changes * Removing implicit conversions: RegisterAddresss and RegisterValue: Removed the implicit conversions. RegisterAddress: Changed member name from address_ to value_ and method as well to value(). RegisterValue: Also added | operator to be able to concatenate with uint32_t. Same in python bindings (but could not find the tests to modify * Allowed concatenation with other RegisterValue, made them all constexpr * fix a ctbConfig test * Maponstack works with integration tests, but need unit tests * tests on mapstack * fixed ctb tests and FixedString being initialized with gibberish * removing parsing from string inside the class RegisterAddress, BitAddress and RegisterValue * updated python bindings * fixed bit utils test * renaming getRegisterDefintiionAddress/Name=>getRegisterAddress/Name and similary for getBitDefinitionAddress/Name * updated python bindings * fix tests (format) * a few python tests added and python bindings corrected * replaceing str with __str__ for bit.cpp * repr reimplemented for bit.cpp * removed make with registerAddress etc * starting server for tests per session and nor module * killprocess throws if no process found-> github runs fails, changed to pkill and not throw * clean shm shouldnt raise, in ci binary not found * ignoring these tests for CI, which fail on CI because simulators are not generated in CI. This is in another PR, where it should work --------- Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com> Co-authored-by: froejdh_e <erik.frojdh@psi.ch>
219 lines
6.8 KiB
C++
219 lines
6.8 KiB
C++
#pragma once
|
|
|
|
// #include "sls/StaticVector.h"
|
|
#include "sls/string_utils.h"
|
|
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
namespace sls {
|
|
|
|
template <size_t N> struct FixedString {
|
|
char data_[N]{};
|
|
constexpr FixedString() noexcept { memset(data_, 0, N); }
|
|
FixedString(const char (&s)[N]) {
|
|
if (N <= 1) {
|
|
throw std::runtime_error("FixedString cannot be empty");
|
|
}
|
|
strcpy_checked<N>(data_, s);
|
|
}
|
|
FixedString(const std::string &s) {
|
|
if (s.size() <= 1) {
|
|
throw std::runtime_error("FixedString cannot be empty");
|
|
}
|
|
strcpy_checked<N>(data_, s);
|
|
}
|
|
bool operator==(const FixedString &other) const noexcept {
|
|
return std::strncmp(data_, other.data_, N) == 0;
|
|
}
|
|
bool operator<(const FixedString &other) const noexcept {
|
|
return std::strncmp(data_, other.data_, N) < 0;
|
|
}
|
|
std::string str() const { return std::string(data_); }
|
|
};
|
|
|
|
template <typename Key, typename Value, size_t Capacity, bool Unique_Values>
|
|
|
|
class MapOnStack {
|
|
// for shared memory use only trivially copyable types
|
|
static_assert(std::is_trivially_copyable_v<Key>);
|
|
static_assert(std::is_trivially_copyable_v<Value>);
|
|
static_assert(std::is_standard_layout_v<Key>);
|
|
static_assert(std::is_standard_layout_v<Value>);
|
|
static_assert(!std::is_pointer_v<Key>);
|
|
static_assert(!std::is_pointer_v<Value>);
|
|
|
|
public:
|
|
constexpr MapOnStack() noexcept = default;
|
|
|
|
constexpr size_t size() const noexcept { return current_size_; }
|
|
constexpr size_t capacity() const noexcept { return Capacity; }
|
|
constexpr bool empty() const noexcept { return current_size_ == 0; }
|
|
void clear() noexcept { current_size_ = 0; }
|
|
|
|
bool containsKey(const Key &key) const {
|
|
return lookupEntryByKey(key).has_value();
|
|
}
|
|
|
|
bool hasValue(const Value &value) const {
|
|
return lookupEntryByValue(value).has_value();
|
|
}
|
|
|
|
void addKeyOrSetValue(const Key &key, const Value &value) {
|
|
if (auto entry = findEntryByKey(key)) {
|
|
(*entry)->setValue(value);
|
|
return;
|
|
}
|
|
addEntry(key, value);
|
|
}
|
|
|
|
void addKey(const Key &key, const Value &value) { addEntry(key, value); }
|
|
|
|
void setValue(const Key &key, const Value &value) {
|
|
if (auto entry = findEntryByKey(key)) {
|
|
(*entry)->setValue(value);
|
|
return;
|
|
}
|
|
throw std::runtime_error("Key not found. Cannot set value.");
|
|
}
|
|
|
|
Value getValue(const Key &key) const {
|
|
auto val = lookupEntryByKey(key);
|
|
if (!val.has_value()) {
|
|
throw std::runtime_error("No entry found for key");
|
|
}
|
|
return val.value();
|
|
}
|
|
|
|
Key getKey(const Value &value) const {
|
|
auto key = lookupEntryByValue(value);
|
|
if (!key.has_value()) {
|
|
throw std::runtime_error("No entry found for value");
|
|
}
|
|
return key.value();
|
|
}
|
|
|
|
void setMap(const std::map<Key, Value> &list) {
|
|
if (list.size() >= Capacity) {
|
|
throw std::runtime_error("List size exceeds maximum Capacity");
|
|
}
|
|
clear();
|
|
for (const auto &[key, value] : list) {
|
|
addEntry(key, value);
|
|
}
|
|
}
|
|
|
|
std::map<Key, Value> getMap() const {
|
|
std::map<Key, Value> list;
|
|
for (size_t i = 0; i != current_size_; ++i)
|
|
list[data_[i].key()] = data_[i].value();
|
|
return list;
|
|
}
|
|
|
|
private:
|
|
struct Entry {
|
|
Key key_{};
|
|
Value value_{};
|
|
constexpr Entry() noexcept = default;
|
|
constexpr Entry(const Key &key, const Value &value)
|
|
: key_(key), value_(value) {}
|
|
constexpr Key key() const noexcept { return key_; }
|
|
constexpr Value value() const noexcept { return value_; }
|
|
constexpr void setValue(Value value) noexcept { value_ = value; }
|
|
};
|
|
|
|
Entry data_[Capacity];
|
|
size_t current_size_{0};
|
|
// StaticVector<Entry, Capacity> entries_;
|
|
|
|
std::optional<Entry *> findEntryByKey(const Key &key) {
|
|
auto it = std::find_if(data_, data_ + current_size_,
|
|
[&key](Entry &e) { return (e.key() == key); });
|
|
if (it != data_ + current_size_)
|
|
return it;
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<const Entry *> findEntryByKey(const Key &key) const {
|
|
auto it =
|
|
std::find_if(data_, data_ + current_size_,
|
|
[&key](const Entry &e) { return (e.key() == key); });
|
|
if (it != data_ + current_size_)
|
|
return it;
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<Entry *> findEntryByValue(Value value) {
|
|
if (!Unique_Values) {
|
|
throw std::runtime_error(
|
|
"Cannot lookup by value when unique values are not enforced.");
|
|
}
|
|
auto it =
|
|
std::find_if(data_, data_ + current_size_,
|
|
[&value](Entry &e) { return e.value() == value; });
|
|
if (it != data_ + current_size_)
|
|
return it;
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<const Entry *> findEntryByValue(Value value) const {
|
|
if (!Unique_Values) {
|
|
throw std::runtime_error(
|
|
"Cannot lookup by value when unique values are not enforced.");
|
|
}
|
|
auto it = std::find_if(
|
|
data_, data_ + current_size_,
|
|
[&value](const Entry &e) { return e.value() == value; });
|
|
if (it != data_ + current_size_)
|
|
return it;
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<const Value> lookupEntryByKey(const Key &key) const {
|
|
auto entry = findEntryByKey(key);
|
|
return (entry ? std::optional<Value>((*entry)->value()) : std::nullopt);
|
|
}
|
|
|
|
std::optional<Key> lookupEntryByValue(Value value) const {
|
|
auto entry = findEntryByValue(value);
|
|
return (entry ? std::optional<Key>((*entry)->key()) : std::nullopt);
|
|
}
|
|
|
|
void checkDuplicateKey(const Key &key) const {
|
|
if (auto entry = findEntryByKey(key)) {
|
|
throw std::runtime_error(
|
|
"Key already exists. Cannot have duplicate keys.");
|
|
}
|
|
}
|
|
|
|
void checkDuplicateValue(Value value) const {
|
|
if (Unique_Values) {
|
|
if (auto entry = findEntryByValue(value)) {
|
|
throw std::runtime_error(
|
|
"Value already assigned to another key '" +
|
|
(*entry)->key().str() + "'. Cannot assign it again.");
|
|
}
|
|
}
|
|
}
|
|
|
|
void checkSize() const {
|
|
if (current_size_ >= Capacity) {
|
|
throw std::runtime_error("Maximum capacity reached");
|
|
}
|
|
}
|
|
|
|
void addEntry(const Key &key, const Value &value) {
|
|
checkSize();
|
|
checkDuplicateKey(key);
|
|
checkDuplicateValue(value);
|
|
|
|
data_[current_size_] = Entry(key, value);
|
|
++(current_size_);
|
|
}
|
|
};
|
|
|
|
} // namespace sls
|