Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:46
Show Gist options
  • Save rcook/9500975 to your computer and use it in GitHub Desktop.
Save rcook/9500975 to your computer and use it in GitHub Desktop.
Simple demonstration of traits in C++: see http://www.clopenset.com/content/traits-c
#pragma once
#include "common.hpp"
template<typename Traits>
class Algorithm
{
public:
typedef typename Traits::Module Module;
typedef typename Traits::Type Type;
typedef typename Traits::Member Member;
public:
void dumpModule(const Module &module) const
{
for (typename vector<Type>::const_iterator i = module.types().begin(); i != module.types().end(); ++i)
{
dumpType(*i);
}
}
private:
void dumpType(const Type &type) const
{
for (typename vector<Member>::const_iterator i = type.members().begin(); i != type.members().end(); ++i)
{
cout << "member: " << (*i).name() << endl;
}
}
};
#pragma once
#include <string>
#include <iostream>
#include <vector>
using namespace std;
The MIT License (MIT)
Copyright (c) 2014 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "common.hpp"
#include "algorithm.hpp"
#include "om1.hpp"
#include "om2.hpp"
struct OM1Traits
{
typedef OM1::Module Module;
typedef OM1::Type Type;
typedef OM1::Member Member;
};
struct OM2Traits
{
typedef OM2::Module Module;
typedef OM2::Type Type;
typedef OM2::Member Member;
};
int main()
{
Algorithm<OM1Traits> algorithm1;
algorithm1.dumpModule(OM1::Module("MODULE"));
Algorithm<OM2Traits> algorithm2;
algorithm2.dumpModule(OM2::Module("MODULE"));
return 0;
}
CPP := g++
TARGET := cpp-traits
all: $(TARGET)
$(TARGET): main.cpp *.hpp
$(CPP) -o $@ main.cpp
.PHONY: clean
clean:
rm -f $(TARGET)
#pragma once
#include "common.hpp"
namespace OM1
{
class Member
{
public:
Member(const string &name) : m_name(name) { }
public:
const string &name() const { return m_name; }
private:
string m_name;
};
class Type
{
public:
Type(const string &name) : m_name(name)
{
m_members.push_back(Member(m_name + "::MEMBER1"));
m_members.push_back(Member(m_name + "::MEMBER2"));
m_members.push_back(Member(m_name + "::MEMBER3"));
m_members.push_back(Member(m_name + "::MEMBER4"));
m_members.push_back(Member(m_name + "::MEMBER5"));
}
public:
const string &name() const { return m_name; }
const vector<Member> &members() const { return m_members; }
private:
string m_name;
vector<Member> m_members;
};
class Module
{
public:
Module(const string &name) : m_name(name)
{
m_types.push_back(Type("OM1::" + m_name + "::TYPE1"));
m_types.push_back(Type("OM1::" + m_name + "::TYPE2"));
m_types.push_back(Type("OM1::" + m_name + "::TYPE3"));
}
public:
const string &name() const { return m_name; }
const vector<Type> &types() const { return m_types; }
private:
const string m_name;
vector<Type> m_types;
};
}
#pragma once
#include "common.hpp"
namespace OM2
{
class Member
{
public:
Member(const string &name) : m_name(name) { }
public:
const string &name() const { return m_name; }
private:
string m_name;
};
class Type
{
public:
Type(const string &name) : m_name(name)
{
m_members.push_back(Member(m_name + "::MEMBER1"));
m_members.push_back(Member(m_name + "::MEMBER2"));
m_members.push_back(Member(m_name + "::MEMBER3"));
m_members.push_back(Member(m_name + "::MEMBER4"));
m_members.push_back(Member(m_name + "::MEMBER5"));
}
public:
const string &name() const { return m_name; }
const vector<Member> &members() const { return m_members; }
private:
string m_name;
vector<Member> m_members;
};
class Module
{
public:
Module(const string &name) : m_name(name)
{
m_types.push_back(Type("OM2::" + m_name + "::TYPE1"));
m_types.push_back(Type("OM2::" + m_name + "::TYPE2"));
m_types.push_back(Type("OM2::" + m_name + "::TYPE3"));
}
public:
const string &name() const { return m_name; }
const vector<Type> &types() const { return m_types; }
private:
const string m_name;
vector<Type> m_types;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment