private SourceCode GenerateReaderWriter(CodeContext context) { var code = new SourceCode(); code.Append("#pragma region /* reader & writer */"); code.NewLine(); foreach (var msg in SortedMessages) { var accessor = context.StartAccessor("msg"); code.Append("template <>"); code.Append("inline msg_writer& msg_writer::operator << (const {0}& {1})", msg.CanonicalName, accessor); code.BracketStart(); code.Append("(*this) << {0};", msg.MessageTypeName); msg.Fields.ForEach(field => code.Append(field.GenerateWriter(context))); code.Append("return (*this);"); code.BracketEnd(); code.NewLine(); code.Append("template <>"); code.Append("inline msg_reader& msg_reader::operator >> ({0}& {1})", msg.CanonicalName, accessor); code.BracketStart(); msg.Fields.ForEach(field => code.Append(field.GenerateReader(context))); code.Append("return (*this);"); code.BracketEnd(); code.NewLine(); context.EndAccessor(); } code.Append("#pragma endregion"); return(code); }
public SourceCode GenerateHandlerBody(CodeContext context) { var code = new SourceCode(); code.Append("inline void {0}::handle(msg_session_ref session, const type& msg)", CanonicalName); code.BracketStart(); code.Append("_ASSERT(_handler != NULL);"); code.Append("_handler(session, msg);"); Fields.ForEach(field => code.Append(field.GenerateHandler(context))); code.BracketEnd(); code.NewLine(); code.Append("inline void {0}::handle(msg_session_ref session, msg_reader reader)", CanonicalName); code.BracketStart(); var accessor = context.StartAccessor("msg"); { code.Append("{0} {1};", ClassName, accessor); code.Append("reader >> {0};", accessor); code.Append("handle(session, {0});", accessor); } context.EndAccessor(); code.BracketEnd(); code.NewLine(); code.Append("inline void {0}::broadcast(const type& msg)", CanonicalName); code.BracketStart(); context.StartAccessor("msg"); { code.Append(CodeUtil.ForEachCode("listener_list", "_listeners.", "iter", false)); code.IndentRight(); // not-recursive ref code.Append("(*iter)(msg);"); code.BracketEnd(); } context.EndAccessor(); code.BracketEnd(); return(code); }
public override SourceCode GenerateReader(CodeContext context) { var code = new SourceCode(); var countVar = Name + "_count"; code.Append("uint32_t {0};", countVar); code.Append("(*this) >> {0};", countVar); code.Append("for (uint32_t index = 0; index < {0}; ++index) {{", countVar); code.IndentRight(); var listAccessor = GetVarAccessor(context); var eachVariable = context.StartAccessor("each"); code.Append("{0} {1};", CanonicalName, eachVariable); Fields.ForEach(field => code.Append(field.GenerateReader(context))); code.Append("{0}push_back({1});", listAccessor, eachVariable); code.BracketEnd(); context.EndAccessor(); return code; }
public override SourceCode GenerateReader(CodeContext context) { var code = new SourceCode(); var countVar = Name + "_count"; code.Append("uint32_t {0};", countVar); code.Append("(*this) >> {0};", countVar); var listAccessor = GetVarAccessor(context); code.Append("for (uint32_t index = 0; index < {0}; ++index) {{", countVar); code.IndentRight(); var eachVariable = context.StartAccessor("each"); code.Append("{0} {1};", CanonicalTarget, eachVariable); code.Append("this->skip(static_cast<msg_size_t>(sizeof(msg_type_t)));"); code.Append("(*this) >> {0};", eachVariable); code.Append("{0}push_back({1});", listAccessor, eachVariable); context.EndAccessor(); code.BracketEnd(); return code; }
public override SourceCode GenerateReader(CodeContext context) { var code = new SourceCode(); var countVar = Name + "_count"; code.Append("uint32_t {0};", countVar); code.Append("(*this) >> {0};", countVar); code.Append("for (uint32_t index = 0; index < {0}; ++index) {{", countVar); code.IndentRight(); var listAccessor = GetVarAccessor(context); var eachVariable = context.StartAccessor("each"); code.Append("{0} {1};", CanonicalName, eachVariable); Fields.ForEach(field => code.Append(field.GenerateReader(context))); code.Append("{0}push_back({1});", listAccessor, eachVariable); code.BracketEnd(); context.EndAccessor(); return(code); }
public override SourceCode GenerateReader(CodeContext context) { var code = new SourceCode(); var countVar = Name + "_count"; code.Append("uint32_t {0};", countVar); code.Append("(*this) >> {0};", countVar); var listAccessor = GetVarAccessor(context); code.Append("for (uint32_t index = 0; index < {0}; ++index) {{", countVar); code.IndentRight(); var eachVariable = context.StartAccessor("each"); code.Append("{0} {1};", CanonicalTarget, eachVariable); code.Append("this->skip(static_cast<msg_size_t>(sizeof(msg_type_t)));"); code.Append("(*this) >> {0};", eachVariable); code.Append("{0}push_back({1});", listAccessor, eachVariable); context.EndAccessor(); code.BracketEnd(); return(code); }
public void Generate(string outputDirectory, string directionName) { var msgFileName = Path.Combine(outputDirectory, Name + "_msg.h"); var code = new SourceCode(); code.Append("#pragma once"); code.NewLine(); code.Append("#include <msg_def.h>"); code.Append("#include <msg_reader.h>"); code.Append("#include <msg_writer.h>"); code.Append("#include <msg_session_ref.h>"); code.NewLine(); code.Append("#pragma warning (disable: 4100)"); code.NewLine(); code.Append(GenerateMsgType()); code.Append("namespace msg {"); code.NewLine(); var context = new CodeContext(); foreach (var msg in SortedMessages) { code.BracketStart("struct {0}", msg.ClassName); code.Append("static const msg_type_t __type = msg_type::{0};", msg.Name); code.NewLine(); context.StartAccessor("msg"); msg.Fields.ForEach(field => code.Append(field.GenerateField(context))); code.NewLine(); code.Append(msg.GenerateArgumentedConstructor()); code.Append(msg.Fields.GenerateDefaultConstructor(msg.ClassName)); code.Append(msg.GenerateHandlerDeclaration()); context.EndAccessor(); code.BracketEnd(";"); code.NewLine(); } code.Append("}"); code.NewLine(); code.NewLine(); code.Append(GenerateReaderWriter(context)); code.NewLine(); code.NewLine(); code.Append("namespace msg {"); foreach (var msg in SortedMessages) { context.StartAccessor("msg"); code.Append(msg.GenerateHandlerBody(context)); context.EndAccessor(); code.NewLine(); } code.Append("}"); code.NewLine(); if (!String.IsNullOrWhiteSpace(directionName)) { code.Append(GenerateEntityBindCode(directionName)); code.NewLine(); } code.NewLine(); code.Append("#pragma warning (default: 4100)"); code.WriteToFile(msgFileName); }
private SourceCode GenerateReaderWriter(CodeContext context) { var code = new SourceCode(); code.Append("#pragma region /* reader & writer */"); code.NewLine(); foreach (var msg in SortedMessages) { var accessor = context.StartAccessor("msg"); code.Append("template <>"); code.Append("inline msg_writer& msg_writer::operator << (const {0}& {1})", msg.CanonicalName, accessor); code.BracketStart(); code.Append("(*this) << {0};", msg.MessageTypeName); msg.Fields.ForEach(field => code.Append(field.GenerateWriter(context))); code.Append("return (*this);"); code.BracketEnd(); code.NewLine(); code.Append("template <>"); code.Append("inline msg_reader& msg_reader::operator >> ({0}& {1})", msg.CanonicalName, accessor); code.BracketStart(); msg.Fields.ForEach(field => code.Append(field.GenerateReader(context))); code.Append("return (*this);"); code.BracketEnd(); code.NewLine(); context.EndAccessor(); } code.Append("#pragma endregion"); return code; }
public SourceCode GenerateHandlerBody(CodeContext context) { var code = new SourceCode(); code.Append("inline void {0}::handle(msg_session_ref session, const type& msg)", CanonicalName); code.BracketStart(); code.Append("_ASSERT(_handler != NULL);"); code.Append("_handler(session, msg);"); Fields.ForEach(field => code.Append(field.GenerateHandler(context))); code.BracketEnd(); code.NewLine(); code.Append("inline void {0}::handle(msg_session_ref session, msg_reader reader)", CanonicalName); code.BracketStart(); var accessor = context.StartAccessor("msg"); { code.Append("{0} {1};", ClassName, accessor); code.Append("reader >> {0};", accessor); code.Append("handle(session, {0});", accessor); } context.EndAccessor(); code.BracketEnd(); code.NewLine(); code.Append("inline void {0}::broadcast(const type& msg)", CanonicalName); code.BracketStart(); context.StartAccessor("msg"); { code.Append(CodeUtil.ForEachCode("listener_list", "_listeners.", "iter", false)); code.IndentRight(); // not-recursive ref code.Append("(*iter)(msg);"); code.BracketEnd(); } context.EndAccessor(); code.BracketEnd(); return code; }