-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ppcpp | ||
|
||
import ( | ||
"github.com/davyxu/protoplus/gen" | ||
"github.com/davyxu/protoplus/model" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var UsefulFunc = template.FuncMap{} | ||
|
||
func ChangeExtension(filename, newExt string) string { | ||
|
||
file := filepath.Base(filename) | ||
|
||
return strings.TrimSuffix(file, path.Ext(file)) + newExt | ||
} | ||
|
||
func SourceList(ctx *gen.Context) (ret []string) { | ||
|
||
rootDS := &model.PBDescriptorSet{DescriptorSet: *ctx.DescriptorSet} | ||
for _, protoFile := range rootDS.SourceList() { | ||
ret = append(ret, ChangeExtension(protoFile, ".pb.h")) | ||
} | ||
|
||
return | ||
} | ||
|
||
func init() { | ||
UsefulFunc["SourceList"] = SourceList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ppcpp | ||
|
||
import ( | ||
"fmt" | ||
"github.com/davyxu/protoplus/codegen" | ||
"github.com/davyxu/protoplus/gen" | ||
) | ||
|
||
func GenCppReg(ctx *gen.Context) error { | ||
|
||
codeGen := codegen.NewCodeGen("cppreg"). | ||
RegisterTemplateFunc(codegen.UsefulFunc). | ||
RegisterTemplateFunc(UsefulFunc). | ||
ParseTemplate(RegTemplateText, ctx) | ||
|
||
if codeGen.Error() != nil { | ||
fmt.Println(codeGen.Code()) | ||
return codeGen.Error() | ||
} | ||
|
||
return codeGen.WriteOutputFile(ctx.OutputFileName).Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ppcpp | ||
|
||
const RegTemplateText = `// Generated by github.com/davyxu/protoplus | ||
#pragma once | ||
#include "MessageRegistry.h" | ||
{{range SourceList $}} | ||
#include "{{.}}"{{end}} | ||
{{range .Structs}} | ||
class {{.Name}}Meta : public MessageMeta | ||
{ | ||
public: | ||
virtual const char* GetMessageName( ) const override{ return "{{$.PackageName}}.{{.Name}}"; } | ||
virtual int32 GetMessageId( ) const override{ return {{StructMsgID .}}; } | ||
virtual void* NewMessage() override { return {{$.PackageName}}::{{.Name}}::default_instance().New(); } | ||
}; {{end}} | ||
void StaticRegisterMeta( ) | ||
{ {{range .Structs}} | ||
MessageRegistry::Register(new {{.Name}}Meta); {{end}} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package model | ||
|
||
import "sort" | ||
|
||
type PBDescriptorSet struct { | ||
DescriptorSet | ||
|
||
// pb生成文件依赖时, 使用以下字段 | ||
DependentSource []string // 按文件管理的描述符取出时, 这个字段有效. 本文件依赖的其他source的symbiol | ||
SourceName string // 按文件管理的描述符取出时, 这个字段有效. 表示本DescriptorSet的文件名 | ||
dsBySource map[string]*PBDescriptorSet // 按文件名管理的描述符集合 | ||
} | ||
|
||
func (self *PBDescriptorSet) addDependentSource(name string) { | ||
|
||
if self.SourceName == name { | ||
return | ||
} | ||
|
||
for _, n := range self.DependentSource { | ||
if n == name { | ||
return | ||
} | ||
} | ||
|
||
self.DependentSource = append(self.DependentSource, name) | ||
} | ||
|
||
func (self *PBDescriptorSet) SourceList() (ret []string) { | ||
for sourceName := range self.DescriptorSetBySource() { | ||
ret = append(ret, sourceName) | ||
} | ||
|
||
sort.Strings(ret) | ||
return | ||
} | ||
|
||
func (self *PBDescriptorSet) DescriptorSetBySource() map[string]*PBDescriptorSet { | ||
if self.dsBySource != nil { | ||
return self.dsBySource | ||
} | ||
|
||
self.dsBySource = map[string]*PBDescriptorSet{} | ||
|
||
for _, obj := range self.Objects { | ||
ds := self.dsBySource[obj.SrcName] | ||
if ds == nil { | ||
ds = &PBDescriptorSet{} | ||
|
||
ds.PackageName = self.PackageName | ||
ds.Codec = self.Codec | ||
ds.SourceName = obj.SrcName | ||
|
||
self.dsBySource[obj.SrcName] = ds | ||
} | ||
|
||
ds.AddObject(obj) | ||
} | ||
|
||
for _, file := range self.dsBySource { | ||
for _, st := range file.Structs() { | ||
|
||
for _, fd := range st.Fields { | ||
|
||
switch fd.Kind { | ||
case Kind_Struct, Kind_Enum: | ||
refTarget := self.ObjectByName(fd.Type) | ||
if refTarget != nil { | ||
file.addDependentSource(refTarget.SrcName) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return self.dsBySource | ||
} |