├── CMakeLists.txt ├── LICENSE ├── README.md ├── symbol ├── a.out ├── ast.hh ├── symbol.hh └── symbol_generator.cc └── test.cc /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(iod_symbol) 4 | 5 | SET(CMAKE_CXX_STANDARD 17) 6 | 7 | enable_testing() 8 | 9 | add_executable(iod_symbol_generator symbol/symbol_generator.cc) 10 | add_executable(test_symbol test.cc) 11 | add_test(test_symbol test_symbol) 12 | 13 | install(DIRECTORY symbol DESTINATION include/iod 14 | FILES_MATCHING PATTERN "*.hh") 15 | install(DIRECTORY symbol DESTINATION include/iod 16 | FILES_MATCHING PATTERN "*.hpp") 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Matthieu Garrigues 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | iod::symbol 3 | ================================= 4 | 5 | This library implements the basics of symbol based 6 | programming. Symbols are a new C++ paradigm that allow you to simply 7 | implement introspection, serialization, named parameters, and other 8 | things that are very hard / impossible to build in classic C++. 9 | 10 | A symbol is defined with a macro function : 11 | 12 | ```c++ 13 | IOD_SYMBOL(my_symbol) 14 | IOD_SYMBOL(my_symbol2) 15 | ``` 16 | 17 | And provides some operators : 18 | 19 | ```c++ 20 | // Named Variable declaration. 21 | auto v = iod::make_variable(s::my_symbol, 42); 22 | assert(v.my_symbol == 42); 23 | 24 | // Symbol introspection 25 | assert(!strcmp(iod::symbol_string(v), "my_symbol")); 26 | 27 | // Member access. 28 | assert(iod::symbol_member_access(v, s::my_symbol) == 42); 29 | 30 | // Method call 31 | struct { 32 | int my_symbol(int a) { return x + a; } 33 | int x; 34 | } obj{40}; 35 | 36 | assert(iod::symbol_method_call(obj, s::my_symbol, 2) == 42); 37 | 38 | // Introspection on objects. 39 | assert(iod::has_member(obj, s::my_symbol)) 40 | assert(!iod::has_member(obj, s::my_symbol2)) 41 | ``` 42 | -------------------------------------------------------------------------------- /symbol/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iodcpp/symbol/42f11f918cc12223dc06b7685522c6550953a5cc/symbol/a.out -------------------------------------------------------------------------------- /symbol/ast.hh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace iod { 7 | 8 | template 9 | struct Exp {}; 10 | 11 | template 12 | struct array_subscriptable; 13 | 14 | template 15 | struct callable; 16 | 17 | template 18 | struct assignable; 19 | 20 | template 21 | struct array_subscriptable; 22 | 23 | 24 | template 25 | struct function_call_exp : 26 | public array_subscriptable>, 27 | public callable>, 28 | public assignable>, 29 | public Exp> 30 | { 31 | using assignable>::operator=; 32 | 33 | function_call_exp(const M& m, A&&... a) 34 | : method(m), args(std::forward(a)...) {} 35 | 36 | M method; 37 | std::tuple args; 38 | }; 39 | 40 | template 41 | struct array_subscript_exp : 42 | public array_subscriptable>, 43 | public callable>, 44 | public assignable>, 45 | public Exp> 46 | { 47 | using assignable>::operator=; 48 | 49 | array_subscript_exp(const O& o, const M& m) : object(o), member(m) {} 50 | 51 | O object; 52 | M member; 53 | }; 54 | 55 | template 56 | struct assign_exp : public Exp> 57 | { 58 | typedef L left_t; 59 | typedef R right_t; 60 | 61 | template 62 | assign_exp(U&& l, V&& r) : left(std::forward(l)), right(std::forward(r)) {} 63 | //assign_exp(U&& l, V&& r) : left(l), right(r) {} 64 | // assign_exp(const L& l, R&& r) : left(l), right(std::forward(r)) {} 65 | 66 | L left; 67 | R right; 68 | }; 69 | 70 | template 71 | struct array_subscriptable 72 | { 73 | public: 74 | // Member accessor 75 | template 76 | constexpr auto operator[](S&& s) const 77 | { 78 | return array_subscript_exp(*static_cast(this), std::forward(s)); 79 | } 80 | 81 | }; 82 | 83 | template 84 | struct callable 85 | { 86 | public: 87 | // Direct call. 88 | template 89 | constexpr auto operator()(A&&... args) const 90 | { 91 | return function_call_exp(*static_cast(this), 92 | std::forward(args)...); 93 | } 94 | 95 | }; 96 | 97 | template 98 | struct assignable 99 | { 100 | public: 101 | 102 | template 103 | auto operator=(L&& l) const 104 | { 105 | return assign_exp(static_cast(*this), std::forward(l)); 106 | } 107 | 108 | template 109 | auto operator=(L&& l) 110 | { 111 | return assign_exp(static_cast(*this), std::forward(l)); 112 | } 113 | 114 | template 115 | auto operator=(const std::initializer_list& l) const 116 | { 117 | return assign_exp>(static_cast(*this), std::vector(l)); 118 | } 119 | 120 | }; 121 | 122 | #define iod_query_declare_binary_op(OP, NAME) \ 123 | template \ 124 | struct NAME##_exp : \ 125 | public assignable>, \ 126 | public Exp> \ 127 | { \ 128 | using assignable>::operator=; \ 129 | NAME##_exp() {} \ 130 | NAME##_exp(A&& a, B&& b) : lhs(std::forward(a)), rhs(std::forward(b)) {} \ 131 | typedef A lhs_type; \ 132 | typedef B rhs_type; \ 133 | lhs_type lhs; \ 134 | rhs_type rhs; \ 135 | }; \ 136 | template \ 137 | inline \ 138 | std::enable_if_t, A>::value or \ 139 | std::is_base_of, B>::value,\ 140 | NAME##_exp> \ 141 | operator OP (const A& b, const B& a) \ 142 | { return NAME##_exp, std::decay_t>{b, a}; } 143 | 144 | iod_query_declare_binary_op(+, plus); 145 | iod_query_declare_binary_op(-, minus); 146 | iod_query_declare_binary_op(*, mult); 147 | iod_query_declare_binary_op(/, div); 148 | iod_query_declare_binary_op(<<, shiftl); 149 | iod_query_declare_binary_op(>>, shiftr); 150 | iod_query_declare_binary_op(<, inf); 151 | iod_query_declare_binary_op(<=, inf_eq); 152 | iod_query_declare_binary_op(>, sup); 153 | iod_query_declare_binary_op(>=, sup_eq); 154 | iod_query_declare_binary_op(==, eq); 155 | iod_query_declare_binary_op(!=, neq); 156 | iod_query_declare_binary_op(&, logical_and); 157 | iod_query_declare_binary_op(^, logical_xor); 158 | iod_query_declare_binary_op(|, logical_or); 159 | iod_query_declare_binary_op(&&, and); 160 | iod_query_declare_binary_op(||, or); 161 | 162 | } 163 | -------------------------------------------------------------------------------- /symbol/symbol.hh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace iod { 7 | 8 | template 9 | class symbol : public assignable, 10 | public array_subscriptable, 11 | public callable, 12 | public Exp 13 | {}; 14 | } 15 | 16 | #define IOD_SYMBOL(NAME) \ 17 | namespace s { \ 18 | struct NAME##_t : iod::symbol { \ 19 | \ 20 | using assignable::operator=; \ 21 | \ 22 | inline constexpr bool operator==(NAME##_t) { return true; } \ 23 | template \ 24 | inline constexpr bool operator==(T) { return false; } \ 25 | \ 26 | template \ 27 | struct variable_t { \ 28 | typedef NAME##_t _iod_symbol_type; \ 29 | typedef V _iod_value_type; \ 30 | V NAME; \ 31 | }; \ 32 | \ 33 | template \ 34 | static inline decltype(auto) symbol_method_call(T&& o, A... args) { return o.NAME(args...); } \ 35 | template \ 36 | static inline auto& symbol_member_access(T&& o) { return o.NAME; } \ 37 | template \ 38 | static constexpr auto has_getter(int) -> decltype(std::declval().NAME(), std::true_type{}) { return {}; } \ 39 | template \ 40 | static constexpr auto has_getter(long) { return std::false_type{}; } \ 41 | template \ 42 | static constexpr auto has_member(int) -> decltype(std::declval().NAME, std::true_type{}) { return {}; } \ 43 | template \ 44 | static constexpr auto has_member(long) { return std::false_type{}; } \ 45 | \ 46 | static inline auto symbol_string() \ 47 | { \ 48 | return #NAME; \ 49 | } \ 50 | \ 51 | }; \ 52 | static constexpr NAME##_t NAME; \ 53 | } 54 | 55 | 56 | namespace iod { 57 | 58 | template 59 | inline decltype(auto) make_variable(S s, char const v[]) 60 | { 61 | typedef typename S::template variable_t ret; 62 | return ret{v}; 63 | } 64 | 65 | template 66 | inline decltype(auto) make_variable(S s, V v) 67 | { 68 | typedef typename S::template variable_t>> ret; 69 | return ret{v}; 70 | } 71 | 72 | template 73 | inline decltype(auto) make_variable_reference(K s, V&& v) 74 | { 75 | typedef typename K::template variable_t ret; 76 | return ret{v}; 77 | } 78 | 79 | template 80 | static inline decltype(auto) symbol_method_call(T&& o, S, A... args) 81 | { 82 | return S::symbol_method_call(o, std::forward(args)...); 83 | } 84 | 85 | template 86 | static inline decltype(auto) symbol_member_access(T&& o, S) 87 | { 88 | return S::symbol_member_access(o); 89 | } 90 | 91 | template 92 | constexpr auto has_member(T&& o, S) { return S::template has_member(0); } 93 | template 94 | constexpr auto has_member() { return S::template has_member(0); } 95 | 96 | template 97 | constexpr auto has_getter(T&& o, S) { return decltype(S::template has_getter(0)){}; } 98 | template 99 | constexpr auto has_getter() { return decltype(S::template has_getter(0)){}; } 100 | 101 | template 102 | struct CANNOT_FIND_REQUESTED_MEMBER_IN_TYPE {}; 103 | 104 | template 105 | decltype(auto) symbol_member_or_getter_access(T&&o, S) 106 | { 107 | if constexpr(has_getter()) { 108 | return symbol_method_call(o, S{}); 109 | } 110 | else if constexpr(has_member()) { 111 | return symbol_member_access(o, S{}); 112 | } 113 | else 114 | { 115 | return CANNOT_FIND_REQUESTED_MEMBER_IN_TYPE::error; 116 | } 117 | 118 | } 119 | 120 | template 121 | auto symbol_string(symbol v) 122 | { 123 | return S::symbol_string(); 124 | } 125 | 126 | template 127 | auto symbol_string(V v, typename V::_iod_symbol_type* = 0) 128 | { 129 | return V::_iod_symbol_type::symbol_string(); 130 | } 131 | } 132 | 133 | -------------------------------------------------------------------------------- /symbol/symbol_generator.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | std::string symbol_definition(std::string s); 9 | 10 | // Iod symbols generator. 11 | // 12 | // For each variable name starting with underscore, generates a symbol 13 | // definition. 14 | // 15 | int main(int argc, char* argv[]) 16 | { 17 | using namespace std; 18 | 19 | if (argc < 2) 20 | { 21 | cout << "Usage: " << argv[0] << " input_cpp_file1, ..., input_cpp_fileN" << endl; 22 | return 1; 23 | } 24 | 25 | std::set symbols; 26 | std::regex symbol_regex(".?s::([[:alnum:]_]+)"); 27 | std::set keywords = {"alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", "compl", "const", "constexpr", "const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq"}; 28 | 29 | auto parse_file = [&] (std::string filename) { 30 | 31 | ifstream f(filename); 32 | if (!f) 33 | { 34 | std::cerr << "Cannot open file " << argv[1] << " for reading." << std::endl; 35 | } 36 | 37 | std::string line; 38 | bool in_raw_string = false; 39 | while (!f.eof()) 40 | { 41 | getline(f, line); 42 | 43 | std::vector dbl_quotes_pos; 44 | bool escaped = false; 45 | for (int i = 0; i < line.size(); i++) 46 | { 47 | if (line[i] == '"' and !escaped) dbl_quotes_pos.push_back(i); 48 | else if (line[i] == '\\') escaped = !escaped; 49 | else escaped = false; 50 | } 51 | 52 | auto is_in_string = [&] (int p) { 53 | int i = 0; 54 | while (i < dbl_quotes_pos.size() and dbl_quotes_pos[i] <= p) i++; 55 | return i % 2; 56 | }; 57 | 58 | std::string::const_iterator start, end; 59 | start = line.begin(); 60 | end = line.end(); 61 | std::match_results what; 62 | std::regex_constants::match_flag_type flags = std::regex_constants::match_default; 63 | while(regex_search(start, end, what, symbol_regex, flags)) 64 | { 65 | std::string m = what[0]; 66 | std::string s = what[1]; 67 | 68 | bool is_type = s.size() >= 2 and s[s.size() - 2] == '_' and s[s.size() - 1] == 't'; 69 | 70 | if (!std::isalnum(m[0]) and !is_in_string(what.position()) and 71 | !is_type and keywords.find(s) == keywords.end()) 72 | symbols.insert(what[1]); 73 | start = what[0].second; 74 | } 75 | 76 | } 77 | }; 78 | 79 | for (int i = 1; i < argc; i++) 80 | parse_file(argv[i]); 81 | 82 | // std::ofstream os(argv[argc - 1]); 83 | // if (!os) 84 | // { 85 | // std::cerr << "Cannot open file " << argv[2] << " for writing." << std::endl; 86 | // return 2; 87 | // } 88 | auto& os = std::cout; 89 | os << "// Generated by the iod symbol generator." << endl; 90 | std::stringstream symbols_content; 91 | os << "#include " << endl; 92 | for (string s : symbols) 93 | { 94 | os << symbol_definition(s) << endl; 95 | } 96 | } 97 | 98 | std::string symbol_definition(std::string s) 99 | { 100 | std::string body; 101 | if (std::isdigit(s[0])) 102 | { 103 | body = R"cpp(#ifndef IOD_SYMBOL___S__ 104 | #define IOD_SYMBOL___S__ 105 | IOD_SYMBOL(__S__) 106 | #endif 107 | )cpp"; 108 | // Check the string is actually a number. 109 | for (int i = 0; i < s.size(); i++) 110 | if (!std::isdigit(s[i])) return ""; 111 | } 112 | else 113 | body = R"cpp(#ifndef IOD_SYMBOL___S__ 114 | #define IOD_SYMBOL___S__ 115 | IOD_SYMBOL(__S__) 116 | #endif 117 | )cpp"; 118 | 119 | std::regex s_regex("__S__"); 120 | body = std::regex_replace(body, s_regex, s); 121 | return body; 122 | } 123 | -------------------------------------------------------------------------------- /test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | IOD_SYMBOL(my_symbol) 6 | 7 | using namespace iod; 8 | 9 | int main() 10 | { 11 | 12 | 13 | // Simple variable. 14 | auto v = make_variable(s::my_symbol, 42); 15 | assert(v.my_symbol == 42); 16 | assert(!strcmp(symbol_string(v), "my_symbol")); 17 | 18 | // Member access. 19 | assert(symbol_member_access(v, s::my_symbol) == 42); 20 | 21 | 22 | // Ref 23 | int x = 23; 24 | auto v2 = make_variable_reference(s::my_symbol, x); 25 | x++; 26 | assert(v2.my_symbol == 24); 27 | 28 | // Not ref 29 | int y = 23; 30 | auto v3 = make_variable(s::my_symbol, y); 31 | y++; 32 | assert(v3.my_symbol == 23); 33 | 34 | 35 | // Method call 36 | struct { 37 | int my_symbol(int a) { return x + a; } 38 | int x; 39 | } obj{40}; 40 | 41 | assert(symbol_method_call(obj, s::my_symbol, 2) == 42); 42 | } 43 | --------------------------------------------------------------------------------