How to define a nested map in a header file for use in .cpp - c++98

I am trying to define a nested map variable in a header file to use for key value lookup (or key, key value lookup since it is nested).
Apologies for being very new to C++ in general, let alone C++98.
I have intermediate JavaScript experience, which might explain difficulties/habits.
I'm trying to insert spoken language translations into a UI using a nested map, something with a structure similar to this:
phrases["english"]["hello"] = "hi";
phrases["spanish"]["hello"] = "hola";
which will allow me to use phrases[selectedLanguage]["hello"] which will return(?) "hi" or "hola" depending on what selectedLanguage is set to.
This is so that a user can switch between languages while also allowing me to just change one translations.h file if/when needed.
I have a working version of the code which puts the map definitions within the .cpp code but I'd like to create something like a header file which defines my 'phrases' map variable so that I can separate language translations from the rest of the .cpp code.
My current working code looks like this:
UI.cpp:
void CScnMgr::InitScreens(){
// selectedLanguage is defined
string selectedLanguage = "spanish";
//phrases map is defined
map <string, map <string, string> > phrases;
phrases["english"]["hello"] = "hi";
phrases["spanish"]["hello"] = "hola";
// then later when i need to use either translation...
phrases[selectedLanguage]["hello"];
}
This works, but I assume this is bad practice because it is creating this object every time the screens are initialized and for reasons I'm unfamiliar with. But I want to put my phrases map into a header file.
This is giving me errors:
translations.h:
#include <string>
#include <map>
int main(){
map <string, map <string, string> > newPhrases;
map <string, string> spanish;
map <string, string> english;
spanish["hello"] = "hola";
english["hello"] = "hi";
newPhrases["spanish"] = spanish;
newPhrases["english"] = english;
return 0;
}
UI.cpp:
#include "translations.h"
void CScnMgr::InitScreens(){
int extern newPhrases;
// further down where I need to display to the UI...
newPhrases[selectedLanguage]["hi"]
}
Errors:
UI.cpp: error: no match for 'operator[]' in 'newPhrases[selectedLanguage]'
I certainly don't understand why putting "int" in 'int extern newPhrases' passes compiling, but that's why it is there, I gave it the type of the main() return. I don't feel very comfortable doing that.
So I've defined selectedLanguage as "english" so I would expect C++ to handle that as newPhrases["english"], but it seems like newPhrases isn't defined as I expect it to be after importing it from translations.h
I'd appreciate a way to make this code work but I'd also appreciate reasons why this is the wrong way to go about this. Thanks in advance!

Let's try this step by step:
JavaScript to C++
That's quite a brave task :)
I guess you chose the hard path going that way. It would have been easier the other way round. Well... it is what it is. Just let me say: C++ feels very different than JavaScript. I strongly recommend to do one of the myriads of tutorials and/or read a good book about it. There are plenty!
File Structure
Generally speaking, there should never be definitions in header files, only declarations. If you want to know more about this, google is your friend.
What you can do is having a declaration in the header file (using the keyword extern or by putting it into a class) and a definition in a (separate) cpp file. The linker will then find that definition and link the output together.
OOP
I strongly recommend to familiarize yourself with the OO concept. It will probably help you in the long run, and there might be more elegant solutions for your problem, but I won't go into detail here, see the other headings.
Analysis of your current code
This works, but I assume this is bad practice because it is creating this object every time the screens are initialized and for reasons I'm unfamiliar with. But I want to put my phrases map into a header file.
The problem is that this object as you have it now is living on the stack and will be soon destroyed (overwritten) when you leave the function. So it won't work if you want to access phrases from a different function. You can read more about object lifetime here and a little more about how scope is connected to lifetime in the first link that popped up in google.
This is giving me errors:
translations.h:
#include <string>
#include <map>
int main(){
map <string, map <string, string> > newPhrases;
map <string, string> spanish;
map <string, string> english;
spanish["hello"] = "hola";
english["hello"] = "hi";
newPhrases["spanish"] = spanish;
newPhrases["english"] = english;
return 0;
}
Best practice is to not implement your functions in header files, but only declare them there and implement them in cpp files. For main(), you don't need a declaration. Just use a cpp file.
The other thing is that you are creating newPhrases on the stack of main(), so newPhrases also only lives while main() is running. Probably not what you want.
UI.cpp:
#include "translations.h"
void CScnMgr::InitScreens(){
int extern newPhrases;
// further down where I need to display to the UI...
newPhrases[selectedLanguage]["hi"]
}
Errors:
UI.cpp: error: no match for 'operator[]' in 'newPhrases[selectedLanguage]'
int extern newPhrases is just a declaration. It tells the compiler that there is something named newPhrases somewhere (but not here) and that it is of type int. Actually you would want to tell the compiler that this thing is of type map<string, map<string, string> >. Besides, extern declarations should not be inside functions. The error itself comes from your extern declaration. The compiler thinks that newPhrases is of type int, but something of type int doesn't have a square-bracket-operator (operator[]). But even if you fixed that it would not run, so I won't go into details how to get something possibly working. (See some suggestions and links in the next section)
The general approach about localization / internationalization / multi language support
The mindset about having the desire to abstract the language and splitting it from your code is good. The question is now how to solve it. A central idiom in programming in general is "not to reinvent the wheel".
Basically, I consider your question a duplicate to this one:
C++, Multilanguage/Localisation support
Another very similar topic that I found:
Bests practices for localized texts in C++ cross-platform applications?
Yet another one:
How to support multiple language in a Linux C/C++ program?
If you want to stick to your approach, have a look at this one (performance):
C++ map<std::string> vs map<char *> performance (I know, "again?")
In the posts mentioned above, there are many suggestions, also frameworks that can handle your problem pretty well. I also recommend to do your own research because some of the questions are rather old and there might be some new stuff already. Just ask your loyal friend

Related

How do I get VC++ to auto generate thin smart pointer wrappers for a COM object?

I'm trying to import a COM interface into VC++. The COM object is from a application called IDEA, but as that is not very easy to get a hold of for others to help me. So I figure that if someone could give me instructions as to how I would do this for Word, it would be equivalent.
IDEA does have a .tlb file, but it would appear that it is incomplete. I can access the COM API using python with an example being something like this:
if __name__ == "__main__":
dbName = "Sample-Employees.IMD"
idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
db = idea.OpenDatabase(dbName) # open db
table_def = db.TableDef() # get table definition
Using the .tbl file, I can get as far as this:
#import "D:\Program Files (x86)\CaseWare IDEA\IDEA\Idea.tlb"
#include "x64\Debug\idea.tlh"
#include "x64\Debug\idea.tli"
void fn()
{
Idea::IIdeaClientPtr client;
auto db = client->OpenDatabase("Sample-Employees.IMD");
db-> // interface not defined
}
Intellisense will complete after the db-> with the following: AddRef, GetIdOfNames, GetTypeInfo, GetTypeInfoCount, Invoke, QueryInterface and Release. Thus, what I mean by an incomplete interface definition.
Now, since the python example states Idea.IdeaClient, and I've seen this with word as well (i.e. word.application), I was thinking that it might be possible to use that. Looking around though, I can't seem to find reference to that using #import. I have seen it being used with CLSIDFromProgID, but that is very manual mechanism. COM SMARTPTRs would be far more preferable.
Is this even possible to do with VC++?
Maybe OpenDatabase returns IDispatch, but interface containing TableDef is still defined in TLB.
In this case you'll need to downcast IDispatch to I-something-containing-TableDef-method.
Use QueryInterface call to get derived interface from IDispatch, not C or C++ casts, such as static_cast.
Otherwise, you'll need to use IDispatch::Invoke. The best help you have is CComPtr<IDispatch> from ATL, this template specialization have Invoke helpers, so that you can do something like this:
CComPtr<IDispatch> p;
p = db;
CComVairant result;
p.Invoke("TableDef", &result);
Or use IDispatch::Invoke as is.
Python aways relies on IDispatch::Invoke and does not use static interfaces, that's why it does not encounter this problem.

Ways of keeping ANTLR4 grammar target independent

I'm writing a grammar for C++ target, however I'd like to keep it working with Java as well since ANTLR comes with great tools that work for grammars with Java target. The book ("The Definitive ANTLR 4 Reference") says that the way of achieving target independence is to use listeners and/or visitors. There is one problem though. Any predicate, local variable, custom constructor, custom token class etc. that I might need introduces target language dependence that cannot be removed, at least according to the information I took from the book. Since the book might be outdated here are the questions:
Is there a way of declaring primitive variables in language independent way, something like:
item[$bool hasAttr]
:
type ( { $hasAttr }? attr | ) ID
;
where $bool would be translated to bool in C++, but to boolean in Java (workaround would be to use int in that case but most likely not in all potential targets)
Is there a way of declaring certain code fragments to be for specific target only, something like:
parser grammar testParser;
options
{
tokenVocab=testLexer;
}
#header
<lang=Cpp>{
#include "utils/helper.h"
}
<lang=Java>{
import test.utils.THelper;
}
#members
<lang=Cpp>{
public:
testParser(antlr4::TokenStream *input, utils::THelper *helper);
private:
utils::THelper *Helper;
public:
}
<lang=Java>{
public testParser(TokenStream input, THelper helper) {
this(input);
Helper = helper;
}
private THelper Helper;
}
start
:
(
<lang=Cpp>{ Helper->OnUnitStart(this); }
<lang=Java>{ Helper.OnUnitStart(this); }
unit
<lang=Cpp>{ _localctx = Helper->OnUnitEnd(this); }
<lang=Java>{ _localctx = Helper.OnUnitEnd(this); }
)*
EOF
;
...
For the time being I'm keeping two separate grammars changing the Java one and merging the changes to C++ one once I'm happy with the results, but if possible
I'd rather keep it in one file.
This target dependency is a real nuisance and I'm thinking for a while already how to get rid of that in a good way. Haven't still found something fully usable.
What you can do is to stay with syntax that both Java and C++ can understand (e.g. write a predicate like a function call: a: { isValid() }? b c; and implement such functions in a base class from which you derive your parser (ANTLR allows to specify such a base class via the grammar option superClass).
The C++ target also got a number of additional named actions which you can use to specify C++ specific stuff only.

Transpile an algorithm with user-suppliable callbacks into target language

I currently transpile the control flow modeled in an SCXML state-chart onto an ANSI-C algorithm which calls a series of user-supplied callback functions in the correct order, effectively realizing the control flow from the state-chart or ANSI-C. Seeing that more target languages may eventually follow, I was thinking about transpiling onto Haxe as a quasi-canonical form and use their transpilation capabilities to target other languages.
Seeing that Haxe is inherently object-oriented, I guess the best way would be to generate an abstract base class with the transpiled algorithm, which would be extended with implementations of the callbacks.
However, looking at Haxe it seems that this is a rather unorthodox usage and I am at a loss how best to approach it. I cannot find native callbacks in the target language agnostic part of Haxe, so I guess it boils down to target language specific approaches anyway?
Update: I want to invoke user-supplied callbacks in the target language. The state-chart here merely formalizes a certain control flow. There is no XML parsing involved in Haxe at all, I already parse the XML, process it and generate ANSI-C which accepts user-supplied callbacks. Now I want to take a detour via Haxe to generate any target-language, still, the user-supplied callbacks and all the "scaffolding" is in the target-language.
If you only need one listener per callback then just use a function per callback, I prefer to not anticipate what data the listener needs.
Run js example here: https://try.haxe.org/#2a6d2
code below for completeness.
class Test {
static function main() { new Test(); }
var testing: WithCallback;
public function new(){
testing = new WithCallback( output );
testing.start();
}
public function output(){
trace( 'test ' + testing.val );
}
}
class WithCallback{
public var cb: Void->Void;
public var val: String;
public function new( cb_: Void -> Void ){
cb = cb_;
}
public function start(){
for( i in 0...100 ){
val = 'callback counting ' + Std.string( i );
if( cb != null ) cb();
}
}
}
If you want multiple objects to listen then you could look at some Signal type library, I believe Tink ( macro library ) provides one but not tried it.
https://github.com/haxetink/tink_core/blob/master/src/tink/core/Signal.hx
There must be a few signals libraries around.
https://code.google.com/archive/p/hxs/
https://github.com/massiveinteractive/msignal
You may also want to look at how Json can be autoparsed in Haxe with abstracts and typedef using stuff like '#to' and '#from'.
https://haxe.org/manual/std-Json-parsing.html
So for instance a nice way to parse some json with Time field in - is to parse to a typedef with the time field using an abstract around a string and add a method within the abstract so you can get the type from the abstract.
https://haxe.org/manual/types-abstract-implicit-casts.html
I think others have worked on a similar approach for xml parsing but if you look into the internals of haxe.Json.parse I am sure you could create a similar approach for xml or binary feeds ( not sure if franco's streams stuff is relevant ). Also there is an approach to get haxe to generate the typedef code for json parsing based on a sample using really smart macros, but I guess it would be very hard to get haxe macros to construct parser based on data example.
Also you should have a look in the format library it has many example of parsing data.
https://github.com/HaxeFoundation/format

How do you load data into an attributed database class generated by the MFC application wizard?

Using the MFCApplication wizard in Visual C++ 2012, if "Generate attributed database class" is checked, a header with some special syntax (attributed C++ classes) are generated, which look like this:
// MFCApplication2Set.h: interface of the CMFCApplication2Set class
//
#pragma once
// code generated on March-05-13, 9:26 AM
[
db_source(L"Provider=SQLNCLI11.1;..."),
db_table(L"dbo.tblEmployee")
]
class CMFCApplication2Set
{
public:
... big list of stuff that corresponds to the fields in your db table omitted ...
}
The above header corresponds to a mostly empty implementation file:
// MFCApplication2Set.cpp : implementation of the CMFCApplication2Set class
//
#include "stdafx.h"
#include "MFCApplication2.h"
#include "MFCApplication2Set.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMFCApplication2Set implementation
That was the WHOLE implementation class. Notice that:
A. No parent class name is specified anywhere.
B. There's some "Visual C++ magic" going on here, that is, as a novice to modern C++, I'm lost.
db_source is documented here but the documentation is pretty thin or at least opaque to me.
Questions:
I was mystified by this syntax when I first saw it, but I figured out it's probably a variant of this Attributes feature in Visual C++. It is that, right?
How am I meant to I use these generated "attributed database class" objects? I found this documentation but if you look carefully at that documentation, that code sample is showing people the Old Way and the New Way, and is not telling me what I want to know which is how to use this new object that the IDE can not give me any documentation or code completion features for. Also since the generated code for the CMFCApplication2Set class generated by the wizard does not reference any types or class names, I'm lost. If I could even use some IDE feature to know what methods and stuff have been Magically Injected into this Magical mystery Object, I'd be better off. The only think I can think to do is to learn the old way and learn all the things you can call from the old two-separate-ATL-types world, and then somehow learn to combine them.
In a nutshell, I'm looking for the minimum syntax I need to know to actually use one of these Attributed Database Class instances, variables, as they are generated in a new MFC app by the wizard. The instance shown below is a member of an MFC document class and CMFCApplication2Set m_MFCApplication2Set is declared as a field inside the MFC document class.
What I have tried is to use this "untyped object". This object appears to have lots of data fields (m_X) and has only one method that shows up in IDE code completion, called GetRowSetProperties. Thanks to whatever magic or injection is going on, this generated Attributed Database Class (which does not visibly inherit anything) is a complete mystery to me at edit time and compile time.
Here's me just trying to inspect this thing to see if it even initialized itself when its constructor ran:
BOOL CMFCApplication2Doc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE;
TRACE( m_MFCApplication2Set.m_AStringFieldName); // outputs NOISE.
return TRUE; }
At compile time and edit time, the IDE gives me NO help about the types involved in this "anonymous" class that inherits from nothing, but which gets lots of secret powers due to some kind of Injection via those attributes I'm guessing. At debug time, I can see that there is more than just a bunch of data fields in this C++ class, but this still doesn't help me know how to use it. A minimal code sample of using one of these to go get a recordset from the database, would be great.
Update: Calling OpenDataSource is fun, because it compiles but the IDE doesn't think it should be valid. Nevertheless, it runs, and returns 0 as the result, but that doesn't actually initialize this CThingyThatVisualStudioGaveYouThatYouDontKnowWhatItIs:
This is a deprecated feature of attributed C++ code. Pre-processor expands code and replaces attributes with actual base classes. If you enable generation of these intermediate files, things are going to be more clear to you:
You will have XXX.mrg.cpp and XXX.mrg.h files generated, which you can review and see the real C++ code forwarded to compiler.
The attributes will be replaced with substituted bases classes, maps like BEGIN_COLUMN_MAP etc. The attributed source code is compact, but the feature is deprecated and looking into expanded code it should be easy (if necessary) to strip the attributes and copy expanded code right into source. It's easy with DB attributes, and more difficult with COM attributes since the internal dependencies are most sophisticated.

Are there any reasons not to use "this" ("Self", "Me", ...)?

I read this answer and its comments and I'm curious: Are there any reasons for not using this / Self / Me ?
BTW: I'm sorry if this has been asked before, it seems that it is impossible to search for the word this on SO.
Warning: Purely subjective answer below.
I think the best "reason" for not using this/self/me is brevity. If it's already a member variable/function then why redundantly add the prefix?
Personally I avoid the use of this/self/me unless it's necessary to disambiguate a particular expression for the compiler. Many people disagree with this but I haven't ever had it be a real sticking point in any group I've worked for.
I think most of the common scenarios have been covered in the two posts already cited; mainly brevity and redundancy vs clarity - a minor addition: in C#, it is required to use "this" in order to access an "extension method" for the current type - i.e.
this.Foo();
where Foo() is declared externally as:
public static void Foo(this SomeType obj) {...}
It clarifies in some instances, like this example in c#:
public class SomeClass
{
private string stringvar = "";
public SomeClass(string stringvar)
{
this.stringvar = stringvar;
}
}
If you use StyleCop with all the rules on, it makes you put the this. in. Since I started using it I find my code is more readable, but that's personal preference.
I think this is a non-issue, because it only adds more readability to the code which is a good thing.
For some languages, like PHP, it is even mandatory to prefix with $this-> if you need to use class fields or methods.
I don't like the fact that it makes some lines unnecessarily longer than they could be, if PHP had some way to reference class members without it.
I personally find that this.whatever is less readable. You may not notice the difference in a 2-line method, but wait until you get this.variable and this.othervariable everywhere in a class.
Furthermore, I think that use of this. was found as a replacement for a part of the much hated Hungarian notation. Some people out there found out that it's still clearer for the reader to see that a variable is a class member, and this. did the trick. But why fool ourselves and not use the plain old "m_" or simply "_" for that, if we need the extra clarity? It's 5 characters vs. 2 (or even 1). Less typing, same result.
Having said that, the choice of style is still a matter of personal preference. It's hard to convince somebody used to read code in a certain way that is useful to change it.
well, eclipse does color fields, arguments and local variables in different colors, so at least working in eclipse environment there is no need to syntactically distinguish fields in order to specially mark them as "fields" for yourself and generations to come.
It was asked before indeed, in the "variable in java" context:
Do you prefix your instance variable with ‘this’ in java ?
The main recurrent reason seems to be:
"it increases the visual noise you need to sift through to find the meaning of the code."
Readability, in other word... which I do not buy, I find this. very useful.
That sounds like nonsense to me. Using 'this' can make the code nicer, and I can see no problems with it. Policies like that is stupid (at least when you don't even tell people why they are in place).
as for me i use this to call methods of an instantiated object whereas self is for a static method
In VB.NET one of the common practice I use is the following code :
Class Test
Private IntVar AS Integer
Public Function New(intVar As Integer)
Me.Intvar = intvar
End Function
End Class
Not all the time but mostly Me / this / self is quite useful. Clarifies the scope that you are talking.
In a typical setter method (taken from lagerdalek's answer):
string name;
public void SetName(string name)
{
this.name = name;
}
If you didn't use it, the compiler wouldn't know you were referring to the member variable.
The use of this. is to tell the compiler that you need to access a member variable - which is out of the immediate scope of the method. Creating a variable within a method which is the same name as a member variable is perfectly legal, just like overriding a method in a class which has extended another class is perfectly legal.
However, if you still need to use the super class's method, you use super. In my opinion using this. is no worse than using super. and allows the programmer more flexibility in their code.
As far as I'm concerned readability doesn't even come into it, it's all about accessibility of your variables.
In the end it's always a matter of personal choice. Personally, I use this coding convention:
public class Foo
{
public string Bar
{
get
{
return this.bar;
}
/*set
{
this.bar = value;
}*/
}
private readonly string bar;
public Foo(string bar)
{
this.bar = bar;
}
}
So for me "this" is actually necessary to keep the constructor readable.
Edit: the exact same example has been posted by "sinje" while I was writing the code above.
Not only do I frequently use "this". I sometimes use "that".
class Foo
{
private string bar;
public int Compare(Foo that)
{
if(this.bar == that.bar)
{
...
And so on. "That" in my code usually means another instance of the same class.
'this.' in code always suggests to me that the coder has used intellisense (or other IDE equivalents) to do their heavy lifting.
I am certainly guilty of this, however I do, for purely vanity reasons, remove them afterwards.
The only other reasons I use them are to qualify an ambiguous variable (bad practice) or build an extension method
Qualifying a variable
string name; //should use something like _name or m_name
public void SetName(string name)
{
this.name = name;
}

Resources