Skip to content
Snippets Groups Projects
Commit 99d8cf85 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

templated label data base class

parent 52920a76
No related branches found
No related tags found
No related merge requests found
......@@ -13,18 +13,33 @@
 
namespace alib {
 
template<typename... Types>
template<int ID>
class base_base {
public:
template<typename T>
int typeId(const T&) const {
return ID;
}
virtual int selfTypeId() const = 0;
};
template<int ID, typename... Types>
class base_helper;
 
template<typename T>
class base_helper<T> {
template<int ID, typename T>
class base_helper<ID, T> : public base_base<ID + 1> {
public:
virtual int typeId(const T &) const {
return ID;
}
virtual bool operator==(const T &) const {
return false;
}
 
virtual bool operator<(const T & other) const {
return typeid(this).before(typeid(&other));
return this->selfTypeId() < typeId(other);
}
 
virtual ~base_helper() noexcept {
......@@ -32,26 +47,32 @@ public:
}
};
 
template<typename T, typename... Types>
class base_helper<T, Types...> : public base_helper<Types...> {
template<int ID, typename T, typename... Types>
class base_helper<ID, T, Types...> : public base_helper<ID + 1, Types...> {
public:
using base_helper<Types...>::operator==;
using base_helper<Types...>::operator<;
using base_helper<ID + 1, Types...>::operator==;
using base_helper<ID + 1, Types...>::operator<;
using base_helper<ID + 1, Types...>::typeId;
virtual int typeId(const T &) const {
return ID;
}
 
virtual bool operator==(const T &) const {
return false;
}
 
virtual bool operator<(const T & other) const {
return typeid(this).before(typeid(&other));
return this->selfTypeId() < typeId(other);
}
};
 
template<typename T, typename... Types>
class base : public base_helper<Types...> {
class base : public base_helper<1, Types...> {
public:
using base_helper<Types...>::operator==;
using base_helper<Types...>::operator<;
using base_helper<1, Types...>::operator==;
using base_helper<1, Types...>::operator<;
using base_helper<1, Types...>::typeId;
 
virtual T* clone() const = 0;
 
......
......@@ -49,6 +49,10 @@ public:
virtual void operator>>(std::ostream&) const;
 
virtual operator std::string () const;
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace label */
......
......@@ -49,6 +49,10 @@ public:
virtual void operator>>(std::ostream&) const;
 
virtual operator std::string() const;
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace label */
......
/*
* LabelBase.cpp
*
* Created on: Apr 16, 2013
* Author: Jan Travnicek
*/
#include "LabelBase.h"
#include <typeinfo>
#include "StringLabel.h"
#include "IntegerLabel.h"
#include "CharacterLabel.h"
namespace label {
LabelBase::~LabelBase() {
}
bool LabelBase::operator<(const StringLabel& other) const {
return typeid(*this).before(typeid(other));
}
bool LabelBase::operator<(const IntegerLabel& other) const {
return typeid(*this).before(typeid(other));
}
bool LabelBase::operator<(const CharacterLabel& other) const {
return typeid(*this).before(typeid(other));
}
bool LabelBase::operator>=(const LabelBase& other) const {
return !(*this < other);
}
bool LabelBase::operator<=(const LabelBase& other) const {
return !(*this > other);
}
bool LabelBase::operator!=(const LabelBase& other) const {
return !(*this == other);
}
bool LabelBase::operator==(const StringLabel&) const {
return false;
}
bool LabelBase::operator==(const IntegerLabel&) const {
return false;
}
bool LabelBase::operator==(const CharacterLabel&) const {
return false;
}
std::ostream& operator<<(std::ostream& os, const LabelBase& label) {
label >> os;
return os;
}
} /* namespace label */
......@@ -9,7 +9,7 @@
#define LABEL_BASE_H_
 
#include "../std/visitor.hpp"
#include <iostream>
#include "../common/base.hpp"
 
namespace label {
 
......@@ -20,45 +20,7 @@ class CharacterLabel;
/**
* Abstract base class for all automata.
*/
class LabelBase : public std::elementBase<StringLabel, IntegerLabel, CharacterLabel> {
public:
virtual LabelBase* clone() const = 0;
virtual LabelBase* plunder() && = 0;
virtual ~LabelBase() noexcept;
virtual bool operator<(const LabelBase& other) const = 0;
virtual bool operator<(const StringLabel& other) const;
virtual bool operator<(const IntegerLabel& other) const;
virtual bool operator<(const CharacterLabel& other) const;
virtual bool operator>(const LabelBase& other) const = 0;
bool operator>=(const LabelBase& other) const;
bool operator<=(const LabelBase& other) const;
bool operator!=(const LabelBase& other) const;
virtual bool operator==(const LabelBase& other) const = 0;
virtual bool operator==(const StringLabel& other) const;
virtual bool operator==(const IntegerLabel& other) const;
virtual bool operator==(const CharacterLabel& other) const;
friend std::ostream& operator<<(std::ostream& os, const LabelBase& label);
virtual void operator>>(std::ostream&) const = 0;
virtual operator std::string() const = 0;
class LabelBase : public alib::base<LabelBase, StringLabel, IntegerLabel, CharacterLabel>, public std::elementBase<StringLabel, IntegerLabel, CharacterLabel> {
};
 
} /* namespace label */
......
......@@ -50,6 +50,10 @@ public:
virtual void operator>>(std::ostream&) const;
 
virtual operator std::string () const;
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace label */
......
......@@ -131,6 +131,10 @@ public:
virtual bool operator<(const FormalRegExp& other) const;
 
virtual operator std::string() const;
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace regexp */
......
......@@ -132,6 +132,9 @@ public:
 
virtual operator std::string() const;
 
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace regexp */
......
......@@ -65,6 +65,9 @@ public:
 
virtual operator std::string() const;
 
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace string */
......
......@@ -60,6 +60,9 @@ public:
 
static Epsilon EPSILON;
 
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace string */
......
......@@ -73,6 +73,9 @@ public:
 
virtual operator std::string() const;
 
virtual int selfTypeId() const {
return typeId(*this);
}
};
 
} /* namespace string */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment