1
Fork 0
mirror of https://github.com/Jaxan/hybrid-ads.git synced 2025-04-27 15:07:45 +02:00

Adds phantom type (to be used for state/input/output)

This commit is contained in:
Joshua Moerman 2015-02-16 17:37:03 +01:00
parent 864d0818d1
commit b62d3a1a17
2 changed files with 74 additions and 0 deletions

44
lib/phantom.hpp Normal file
View file

@ -0,0 +1,44 @@
#pragma once
#include <boost/operators.hpp>
#include <iosfwd>
template <typename Base, typename T>
struct phantom : boost::operators<phantom<Base, T>>, boost::shiftable<phantom<Base, T>> {
phantom(Base y) : x(y) {}
explicit operator Base() const { return x; }
Base base() const { return x; }
Base x;
#define IMPL(op) \
phantom & operator op (phantom rh) { \
x op rh.x; \
return *this; \
}
IMPL(+=)
IMPL(-=)
IMPL(*=)
IMPL(/=)
IMPL(%=)
IMPL(|=)
IMPL(&=)
IMPL(^=)
IMPL(<<=)
IMPL(>>=)
#undef IMPL
phantom & operator++() { ++x; return *this; }
phantom & operator--() { --x; return *this; }
bool operator<(phantom rh) const { return x < rh.x; }
bool operator==(phantom rh) const { return x == rh.x; }
};
template <typename B, typename T>
std::ostream & operator<<(std::ostream & out, phantom<B, T> p){ return out << p.x; }
template <typename B, typename T>
std::istream & operator>>(std::istream & in, phantom<B, T> & p){ return in >> p.x; }

30
src/phantom_test.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <phantom.hpp>
#include <iostream>
template <typename T>
using phantom_int = phantom<int, T>;
struct state_tag;
using state = phantom_int<state_tag>;
struct input_tag;
using input = phantom_int<input_tag>;
int main(int argc, char *argv[]){
state x = 5;
x += 5;
input y = 9;
y -= 9;
y++;
std::cout << (x - 1) << std::endl;
std::cout << (y << 1) << std::endl;
// std::cout << x+y << std::endl; // does not compile
for(input i = 0; i < 10; ++i){
std::cout << i << ", ";
}
std::cout << std::endl;
}