[cfe-dev] How much effort is implementing a constexpr object driven template system?

Péter Németh hun.nemethpeter at gmail.com
Mon Nov 4 17:15:00 PST 2013


Hi,


I started to think on a new kind of templating system in c++. Idea come
from the AngularJS.com templating framework.

Here is a link for a longer description:
https://github.com/hun-nemethpeter/cpp-reflector-mini/blob/master/Proposal.md

here is an example:

// template parttemplate<typename T>
[[meta::driver="EnumDriver(meta::class<T>) driver"]]// EnumDriver is a
constexpr class, that gets a compiler generated AST node// in
constructor parameter, through meta::class<T> in a type safe manner.//
(ex. enum declaration generates const EnumDecl*,// and a class
declaration generates const CXXRecordDecl*).// driver instance name
will be "driver", this is a normal C++ syntax// members and methods of
this object can be accessed with the following syntax://
$driver.member$ or $driver.method(param1, param2, ...)$void
Json::readFrom(boost::optional<$driver.enumName$>& obj, const
std::string& data){
  folly::fbstring jsonVal = folly::parseJson(data).asString();
  llvm::StringRef decoded(jsonVal.c_str(), jsonVal.length());
  obj = llvm::StringSwitch<$driver.enumName$>(decoded)
    // controlling directive meta::for, with the syntax of range base for
    // enumValueName will be a local variable of a CODT
    [[meta::for="(enumValueName:driver.enumValueNames)"]]
    .Case($enumValueName.asStr()$, $enumValueName$)
  ;}
// driver partclass EnumDriver{
  const EnumDecl* enumDecl;
  public:
    // used in [[std::driver="EnumDriver(meta::class<EVote>) driver"]]
    constexpr EnumDriver(const EnumDecl* enumDecl)
      : enumDecl(enumDecl)
    {
      std::string enumName = enumDecl->getNameAsString();
      for (auto it = enumDecl->enumerator_begin(); it !=
enumDecl->enumerator_end(); it++)
        enumNames.push_back((*it)->getName().data());
    }
    // used in [[meta::for(enumValueName:driver.enumValueNames)]]
    // meta::vector is a constexpr vector
    meta::vector<meta::id_string> enumValueNames;
    // used in $driver.enumName$
    // meta::id_string is a constexpr string that contains only valid
C++ identifier
    // it has an asStr() that gives back a stringified string, so
between "" signes
    meta::id_string enumName;};


Is there any chance that I'm able to implement it in some day? How much
effort implementing such a feature?


Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131105/c3ee8350/attachment.html>


More information about the cfe-dev mailing list